F
feagin
I am very new at VBA, and found this macro on the web that
will do search and replace everywhere, including headers.
But I cannot make it run on my Word 2002 documents. How do
I set up a dialog box? And Do I need a 'sub' line to begin
it? thanks
Option Explicit
Sub RunAllSearchAndReplaces()
' Call this routine to perform the complete search and
' replace - changed by CLR to use ActiveDocument
instead
' of a passed Document object for simplicity's sake.
Dim rgLoop As Word.Range
Dim secLoop As Word.Section
Dim hfLoop As Word.HeaderFooter
For Each rgLoop In ActiveDocument.StoryRanges
Select Case rgLoop.StoryType
Case wdEvenPagesFooterStory,
wdEvenPagesHeaderStory, _
wdFirstPageFooterStory,
wdFirstPageHeaderStory, _
wdPrimaryFooterStory, wdPrimaryHeaderStory
Case Else
RunSRthroughRange rgLoop
Do Until (rgLoop.NextStoryRange Is Nothing)
Set rgLoop = rgLoop.NextStoryRange
RunSRthroughRange rgLoop
Loop
End Select
Next rgLoop
For Each secLoop In ActiveDocument.Sections
For Each hfLoop In secLoop.Headers
RunSRthroughRange hfLoop.Range
Next hfLoop
For Each hfLoop In secLoop.Footers
RunSRthroughRange hfLoop.Range
Next hfLoop
Next secLoop
End Sub
Private Sub RunSRthroughRange(DoTo As Range)
' This routine performs the actual search and replace -
' any other operation can of course be performed on
the range.
DoTo.Find.Execute FindText:="hi",
ReplaceWith:="hello", Replace:=wdReplaceAll
End Sub
will do search and replace everywhere, including headers.
But I cannot make it run on my Word 2002 documents. How do
I set up a dialog box? And Do I need a 'sub' line to begin
it? thanks
Option Explicit
Sub RunAllSearchAndReplaces()
' Call this routine to perform the complete search and
' replace - changed by CLR to use ActiveDocument
instead
' of a passed Document object for simplicity's sake.
Dim rgLoop As Word.Range
Dim secLoop As Word.Section
Dim hfLoop As Word.HeaderFooter
For Each rgLoop In ActiveDocument.StoryRanges
Select Case rgLoop.StoryType
Case wdEvenPagesFooterStory,
wdEvenPagesHeaderStory, _
wdFirstPageFooterStory,
wdFirstPageHeaderStory, _
wdPrimaryFooterStory, wdPrimaryHeaderStory
Case Else
RunSRthroughRange rgLoop
Do Until (rgLoop.NextStoryRange Is Nothing)
Set rgLoop = rgLoop.NextStoryRange
RunSRthroughRange rgLoop
Loop
End Select
Next rgLoop
For Each secLoop In ActiveDocument.Sections
For Each hfLoop In secLoop.Headers
RunSRthroughRange hfLoop.Range
Next hfLoop
For Each hfLoop In secLoop.Footers
RunSRthroughRange hfLoop.Range
Next hfLoop
Next secLoop
End Sub
Private Sub RunSRthroughRange(DoTo As Range)
' This routine performs the actual search and replace -
' any other operation can of course be performed on
the range.
DoTo.Find.Execute FindText:="hi",
ReplaceWith:="hello", Replace:=wdReplaceAll
End Sub