【发布时间】:2012-11-13 19:26:30
【问题描述】:
我编写了一个宏,用于替换三个空格中的第一个的格式,后跟某个字符串,用蓝色字体的数字替换另一个宏,用于替换括号之间的空格,后跟某个字符串。
你知道如何优化这两个过程吗(我使用 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
【问题讨论】:
标签: vba replace ms-word wildcard