K
Kelly
I need to be able to do a search and replace in the header of Word
documents. The following VBS code works on the body of Word documents, but
not the header. Help much appreciated.
Const wdReplaceAll = 2
Const END_OF_FILE = 6
Const MOVE_SELECTION = 0
Set objWord = CreateObject("Word.Application")
Set objDoc = objWord.Documents.Add()
Do While objPath =""
objPath = InputBox("Enter path (C:\Junk): ", "File")
Loop
objSearch = InputBox("Enter search string: ", "Search")
objReplace = InputBox("Enter replace string: ", "Replace")
objWord.FileSearch.Filename = "*.doc"
objWord.FileSearch.LookIn = objPath
objWord.FileSearch.SearchSubfolders = False
objWord.FileSearch.Execute
objWord.Visible = FALSE
Set objWord2 = CreateObject("Word.Application")
For Each objFile in objWord.FileSearch.FoundFiles
objWord2.visible = FALSE
Set objDoc2 = objWord2.Documents.Open(objFile)
set objSelection = objWord2.Selection
objSelection.Find.Text = objSearch
objSelection.Find.Forward = TRUE
objSelection.Find.MatchWholeWord = FALSE
objSelection.Find.Replacement.Text = objReplace
objSelection.Find.Execute ,,,,,,,,,,wdReplaceAll
If Not objDoc2.Saved Then
objDoc2.Save
End If
objDoc2.Close
Next
objword2.Quit
objWord.Quit
Set objWord = Nothing
Set objWord2 = Nothing
MsgBox("Finished")
documents. The following VBS code works on the body of Word documents, but
not the header. Help much appreciated.
Const wdReplaceAll = 2
Const END_OF_FILE = 6
Const MOVE_SELECTION = 0
Set objWord = CreateObject("Word.Application")
Set objDoc = objWord.Documents.Add()
Do While objPath =""
objPath = InputBox("Enter path (C:\Junk): ", "File")
Loop
objSearch = InputBox("Enter search string: ", "Search")
objReplace = InputBox("Enter replace string: ", "Replace")
objWord.FileSearch.Filename = "*.doc"
objWord.FileSearch.LookIn = objPath
objWord.FileSearch.SearchSubfolders = False
objWord.FileSearch.Execute
objWord.Visible = FALSE
Set objWord2 = CreateObject("Word.Application")
For Each objFile in objWord.FileSearch.FoundFiles
objWord2.visible = FALSE
Set objDoc2 = objWord2.Documents.Open(objFile)
set objSelection = objWord2.Selection
objSelection.Find.Text = objSearch
objSelection.Find.Forward = TRUE
objSelection.Find.MatchWholeWord = FALSE
objSelection.Find.Replacement.Text = objReplace
objSelection.Find.Execute ,,,,,,,,,,wdReplaceAll
If Not objDoc2.Saved Then
objDoc2.Save
End If
objDoc2.Close
Next
objword2.Quit
objWord.Quit
Set objWord = Nothing
Set objWord2 = Nothing
MsgBox("Finished")