View Single Post
 
Old 03-19-2022, 03:35 AM
gmayor's Avatar
gmayor gmayor is offline Windows 10 Office 2019
Expert
 
Join Date: Aug 2014
Posts: 4,144
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

That's just a matter of applying a highlight to the replacements e.g
Code:
Option Explicit
'Graham Mayor - https://www.gmayor.com - Last updated - 19 Mar 2022
'The Symbol function is based on code from https://wordmvp.com/FAQs/MacrosVBA/FindReplaceSymbols.htm.


Private OriginalRange As Range, oPara As Range, oChar As Range
Private vFindText As Variant
Private vReplaceText As Variant
Private oRng As Range
Private i As Long
Private lHiLite As Long


Sub Macro1()
    lHiLite = Options.DefaultHighlightColorIndex
    Options.DefaultHighlightColorIndex = wdYellow
    vFindText = Array("( [\>\<\+\=])([0-9])", "([! ])([\>\<\+\=])([0-9])")
    vReplaceText = Array("\1 \2", "\1 \2 \3")
    For i = 0 To UBound(vFindText)
        Set oRng = ActiveDocument.Range
        With oRng.Find
            .ClearFormatting
            .Replacement.ClearFormatting
            .Replacement.Highlight = True
            .Execute findText:=vFindText(i), _
                     MatchWildcards:=True, _
                     Forward:=True, _
                     Wrap:=wdFindStop, _
                     Replacewith:=vReplaceText(i), _
                     Replace:=wdReplaceAll
        End With
    Next i

    Set oRng = ActiveDocument.Range
    With oRng.Find
        .ClearFormatting
        .Replacement.ClearFormatting
        Do While .Execute(findText:=ChrW(247), _
                          MatchWildcards:=False, _
                          Forward:=True, _
                          Wrap:=wdFindStop)
            If oRng.Start = oRng.Paragraphs(1).Range.Start Then
                oRng.Text = ChrW(247) & Chr(32)
            Else
                oRng.Text = Chr(32) & ChrW(247) & Chr(32)
            End If
            oRng.HighlightColorIndex = wdYellow
            oRng.Collapse 0
        Loop
    End With

    Call ReplaceSymbol(FindChar:=ChrW(-3916), _
                       FindFont:="Symbol", _
                       ReplaceChar:=-3916, _
                       ReplaceFont:="Symbol")
lbl_Exit:
    Options.DefaultHighlightColorIndex = lHiLite
    Set oRng = Nothing
    Exit Sub
End Sub

Sub ReplaceSymbol(FindChar As String, FindFont As String, _
                  ReplaceChar As String, ReplaceFont As String)
    Set OriginalRange = Selection.Range
    ActiveDocument.Range(0, 0).Select

    With Selection.Find
        .ClearFormatting
        .Text = FindChar
        .Replacement.ClearFormatting
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindStop
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False

        Do While .Execute
            'keep searching until nothing found
            If Dialogs(wdDialogInsertSymbol).Font = FindFont Then
                'Insert the replacement symbol where the found symbol was
                Set oChar = Selection.Range
                oChar.HighlightColorIndex = wdYellow
                Set oPara = oChar.Paragraphs(1).Range
                If Not oChar.Start = oPara.Start Then
                    Selection.Text = Chr(32)
                    Selection.Range.HighlightColorIndex = wdYellow
                    Selection.Collapse 0
                End If
                Selection.InsertSymbol Font:=ReplaceFont, _
                                       CharacterNumber:=ReplaceChar, Unicode:=True
                Selection.Collapse (0)
                Selection.Text = Chr(32)
                Selection.Range.HighlightColorIndex = wdYellow
                Selection.Collapse 0
            Else
                Selection.Collapse 0
            End If
        Loop
    End With
    OriginalRange.Select
lbl_Exit:
    Set OriginalRange = Nothing
    Exit Sub
End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote