vba code to run a macro on multiple Word Files

vba code to run a macro on multiple Word Files

You can use the below vba code, posted here, to run a single macro on multiple Word files. My modification of this code will run a macro to list all of the hyperlinks in a set of Word files.

Follow these steps:

1. Open a new Word document.

2. Press ALT + F11 to open Visual Basic.

3. In the project list, select 'ThisDocument', right click and insert a new module.

4. Paste the below code into the module.

5. As you can see this consists of two macros. The first RunMacroMultipleFiles runs the second ListHyperlinks.

6. Edit the eighth line which begins, "path =" to list the file path of the folder which contains the Word files you want to process.

7. Three lines down you can change the macro to either process files in the old .doc format, or the newer .docx format.

8. Five lines from the bottom edit the first macro to call the second macro below.

9. The macro will open each file in the designated folder and create new documents with tables listing the links in the source documents, and the text displayed for those links.

As always, I tested out this macro tonight and confirmed that it works.

Sub RunMacroMultipleFiles()

Dim file

Dim path As String

' this code taken from Running a macro on all files in a folder - Tips for Module Creation

' Path to your folder. MY folder is listed below. I bet yours is different.

' make SURE you include the terminating "\"


path = "C:\FooFolder\doc\"

'Change this file extension to the file you are opening. .htm is listed below. You may have rtf or docx.


file = Dir(path & "*.docx")

Do While file <> ""

Documents.Open FileName:=path & file

' This is the call to the macro you want to run on each file the folder

'YOU MUST EDIT THIS. lange01 is my macro name. You put yours here.

Call ListHyperlinks

' set file to next in Dir

file = Dir()


End Sub

Sub ListHyperlinks() Dim srcDoc As Document, destDoc As Document Dim HL As Hyperlink

Set srcDoc = ActiveDocument Set destDoc = Documents.Add

For Each HL In srcDoc.Hyperlinks destDoc.Range.InsertAfter _ HL.TextToDisplay & vbTab & HL.Address & vbCr Next

destDoc.Range.ConvertToTable End Sub


Contact Me With Your Litigation Support Questions:


  • Twitter Long Shadow

© 2015 by Sean O'Shea . Proudly created with Wix.com