I adapted
http://word.mvps.org/faqs/macrosvba/...AllWithVBA.htm
to a what I hope / wanted / intended to be a 'general purpose', 'global', 'any where in the document' S&R sub
Sub StrReplace(OldStr As String, NewStr As String, _
Optional WholeWord = False, _
Optional MatchCase = False, _
Optional WildCard = False)
Dim xStory As Range, xStory1 As Range
'There are 17 different types of stories that can be part of a document,
' wdMainTextStory
' wdTextFrameStory
' wdPrimaryFooterStory, wdFirstPageFooterStory , wdEvenPagesFooterStory
' wdPrimaryHeaderStory, wdFirstPageHeaderStory, wdEvenPagesHeaderStory
' wdFootnotesStory, wdFootnoteContinuationNoticeStory, wdFootnoteContinuationSeparatorStory, wdFootnoteSeparatorStory
' wdCommentsStory
' wdEndnotesStory, wdEndnoteContinuationNoticeStory, wdEndnoteContinuationSeparatorStory, wdEndnoteSeparatorStory
For Each xStory In ActiveDocument.StoryRanges
'
Set xStory1 = xStory
With xStory1.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = OldStr
.Replacement.Text = NewStr
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = MatchCase
.MatchWholeWord = WholeWord
.MatchWildcards = WildCard
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute Replace:=wdReplaceAll
End With
'As mentioned previously, the above code will only act upon the first story for
'each story type in the document. (The first Header, the first Text Box, and
'so on). If your document contains sections with un-linked headers and
'footers in them, or, for example, contains more than one Text Box, the
'code will not act upon the second and subsequent occurrences of each type
'of story. So to make sure that the code does act on each occurrence of the
'text, no matter where it appears, you have to make use of the
'NextStoryRange method as in the following code:
Do While Not (xStory1.NextStoryRange Is Nothing)
Set xStory1 = xStory1.NextStoryRange
With xStory1.Find
.Text = OldStr
.Replacement.Text = NewStr
.Forward = True
.Wrap = wdFindContinue 'Continue
.Format = False
.MatchCase = MatchCase
.MatchWholeWord = WholeWord
.MatchWildcards = WildCard
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute Replace:=wdReplaceAll
End With
Loop
Next
End Sub
Not stress tested by millions of users, but seems to work.
There is a note, but the web site might be out of date, i.e. pre-2007
Unfortunately, even this method doesn't work reliably if you have any blank Headers or Footers in your document. If, for example, you have a document in which the first section has a blank primary Header in the first section (such as might be the case for a report cover sheet), then
none of the primary Headers in the subsequent sections will be checked! Another thing that is well worth contacting
http://support.microsoft.com/contactus/ about.
Paul