我编写了一个宏,用于替换三个空格中的第一个空格的格式,然后将某个字符串替换为蓝色字体的数字,另一个宏用于替换括号之间的空格,然后是某个字符串。
你知道如何优化这两个程序(我使用 MS-Word 的搜索和替换对话的通配符,但猜想在 VBA 中使用它是相当尴尬的......)?
我的宏:
Sub replace_3spaces()
Dim str_after As String
Dim re_number As Integer
str_after = "normal"
re_number = "1"
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "([^s]{3})" & "(" & str_after & ")"
.Replacement.Text = "§§§\2"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
End With
Selection.Find.Execute Replace:=wdReplaceAll
Selection.Find.ClearFormatting
Selection.Find.Replacement.Font.ColorIndex = wdBlue
With Selection.Find
.Text = "§§§"
.Replacement.Text = re_number & " "
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
End With
Selection.Find.Execute Replace:=wdReplaceAll
End Sub