将两个单独的查找和格式化宏合并为一个
Combine two separate Find and Format Macros into one
目前,我正在使用两个单独的宏来查找 Word 文档中的特定文本并执行某些格式化命令(每次搜索都不同)。为了方便起见,我想以某种方式将两者合二为一。
这是第一个:
With Selection.Find
.Text = "potatoes"
.Forward = True
.Wrap = wdFindContinue
.MatchWildcards = True
End With
'
Selection.Find.Execute
Selection.MoveRight Unit:=wdCharacter, Count:=3
Selection.InsertParagraphAfter
Repeat (3)
第二个:
With Selection.Find
.Text = "tomatoes"
.Forward = True
.Wrap = wdFindContinue
.MatchWildcards = True
End With
'
Selection.Find.Execute
Selection.InsertParagraphAfter
Repeat (2)
在我的例子中,只有一个搜索结果是正确的,并且 return 结果。
所以这个想法是这样的...
- 如果您找到“potatoes”,请执行第一种情况格式化,不要搜索“tomatoes”。
- 如果您找到“tomatoes”,请执行第二种大小写格式,不要搜索“potatoes”。
- 或者...如果您没有找到“potatoes”,请搜索“tomatoes”并进行第二种情况格式化。
使用:
Dim fnd As Boolean
fnd = False
Selection.HomeKey wdStory
Selection.Find.ClearFormatting
With Selection.Find
Do While .Execute(FindText:="potatoes", Forward:=True, _
MatchWildcards:=False, Wrap:=wdFindStop, MatchCase:=True) = True
fnd = True
Selection.MoveRight Unit:=wdCharacter, Count:=3
Selection.InsertAfter vbCr
Selection.Collapse wdCollapseEnd
Loop
End With
If fnd = False Then
Selection.HomeKey wdStory
Selection.Find.ClearFormatting
With Selection.Find
Do While .Execute(FindText:="tomatoes", Forward:=True, _
MatchWildcards:=False, Wrap:=wdFindStop, MatchCase:=True) = True
Selection.InsertAfter vbCr
Selection.Collapse wdCollapseEnd
Loop
End With
End If
目前,我正在使用两个单独的宏来查找 Word 文档中的特定文本并执行某些格式化命令(每次搜索都不同)。为了方便起见,我想以某种方式将两者合二为一。
这是第一个:
With Selection.Find
.Text = "potatoes"
.Forward = True
.Wrap = wdFindContinue
.MatchWildcards = True
End With
'
Selection.Find.Execute
Selection.MoveRight Unit:=wdCharacter, Count:=3
Selection.InsertParagraphAfter
Repeat (3)
第二个:
With Selection.Find
.Text = "tomatoes"
.Forward = True
.Wrap = wdFindContinue
.MatchWildcards = True
End With
'
Selection.Find.Execute
Selection.InsertParagraphAfter
Repeat (2)
在我的例子中,只有一个搜索结果是正确的,并且 return 结果。
所以这个想法是这样的...
- 如果您找到“potatoes”,请执行第一种情况格式化,不要搜索“tomatoes”。
- 如果您找到“tomatoes”,请执行第二种大小写格式,不要搜索“potatoes”。
- 或者...如果您没有找到“potatoes”,请搜索“tomatoes”并进行第二种情况格式化。
使用:
Dim fnd As Boolean
fnd = False
Selection.HomeKey wdStory
Selection.Find.ClearFormatting
With Selection.Find
Do While .Execute(FindText:="potatoes", Forward:=True, _
MatchWildcards:=False, Wrap:=wdFindStop, MatchCase:=True) = True
fnd = True
Selection.MoveRight Unit:=wdCharacter, Count:=3
Selection.InsertAfter vbCr
Selection.Collapse wdCollapseEnd
Loop
End With
If fnd = False Then
Selection.HomeKey wdStory
Selection.Find.ClearFormatting
With Selection.Find
Do While .Execute(FindText:="tomatoes", Forward:=True, _
MatchWildcards:=False, Wrap:=wdFindStop, MatchCase:=True) = True
Selection.InsertAfter vbCr
Selection.Collapse wdCollapseEnd
Loop
End With
End If