View Single Post
 
Old 10-21-2023, 02:49 AM
vivka vivka is offline Windows 7 64bit Office 2016
Expert
 
Join Date: Jul 2023
Posts: 293
vivka is on a distinguished road
Default

Hi, Robinew! The problem with chr(146) is that it can't be inserted (replace any char) in a position preceded by space (instead, chr(145) is inserted, that's why the 'Save Changes?' message pops up). For small docs your code is OK. But if you want to gain milliseconds, I offer a tricky workaround: first, replace all instances of chr(145) and the digit after it with something quite rare (in the code below it's a hash) + chr(146) + that digit, then delete hashes (replace them with nothing). There'll be only two code executions however big is a document instead of as many executions as many instances of chr(145) are in the doc using your code. I hope I have formulated my idea clearly.

Code:
Sub SingleBefore_Digit()
'Replace Chr(145) with Chr(146) before a digit (as in ’95)

Dim oRng As range, iType As Integer
  
    For iType = 1 To 2
        Set oRng = ActiveDocument.StoryRanges(1)
        Set oRng = selection.range
        With oRng.Find
            .ClearFormatting
            .Replacement.ClearFormatting
            .text = Chr(145) & "([0-9])"
            .Font.Superscript = False
            .Replacement.text = "#" & Chr(146) & "\1"
            .MatchWildcards = True
            .Execute Replace:=wdReplaceAll
            .text = "#"
            .Replacement.text = ""
            .Execute Replace:=wdReplaceAll
        End With
   Next iType
End Sub
Reply With Quote