我正在尝试构建一个宏(作为菜鸟)来查找某些单词,然后更改该单词的格式(即使其变为粗体或斜体)
此代码'有点作品。'它会找到一些单词并改变它们但不会改变其他单词。奇怪的是它是有效的,直到我添加另一个子,然后它停止格式化一些单词,同时格式化其他。例程永远不会中断并运行直到结束而没有错误。
任何人都可以教导为什么会这样,我做错了什么?我不是程序员。感谢
Sub Macro2()
'
' Macro2 Macro
'
'
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "Printer"
.Replacement.Text = ""
.Replacement.Font.bold = True
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
With Selection.Find
.Text = "Parameter Values"
.Replacement.Text = ""
.Replacement.Font.bold = True
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
With Selection.Find
.Text = "Use All Applicants Indicator"
.Replacement.Text = ""
.Replacement.Font.bold = True
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
With Selection.Find
.Text = "Next Section"
.Replacement.Text = ""
.Replacement.Font.bold = True
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
答案 0 :(得分:1)
Selection
是包含当前所选文档部分的对象。
Find
是定义Selection
对象的Find
对象的属性(同名,但一个是属性,另一个是对象)。 Find
对象具有Text
,Forward
,Wrap
等属性,并且具有ClearFormatting
或Execute
等方法。所有这一切都可以在MSDN库中阅读。
现在,当您定义Find
对象时,您正在描述要查找的内容。使用Execute
命令开始查找它。您的代码在某些地方缺少此命令。
搜索仅限于Selection
。如果您没有选择任何内容,Word将假定您要搜索整个文档。但是Selection.Find
会更改选择以突出显示找到的项目。因此,如果您想继续搜索整个文档,则需要在每次搜索后重置选择,例如Activedocument.Content.Select
。
简而言之,如果您在每次使用后清除Find
对象,请在每次重复使用之前设置新描述,为每次搜索定义Selection
对象,不要忘记{{1每个单独的搜索,你的代码应该按照你打算工作的方式工作。
答案 1 :(得分:1)
我会将搜索宏编程为一个单独的子,就像这样。
Private Sub FindAndReplace(ByVal Txt As String, _
Optional ByVal NewTxt As String, _
Optional ByVal Fmt As Boolean = False, _
Optional ByVal BldFmt As Boolean = False)
With ActiveDocument.Content
With .Find
.ClearFormatting
.Text = Txt
.Format = Fmt Or BldFmt
With .Replacement
.ClearFormatting
.Text = NewTxt
.Font.Bold = BldFmt
End With
.Forward = True
.Wrap = wdFindContinue
.MatchCase = False
.MatchWholeWord = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
.Find.Execute Replace:=wdReplaceAll
End With
End Sub
您可以使用但不必使用的所有可选参数。 然后我会用不同的参数反复调用sub,也许是这样的: -
Sub MakeReplacements1()
FindAndReplace "Printer", BldFmt:=True
FindAndReplace "Parameter values", BldFmt:=True
FindAndReplace "Use All Applicants Indicator", BldFmt:=True
FindAndReplace "Next Section", BldFmt:=True
End Sub
甚至是这样的: -
Sub MakeReplacements2()
Dim Fnd() As String
Dim i As Long
Fnd = Split("Printer|Parameter values|Use All Applicants Indicator|Next Section", "|")
For i = 0 To UBound(Fnd)
FindAndReplace Fnd(i), BldFmt:=True
Next i
End Sub