使用宏反转文档中的数字

Invert numbers in document using macro

我想写一个宏来查找数字并在文档中反转它们。
例如转换:123456654321
我写了这个宏,但它不起作用:

Function inverter(Numbers)
Dim Num As Long
Dim inverted As Long
Num = Numbers
Do Until Num >= 1
    remainder = Num Mod 10
    Num = Num / 10
    inverted = inverted * 10 + remainder
Loop
inverter = inverted
End Function
Sub invert()
Dim a As Long
    Selection.GoTo What = wdGoToLine, Which = wdGoToFirst
    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
    With Selection.Find
           .Text = "([!0-9])([0-9]@)([!0-9])"
           .Replacement.Text = "" & a="" inverter(a) & ""
           .Forward = True
           .Wrap = wdFindContinue
           .Format = False
           .MatchCase = False
           .MatchWholeWord = False
           .MatchKashida = False
           .MatchDiacritics = False
           .MatchAlefHamza = False
           .MatchControl = False
           .MatchWildcards = True
           .MatchSoundsLike = False
           .MatchAllWordForms = False
           .Execute Replace:=wdReplaceAll
    End With
End Sub

当我编译它时 return 这个错误:

---------------------------
Microsoft Visual Basic for Applications
---------------------------
Compile error:

Expected: end of statement
---------------------------
OK   Help   
---------------------------

这条线对我来说没有意义

.Replacement.Text = "" & a="" inverter(a) & ""

好像是赋值、逻辑求值和"error"

的组合

我找不到您需要的批量替换解决方案;所以我的建议是:

我将您的 Invertor 改为使用字符串,因为您的 Invertor 将 return 1 用于 1000 而不是 0001 (可能这在 Word RegEx 中没有考虑)

Function Inverter2(nb As String) As String
    Dim bn As String
    For i = 1 To Len(nb)
        bn = Mid(nb, i, 1) & bn
    Next i
    Inverter2 = bn
End Function

这是您的 invert 更改

Sub invert()
Dim a As Long
    Selection.GoTo What = wdGoToLine, Which = wdGoToFirst
    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
    With Selection.Find
           .Text = "([!0-9])([0-9]@)([!0-9])"
           '.Replacement.Text = "" & a="" inverter(a) & ""
           .Forward = True
           '.Wrap = wdFindContinue
           .Format = False
           .MatchCase = False
           .MatchWholeWord = False
           .MatchKashida = False
           .MatchDiacritics = False
           .MatchAlefHamza = False
           .MatchControl = False
           .MatchWildcards = True
           .MatchSoundsLike = False
           .MatchAllWordForms = False
           '.Execute Replace:=wdReplaceOne
           While .Execute
               Selection.Text = Inverter2(Selection.Text)
           Wend
    End With
    Selection.GoTo What = wdGoToLine, Which = wdGoToFirst
End Sub

我删除了 .Wrap = wdFindContinue 因为文本会被反转两次所以保持原样

我也去掉了你的替换,一个一个进行。