目前,我正在使用兩個單獨的宏在 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)
在我的情況下,只有一個搜索是真實的并且會回傳一個結果。
所以這個想法是這樣的......
- 如果您找到“potatoes”,請進行第一種情況格式化,不要搜索“tomatoes”。
- 如果您找到“tomatoes”,請進行第二種格式的格式化并且不要搜索“potatoes”。
- 或者...如果您沒有找到“土豆”,請搜索“西紅柿”并進行第二種格式的格式化。
uj5u.com熱心網友回復:
利用:
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
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/410928.html
標籤:
