我正在尝试搜索单词的某个部分,然后替换整个单词。
想象的例子:
该文件包含两个词:“何时、何地”
我想搜索“wh”,然后将整个单词更改为空格或将其删除。
真实例子:
%%超链接%%312
%%超链接%2210
我想搜索任何包含%%超链接%%的句子或单词
然后替换整个内容,例如替换或删除“%%hyperlink%%312 %%hyperlink%2210”
AutoExec Hyper
Dim rng As Word.Range
Dim SearchString As String
Set rng = ActiveDocument.Content
SearchString = "sa"
With rng.Find
Do While .Execute(findText:=SearchString, Forward:=True) = True
rng.MoveEndUntil (" ")
Selection.Expand Unit:=wdWord
MsgBox rng.Text
rng.Collapse wdCollapseEnd
Loop
End With
End Sub
您应该学习如何在查找/替换中使用通配符。例如,要删除所有 %%hyperlink%% 字符串:
Sub Demo()
Application.ScreenUpdating = False
With ActiveDocument.Range.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = " %%hyperlink%%[! ]@>"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchWildcards = True
.Execute Replace:=wdReplaceAll
End With
Application.ScreenUpdating = True
End Sub