我正在尝试改进Microsoft Word的VBA宏。
宏的目的是加粗和斜体显示文档中与文档第一个表中的搜索词匹配的所有单词。
问题是搜索字词包括以下通配符:
连字符“ - ”:字母之间的空格或句点的通配符
asterisk“&”:(该网站不允许我输入星号,因为这是斜体的降价,所以我会放入&符号而不是绕过过滤器)任意数量的通配符单词开头或结尾处的字符。但是,与普通的编程语言不同,当它在单词的中间使用时,它需要与连字符组合成一系列字符的通配符。例如,“th& -e”会选择“那里”,而“th& e”则不会。
问号“?”:单个字符的通配符
到目前为止我所做的只是测试这些字符,如果它们存在,我或者在星号的情况下将它们丢掉,或者我提醒用户他们必须手动搜索这个字。不理想:-P
我在VBA中尝试过.MatchWildcard属性,但尚未使用它。我觉得它与替换文本有关,而不是搜索文本。
一个工作的宏将把以下内容作为输入(有意忽略第一行,第二列是目标搜索术语的那一行):
想象一下这个在第二列的所有表中(因为这里允许的html不允许tr和td等)
第一行:字
第二行:搜索
第三行:& earch1
第四行:Search2&
第五行:S-earch3
第六排:S?arch4
第七行:S& -ch5
它将搜索文档并替换为粗体和斜体的内容,如下所示:
搜索搜索1搜索2搜索3搜索4搜索
注意:S-earch3也可以选择S.earch3并替换为Search3
正如人们可能认为搜索词通常不会彼此相邻 - 宏应该找到所有实例。
在第一个工作宏之后,我将包括我尝试但无功能的代码。
答案 0 :(得分:1)
也许LIKE声明可以帮助你:
if "My House" like "* House" then
end if
正则表达式: 搜索Search4并将其替换为SEARCH4并使用通配符来实现:
Set objRegEx = CreateObject("vbscript.regexp")
objRegEx.Global = True
objRegEx.IgnoreCase = True
objRegEx.MultiLine = True
'here you can enter your search with wild cards
'mine says "S" followed by any character followed by "arch" followed by 1-n numbers.
objRegEx.Pattern = "S.arch([0-9]+)"
newText = objRegEx.Replace("Test Search4", "SEARCH$1")
MsgBox (newText)
'gives you: Test SEARCH4
有关如何使用这些通配符的更多信息here 一开始可能很难,但我保证你会喜欢它;)
您也可以替换use来搜索字符串:
Dim text As String text =“Hello Search4 search3 sAarch2 search0 search”
Set objRegEx = CreateObject("vbscript.regexp")
objRegEx.Global = True
objRegEx.IgnoreCase = True
objRegEx.MultiLine = True
'here you can enter your search with wild cards
'mine says "S" followed by any character followed by "arch" followed by 1-n numbers.
objRegEx.Pattern = "S.arch[0-9]+"
If (objRegEx.test(text) = True) Then
Dim objMatch As Variant
Set objMatch = objRegEx.Execute(text) ' Execute search.
Dim wordStart As Long
Dim wordEnd As Long
Dim intIndex As Integer
For intIndex = 0 To objMatch.Count - 1
wordStart = objMatch(intIndex).FirstIndex
wordEnd = wordStart + Len(objMatch(intIndex))
MsgBox ("found " & objMatch(intIndex) & " position: " & wordStart & " - " & wordEnd)
Next
End If
变量文本的结果是:
Search4 position: 6 - 13
Search3 position: 14- 21
...
因此,在您的代码中,您将使用
rngTable.Text as text
和
rngTable.SetRange Start:=rngTable.Start + wordStart, End:=rngTable.Start + wordEnd
将是您想要设置粗体的范围。
答案 1 :(得分:1)
Sub AllBold()
Dim tblOne As Table
Dim celTable As Cell
Dim rngTable As Range
Dim intCount As Integer
Dim intMatch As Integer
Dim celColl As Cells
Dim i As Integer
Dim strRegex As String
Dim Match, Matches
Set tblOne = ActiveDocument.Tables(1)
intCount = tblOne.Columns(2).Cells.Count
Set celColl = tblOne.Columns(2).Cells
Set objRegEx = CreateObject("vbscript.regexp")
objRegEx.Global = True
objRegEx.IgnoreCase = True
objRegEx.MultiLine = True
For i = 1 To intCount
If i = 1 Then
i = i + 1
End If
Set celTable = ActiveDocument.Tables(1).Cell(Row:=i, Column:=2)
Set rngTable = ActiveDocument.Range(Start:=celTable.Range.Start, _
End:=celTable.Range.End - 1)
If rngTable.Text <> "" Then
strRegex = rngTable.Text
strRegex = Replace(strRegex, "*-", "[\w]{0,}[^\w]{0,1}[\w]{0,}", 1)
strRegex = Replace(strRegex, "*", "\w+", 1)
strRegex = Replace(strRegex, "-", "[^\w]{0,1}", 1)
strRegex = Replace(strRegex, "?", ".", 1)
objRegEx.Pattern = "\b" + strRegex + "\b"
Dim oRng As Word.Range
Set oRng = ActiveDocument.Range
Set Matches = objRegEx.Execute(ActiveDocument.Range.Text)
intMatch = Matches.Count
If intMatch >= 1 Then
rngTable.Bold = True
For Each Match In Matches
With oRng.Find
.ClearFormatting
.Text = Match.Value
With .Replacement
.Text = Match.Value
.Font.Bold = True
.Font.Italic = True
End With
.Execute Replace:=wdReplaceAll
End With
Next Match
End If
End If
Next i
End Sub