Others

How to Use Word VBA FileDialog to Select Two Documents

Olivia MillerOlivia Miller Nov 3, 2026 868 views

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.

How to Use Word VBA FileDialog to Select Two Documents
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.
Before you start

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.

Solution 1Recommended

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.

1
Open the VBA Editor

Press ALT + F11 in your Word processor to launch the Visual Basic for Applications (VBA) editor.

2
Insert a New Module

Click on 'Insert' in the top menu and select 'Module' to create a blank script window.

3
Write the MultiSelect Code

Declare your variables: `Dim Doc1 As Document, Doc2 As Document`. Initialize the FileDialog with `Set fd = Application.FileDialog(msoFileDialogFilePicker)` and set `fd.AllowMultiSelect = True`.

4
Assign the Variables

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))`.

Use FileDialog with MultiSelect Enabled
Successful Assignment: Your script will now hold the first selected file as Doc1 and the second as Doc2, allowing you to manipulate both simultaneously without overwriting.

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. 1. Download WPS Office: Download and install WPS Office Free from the official website.
  2. 2. Open WPS Writer: Launch WPS Writer and open a new or existing document.
  3. 3. Access the Developer Tools: Navigate to the 'Tools' tab on the top ribbon.
  4. 4. Launch the Macro Editor: Click on 'Macros' to access the VBA Editor and paste your FileDialog macro code.
  5. 5. Run the Script: Execute the macro to seamlessly select and open multiple documents side-by-side.
Fully compatible with Microsoft Word VBA scripts, user forms, and macros.Lightweight architecture for faster execution of macro-enabled documents (.docm).Built-in VBA editor with a familiar interface for developers.
microsoft office alternative - wps office

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.