find and replace in footer

  • Follow


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! 


0
Reply Utf 4/7/2010 5:14:02 PM

Never mind - I found out what my problem was - and it works great now!!

"PJY" wrote:

> 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! 
> 
> 
0
Reply Utf 4/7/2010 5:20:02 PM


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

PJY wrote:
>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!

-- 
Gerry

Message posted via OfficeKB.com
http://www.officekb.com/Uwe/Forums.aspx/word-programming/201004/1

0
Reply Fumei2 4/7/2010 5:36:53 PM

2 Replies
1663 Views

(page loaded in 0.068 seconds)

Similiar Articles:
















7/22/2012 3:20:01 AM


Reply: