pFindTxt = Array("Word1", "Word2", "Word3", "Word4", "Word5")
pReplaceTxt = Array("Replacement1", "Replacement2", "Replacement3", _
"Replacement4", "Replacement5")
http://www.gmayor.com/installing_macro.htm
Option Explicit
Public Sub FindReplaceAnywhere()
Dim rngStory As Word.Range
Dim pFindTxt As Variant
Dim pReplaceTxt As Variant
Dim lngJunk As Long
Dim i As Long
Dim oShp As Shape
pFindTxt = Array("Word1", "Word2", "Word3", "Word4", "Word5")
pReplaceTxt = Array("Replacement1", "Replacement2", "Replacement3", _
"Replacement4", "Replacement5")
For i = 0 To UBound(pFindTxt)
TryAgain:
'Fix the skipped blank Header/Footer problem
lngJunk = ActiveDocument.Sections(1).Headers(1).Range.StoryType
'Iterate through all story types in the current document
For Each rngStory In ActiveDocument.StoryRanges
'Iterate through all linked stories
Do
SearchAndReplaceInStory rngStory, pFindTxt(i), pReplaceTxt(i)
On Error Resume Next
Select Case rngStory.StoryType
Case 6, 7, 8, 9, 10, 11
If rngStory.ShapeRange.Count > 0 Then
For Each oShp In rngStory.ShapeRange
If oShp.TextFrame.HasText Then
SearchAndReplaceInStory oShp.TextFrame.TextRange, _
pFindTxt(i), pReplaceTxt(i)
End If
Next
End If
Case Else
'Do Nothing
End Select
On Error GoTo 0
'Get next linked story (if any)
Set rngStory = rngStory.NextStoryRange
Loop Until rngStory Is Nothing
Next rngStory
Next i
End Sub
Public Sub SearchAndReplaceInStory(ByVal rngStory As Word.Range, _
ByVal strSearch As String, ByVal strReplace As String)
With rngStory.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = strSearch
.Replacement.Text = strReplace
.Wrap = wdFindContinue
.Execute Replace:=wdReplaceAll
End With
End Sub
--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP
My web site www.gmayor.com
Word MVP web site http://word.mvps.org
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
"KurtB" <kurta...@gmail.com> wrote in message
news:6d179971-2e1c-4341...@b18g2000yqb.googlegroups.com...
>I have a piece of software that exports data to an RTF file with each
> line as a separate textbox. The documents are usually 5 pages. I need
> to manually go in and change some of the text (department name,
> address etc.) because the software doesn't let me.
>
> I'm looking for a macro to search through every text box and replace
> certain pieces of text. I have about 5 different find replaces I run
> on each document.
>
> Any help is appreciated.
--
Hope this helps.
Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.
Doug Robbins - Word MVP, originally posted via msnews.microsoft.com