Thread: [Solved] Help, please, with macro bug
View Single Post
 
Old 02-18-2022, 02:18 PM
Ulodesk Ulodesk is offline Windows 10 Office 2016
Word 2013 Expert Cert
 
Join Date: Sep 2009
Location: Virginia
Posts: 872
Ulodesk is on a distinguished road
Default Help, please, with macro bug

Back once again. I can't this macro to run. It's to delete any spaces on either side of an em dash in a document. Seems simple enough. I started with a recorded one, then made alterations as I saw it wasn't working. It chokes on the line in red; no clue why, since I don't know VBA.

Thanks.

Code:
Sub Ems()
'
' Ems Macro
'
'
    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
    With Selection.Find
        .Text = "(Chr(32)^+)"
        .Replacement.Text = "^+"
        .Forward = True
        .Wrap = wdFindAsk
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchAllWordForms = False
        .MatchSoundsLike = False
        .MatchWildcards = True
    End With
    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
    With Selection.Find
        .Text = "(^+Chr(32))"
        .Replacement.Text = "^+"
        .Forward = True
        .Wrap = wdFindAsk
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchAllWordForms = False
        .MatchSoundsLike = False
        .MatchWildcards = True
    End With
    Selection.Find.Execute Replace:=wdReplaceAll
    Selection.MoveLeft Unit:=wdCharacter, Count:=1
    Selection.Delete Unit:=wdCharacter, Count:=1
    ActiveWindow.Close
    Application.Quit
End Sub
Reply With Quote