Thread: [Solved] Puzzling Behavior
View Single Post
 
Old 03-26-2024, 09:44 AM
Italophile Italophile is offline Windows 11 Office 2021
Expert
 
Join Date: Mar 2022
Posts: 542
Italophile is just really niceItalophile is just really niceItalophile is just really niceItalophile is just really nice
Default

Find doesn't work simply because you are attempting to execute multiple find operations within a looped find.

Instead try the following:

Code:
Sub PrepDocument()
    '
    ' Temporarily rename "5AX"
    '
    '
    With ActiveDocument.Content.Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
        .Text = "5AX TRIM"
        .Replacement.Text = "BAD TRIM"
        .Execute Replace:=wdReplaceAll
        .Text = "-."
        .Replacement.Text = "-0."
        .Execute Replace:=wdReplaceAll
        .Text = "X."
        .Replacement.Text = "X0."
        .Execute Replace:=wdReplaceAll
        .Text = "Y."
        .Replacement.Text = "Y0."
        .Execute Replace:=wdReplaceAll
    End With


End Sub

Sub SwitchXAndY()

    Dim findRange As Range: Set findRange = ActiveDocument.Content
    Dim switchText() As String

    On Error Resume Next

    ' Go to the string "X*Y"
    With findRange
        With .Find
            .ClearFormatting
            .Forward = True
            .MatchWildcards = True
            .Wrap = wdFindStop
            .Text = "X*Z"
        End With
        Do While .Find.Execute = True
            .MoveEnd wdCharacter, -2
            switchText = Split(.Text, " ")
            .Text = switchText(1) & " " & switchText(0)
            .Collapse wdCollapseEnd
        Loop
    End With
End Sub
Reply With Quote