Dang it, I hate hitting the escape key by accident right after typing a long post.
Okay, the error you got was because you need a trailing backslash at the end of the FILE_PATH constant assignment to denote it as a directory. It should've been
Const FILE_PATH As String = "Y:\Client Services\CS\"
I've changed the code below to reflect your directory tree.
The old code is scrapped. The new (working) code is below. There are three big changes: dumped
GetObject for
Documents.Open, swapped
Replace for a more Word-native
Find method, and changed the address constants to String objects because you can't programmatically add a tab character to a constant.
Here it be:
Code:
Public Sub FixAddresses()
Dim wdDoc As Document
Dim strFileName As String
Dim OLD_ADDRESS As String
Dim NEW_ADDRESS As String
'Change the path to point to the appropriate folder
Const FILE_PATH As String = "Y:\Client Services\CS\"
'Change the constants below to reflect the appropriate
'street addresses including trailing spaces. Chr$(9)
'represents a tab.
OLD_ADDRESS = "XXXXXXXXXXXXX " & Chr$(9) & _
Chr$(9) & _
Chr$(9) & _
Chr$(9) & _
Chr$(9) & _
Chr$(9) & _
Chr$(9) & " "
NEW_ADDRESS = "YYYYYYYY " & Chr$(9) & _
Chr$(9) & _
Chr$(9) & _
Chr$(9) & _
Chr$(9) & _
Chr$(9) & _
Chr$(9) & _
Chr$(9) & " "
Application.ScreenUpdating = False
strFileName = Dir(FILE_PATH)
Do While Len(strFileName & vbNullString) <> 0
Set wdDoc = Word.Documents.Open(FILE_PATH & strFileName)
With ActiveDocument
With .Range.Find
.ClearFormatting
.Text = OLD_ADDRESS
With .Replacement
.ClearFormatting
.Text = NEW_ADDRESS
End With
.Execute Replace:=wdReplaceAll, _
Format:=True, MatchCase:=True, _
MatchWholeWord:=True
End With
.Save
.Close
End With
Set wdDoc = Nothing
strFileName = Dir
Loop
Application.ScreenUpdating = True
End Sub
You'll have to play with the address "constants" to get the spacing just right. If you need help with that, let me know; or, if the way I've assigned them now (on multiple lines) is confusing you, let me know that too.
HTH, test it on your copies
chris.
PS tested on 64 docs, took about 30 seconds. GL