Microsoft Office Tutorials and References
In Depth Information
Finding and Replacing in Multiple Documents
Finding and Replacing in Multiple Documents
This procedure demonstrates how to use common Office dialogs and how to loop
Find through all parts of a document.
Example file:
W002_1.doc and
Scenario: The macro recorder is useful, but when the result
is played back, the behavior does not always correspond to
what happens in the user interface. One excellent example of
this is recording Edit | Find or Edit | Replace. In the user
interface, Find and Replace processes the entire document,
including headers, footers, footnotes and drawing objects. It
is rather a nasty surprise to find out that the recorded macro
only works in the current "document story"; that is, the main
body OR the header, OR the footer, OR the drawing objects.
The macro recorder also cannot record looping through and
processing all the files in a selected folder.
This macro combines these two tasks. It loops through all Word files in the
folder selected from the dialog box, opens each one in turn, searches for fields
that link to outside files, and changes the file path. This approach can be
adapted to find other things, such as the need to replace a company logo or to
take a desired action.
View the Appendix to learn how to store this procedure
in a Standard module.
Option explicit¶
' * * * * *¶
'Finds a field code¶
Const FindText = "^d"¶
' * * * * *¶
Sub ChangeLinks()¶
'Variable declaration¶
Dim FilePath As String¶
Dim linkPath As String¶
Dim securitySetting As Long¶
FilePath = GetFileFolder("Select folder to process")¶
'User cancelled¶
If Len(FilePath) = 0 Then Exit Sub¶
linkPath = GetFileFolder("Select path to linked file")¶
'User cancelled¶
If Len(linkPath) = 0 Then Exit Sub¶
Search JabSto ::

Custom Search