Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Re: Find Replace within Text Boxes

15 views
Skip to first unread message
Message has been deleted

Graham Mayor

unread,
May 7, 2010, 10:59:09 AM5/7/10
to

The essential macro code is covered at
http://word.mvps.org/faQs/Customization/ReplaceAnywhere.htm and the code at
the end of that page would work in five separate operations. If you want to
combine those operations you need a pair of arrays containing the five (or
more) expressions to find and the same number of replacements

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.


Doug Robbins - Word MVP

unread,
May 7, 2010, 7:15:21 PM5/7/10
to
Please do not post the same question separately to multiple newsgroups. It
has also been answered in the other newsgroup to which you posted it.

--
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

0 new messages