Word

Process All Documents in a Specified Directory Folder

Ease of Use

Intermediate

Version tested with

 

Submitted by:

Steiner

Description:

Run your macro on all files in a specified folder. 

Discussion:

You have a whole bunch of documents in a given directory and need to do a search & replace on all of them. Instead of opening them all manually, insert your search & replace call into this macro and let VBA do the work for you. 

Code:

instructions for use

			

Sub ProcessAll(sPath As String) Dim WdDoc As Document, sFile As String sFile = Dir(sPath & "*.doc") 'Loop through all .doc files in that path Do While sFile <> "" Set WdDoc = Application.Documents.Open(sPath & sFile) 'Do something with that Document, insert whatever you want to do here Debug.Print WdDoc.Name 'You can save it, if you like, here it's not saved WdDoc.Close wdDoNotSaveChanges sFile = Dir Loop End Sub

How to use:

  1. Copy the code above.
  2. Create a new document.
  3. Hit Alt+F11 to open the Visual Basic Editor (VBE).
  4. From the menu, choose Insert-Module.
  5. Paste the code into the code window at right.
  6. Add the code that will perform the process you desire.
  7. Close the VBE.
 

Test the code:

  1. Create a directory, let's call it "C:\test\"
  2. Be sure to add the backslash at the end of the directory name in the code!
  3. Place several Word docs in there. (Password protection requires additional code; ask at the forum.)
  4. Hit Tools-Macro-Macros and double-click ProcessAll
 

Sample File:

No Attachment 

Approved by Anne Troy


This entry has been viewed 155 times.

Please read our Legal Information and Privacy Policy
Copyright @2004 - 2020 VBA Express