Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Word - Find Replace only in one section 2

Status
Not open for further replies.

waubain

Technical User
Dec 13, 2011
200
US
I have a Word document with 4 sections. The following code works, but is finding and replacing in the entire document rather than only in Section 3 as I wish. Any suggestions are greatly appreciated.

Code:
Sub PrefixPipe()

Dim vFindText As Variant
Dim vReplText As Variant
Dim sFindText As String
Dim sReplText As String
Dim i As Long

vFindText = Array("^w^p", "OPT", "INP", "Remote", "Non VA")
vReplText = Array("^p", "|OPT|", "|INP|", "|Remote|", "|NonVA|")
For i = LBound(vFindText) To UBound(vFindText)
    sFindText = vFindText(i)
    sReplText = vReplText(i)
    With ActiveDocument.Sections(3).Range.Find
        .Forward = True
        .Wrap = wdFindContinue
        .MatchWholeWord = True
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
        .Format = True
        .MatchCase = False
        .Text = sFindText
        .Replacement.Text = sReplText
        .Execute Replace:=wdReplaceAll
    End With
Next i

End Sub

You don't know what you don't know...
 
I'd replace wdFindContinue with wdFindStop

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
PHV,

Worked perfectly. This is my first big project in Word and I was adapting what I need from a slightly different example. I was looking for the problem in the wrong place.

Thank you again.

You don't know what you don't know...
 
hi,
Code:
        With ThisDocument.Sections(3).Range.Find
            .ClearFormatting
            .Text = sFindText
            With .Replacement
                .ClearFormatting
                .Text = sReplText
            End With
            .Execute Replace:=wdReplaceAll, _
                Format:=True, MatchCase:=True, _
                MatchWholeWord:=True
        End With

Skip,

[glasses]Just traded in my OLD subtlety...
for a NUance![tongue]
 
Skip,

Thank you for providing an alternative. When learning, it helps to see the various ways to get the same results.

Bob

You don't know what you don't know...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top