How to Use Word VBA FileDialog to Select Two Documents
Question details
The user wants to use a VBA script with the FileDialog picker to select two different Word documents and accurately assign them to separate object variables.

- Product
- Word
- Device & OS
- not provided
- Scenario
- Developing a Word VBA macro or UserForm that requires opening two different documents simultaneously for comparison, merging, or data extraction.
- Observed behavior
- The goal is to capture two separate file paths and initialize them as Doc1 and Doc2 without the second document overwriting the first variable.
Ensure the Developer tab is enabled in your Word processor's ribbon, and remember to save your work before running or testing new macro scripts.
Use FileDialog with MultiSelect Enabled
Enable the AllowMultiSelect property in a single FileDialog prompt to let users pick two files at once, then assign them via an array index.
This method is the most efficient if the two documents do not have strict order dependencies (like 'Source' vs. 'Destination'). It opens one window, allowing the user to CTRL-click two files.
Press ALT + F11 in your Word processor to launch the Visual Basic for Applications (VBA) editor.
Click on 'Insert' in the top menu and select 'Module' to create a blank script window.
Declare your variables: `Dim Doc1 As Document, Doc2 As Document`. Initialize the FileDialog with `Set fd = Application.FileDialog(msoFileDialogFilePicker)` and set `fd.AllowMultiSelect = True`.
Use an If statement to verify `fd.SelectedItems.Count >= 2`. Inside the statement, assign the documents by writing `Set Doc1 = Documents.Open(fd.SelectedItems(1))` and `Set Doc2 = Documents.Open(fd.SelectedItems(2))`.

Prompt FileDialog Twice for Sequential Selection
If you need to specify exactly which file is the source and which is the target, prompt the user twice with distinct dialog titles.
Automate Documents with WPS Office VBA
WPS Office provides robust support for VBA macros, allowing you to run your FileDialog scripts and automate advanced document processing seamlessly just like in Microsoft Word.
- 1. Download WPS Office: Download and install WPS Office Free from the official website.
- 2. Open WPS Writer: Launch WPS Writer and open a new or existing document.
- 3. Access the Developer Tools: Navigate to the 'Tools' tab on the top ribbon.
- 4. Launch the Macro Editor: Click on 'Macros' to access the VBA Editor and paste your FileDialog macro code.
- 5. Run the Script: Execute the macro to seamlessly select and open multiple documents side-by-side.

Frequently Asked Questions
Why is my second document overwriting the first one in VBA?
This usually happens if you are re-using the same Document object variable (e.g., `Set Doc1 = ...`) for the second file instead of declaring a new object and assigning it to a separate variable like `Doc2`.
Can I restrict the VBA FileDialog to only show Word documents?
Yes, you can add filters to the FileDialog object to restrict extensions. Use the code `fd.Filters.Add "Word Documents", "*.doc; *.docx; *.docm"` before calling the `.Show` method.
What happens if the user cancels the FileDialog prompt?
If the user clicks Cancel, the FileDialog `.Show` method returns `0`. You should wrap your execution code in an `If fd.Show = -1 Then` statement to proceed only when files are selected, preventing runtime errors.




