How to Run a Macro on Multiple Files in MS Word

Written by darrin koltow
  • Share
  • Tweet
  • Share
  • Email

Since work and personal projects can easily extend beyond a single Word document, it makes sense to enable your macros to work on multiple documents. You can do this by using Visual Basic's "Open" command to load into Word a file on which you want to run your macro. Process the file with your macro, such as replacing text, or excerpting it to offer a preview of a document. After running the macro, use Visual Basic's "Close" command to save the document to disk.

Skill level:

Other People Are Reading


  1. 1

    Click the "Office" button's "Open" command, then navigate to and double-click a file with a macro you'd like to run on multiple files. Click the "Developer" tab's "Macros" button to display a list of macros in the document you just loaded.

  2. 2

    Click the name of a macro, then click the "Edit" button to enter the Visual Basic integrated development environment. Type the following before the first statement of the macro. These statements prompt your macro's user for a filename on which to run your macro. The "While" statement begins a loop, within which the "Open" function opens the Word file specified by the user.

    Dim x

    X = InputBox ("Enter filename")

    While (x <> "")

    Documents.Open x


  3. 3

    Type over the comment "YOUR CODE BEGINS HERE" with text describing your macro's purpose. For example, if you've written a macro that replaces each occurrence of the word "Marianne" with "Ginger," you might type "Begin 'Find/Replace' code for Marianne/Ginger text'"

  4. 4

    Type the following after the last statement of your macro. These statements save and close the file opened by the "Open" statement and prompt your macro's user for another filename. The "Wend" statement ends the loop that began with the "While" statement.

    ActiveDocument.Close wdSaveChanges

    X = InputBox ("Enter filename")


  5. 5

    Click the "Word" icon on the Windows taskbar to return to Word, then click the "Macros" button on the "Developer" tab. Double-click the name of your macro to run it.

  6. 6

    Type the name of a file you'd like your macro to run on, when the "Enter filename" prompt you wrote earlier appears. Press "Enter" to let your macro process the file.

  7. 7

    Repeat the previous step until you've typed the name of the last file on which you'd like your macro to run. The next time the "Enter filename" prompt appears, press "Enter" without entering a filename. Your macro will exit.

Don't Miss

  • All types
  • Articles
  • Slideshows
  • Videos
  • Most relevant
  • Most popular
  • Most recent

No articles available

No slideshows available

No videos available

By using the site, you consent to the use of cookies. For more information, please see our Cookie policy.