find and replace in footer

P

PJY

Hi all, Once again I need to ask for your expertise. I have created a macro
that will open a file, remove the password, find text in the footer, then
replace that text, close the file, and move on to the next file in the folder
and so far it works OK, except I cannot get it to work on the first file in
the folder. Can anyone tell me where I went wrong? Here's what I have so
far:

Sub ReplaceFooter()
Dim myFile As String
Dim PathToUse As String
Dim myDoc As Document
Dim Response As String
PathToUse = "U:\FindReplace\"
On Error Resume Next
Documents.Close SaveChanges:=wdPromptToSaveChanges

myFile = Dir$(PathToUse & "*.doc")
While myFile <> ""
Set myDoc = Documents.Open(PathToUse & myFile)
myDoc.Unprotect "cmf"
With ActiveWindow.View
..ShowRevisionsAndComments = False
..RevisionsView = wdRevisionsViewFinal
End With
Dim myStoryRange As Range

For Each myStoryRange In ActiveDocument.StoryRanges
With myStoryRange.Find
.Text = "(ILLINOIS - STD.)"
.Replacement.Text = "(123456)"
.Wrap = wdFindContinue
.Execute Replace:=wdReplaceAll
End With
Do While Not (myStoryRange.NextStoryRange Is Nothing)
Set myStoryRange = myStoryRange.NextStoryRange
With myStoryRange.Find
.Text = "findme"
.Replacement.Text = ""
.Wrap = wdFindContinue
.Execute Replace:=wdReplaceAll
End With
Loop
Next myStoryRange

myDoc.Close SaveChanges:=wdSaveChanges
myFile = Dir$()
Wend

End Sub

Thanks in advance for your time and sharing of our knowledge!
 
F

Fumei2 via OfficeKB.com

If you know it just needs to action the footers, then you do not need to loop
through ALL of the StoryRanges.

Sub ReplaceFooter_2()
Dim myFile
Dim PathToUse As String
Dim oHF As HeaderFooter
Dim oSection As Section

PathToUse = "U:\FindReplace\"

myFile = Dir$(PathToUse & "*.doc")
Do While myFile <> ""
Documents.Open (PathToUse & myFile)
ActiveDocument.Unprotect "cmf"
For Each oSection In ActiveDocument.Sections
For Each oHF In oSection.Footers
On Error Resume Next
oHF.Range.Text = _
Replace(oHF.Range.Text, "(ILLINOIS - STD.)", _
"(123456)")
Next
Next
ActiveDocument.Save
ActiveDocument.Close
myFile = Dir()
Loop
End Sub
Never mind - I found out what my problem was - and it works great now!!
Hi all, Once again I need to ask for your expertise. I have created a macro
that will open a file, remove the password, find text in the footer, then
[quoted text clipped - 47 lines]
Thanks in advance for your time and sharing of our knowledge!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top