rybozome
04-17-2013, 02:59 PM
First post, so I apologize if I miss any vbaexpress etiquette. I am currently in the process of updating a large amount of MS Word documents that reference other MS Word documents in them (because we changed the name of a bunch of the documents).
I have written a code that I would like to search a certain folder directory and go through and open each document to execute the find & replace, but it does not seem to be working. As soon as it hits my "Do While", it just skips the whole loop entirely and goes to End Sub. What am I doing wrong? Thanks in advance. Here is what I currently have:
Sub MassReplace()
Dim strPath As String
Dim strFile As String
Dim doc As Document
Application.ScreenUpdating = False
strPath = InputBox("Insert filepath you would like to search")
strFile = Dir(strPath + "*.docx")
Do While strFile <> ""
Documents.Open (strPath & strFile)
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "docname"
.MatchCase = True
.Replacement.Text = "newdocname"
End With
Selection.Find.Execute Replace:=wdReplaceAll
doc.Close True
Loop
Application.ScreenUpdating = True
End Sub
I have written a code that I would like to search a certain folder directory and go through and open each document to execute the find & replace, but it does not seem to be working. As soon as it hits my "Do While", it just skips the whole loop entirely and goes to End Sub. What am I doing wrong? Thanks in advance. Here is what I currently have:
Sub MassReplace()
Dim strPath As String
Dim strFile As String
Dim doc As Document
Application.ScreenUpdating = False
strPath = InputBox("Insert filepath you would like to search")
strFile = Dir(strPath + "*.docx")
Do While strFile <> ""
Documents.Open (strPath & strFile)
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "docname"
.MatchCase = True
.Replacement.Text = "newdocname"
End With
Selection.Find.Execute Replace:=wdReplaceAll
doc.Close True
Loop
Application.ScreenUpdating = True
End Sub