如何在Microsoft Word的Visual Basic宏中找到部分单词?我想查找所有出现的单词片段,而不是整个单词。换句话说,我希望(1)前没有空格,(2)后只有空格或标点符号的字符串。
例如搜索“ able”,将返回可靠但不可靠的信息。
我想查找并突出显示所有出现的内容:要么仅突出显示单词片段,要么突出显示包含单词片段的整个单词。
答案 0 :(得分:0)
根据您的情况,我创建了一个简单的正则表达式来查找相关单词。请尝试以下代码:
Sub RegSearchHL()
Dim myMatches, myMatch
Dim regExp As Object
Set regExp = CreateObject("vbscript.regexp")
With regExp
.Pattern = "[A-Za-z]+able+[\ \.\,\:\?\!\;]" 'please replace your text
.Global = True
End With
Set myMatches = regExp.Execute(ActiveDocument.Content.Text)
For Each match In myMatches
Options.DefaultHighlightColorIndex = wdYellow
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
Selection.Find.Replacement.Highlight = True
With Selection.Find
.Text = match.Value
.Replacement.Text = match.Value
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
Next match
End Sub
此外,您可以将参数传递给RegExp Pattern并更新所需的内容。
谢谢
Yuki