View Single Post
 
Old 03-19-2022, 02:32 AM
gmayor's Avatar
gmayor gmayor is offline Windows 10 Office 2019
Expert
 
Join Date: Aug 2014
Posts: 4,138
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

This is easier said than done as the 'x' is a symbol character and the '÷' sign cannot be used in a wildcard search string, so they have to be processed separately.

The following, however, works in your test document. See also Replace using wildcards

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


Sub Macro1()
    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
            .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.Collapse 0
        Loop
    End With

    Call ReplaceSymbol(FindChar:=ChrW(-3916), _
                       FindFont:="Symbol", _
                       ReplaceChar:=-3916, _
                       ReplaceFont:="Symbol")
lbl_Exit:
    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
                Set oPara = oChar.Paragraphs(1).Range
                If Not oChar.Start = oPara.Start Then
                    Selection.Text = Chr(32)
                    Selection.Collapse 0
                End If
                Selection.InsertSymbol Font:=ReplaceFont, _
                                       CharacterNumber:=ReplaceChar, Unicode:=True
                Selection.Collapse (0)
                Selection.Text = Chr(32)
                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