我有一个我正在尝试改进的 Microsoft Word 的 VBA 宏。
宏的目的是将文档中与文档第一个表中的搜索词匹配的所有单词加粗和斜体。
问题是搜索词包括以下通配符:
连字符“-”:字母之间的通配符,用于空格或句点
星号“&”:(该网站不允许我输入星号,因为这是斜体的降价,所以我将输入 & 符号来绕过过滤器)在开头的任意数量字符的通配符一个词或在最后。然而,与普通编程语言不同的是,当它用在单词的中间时,它需要与连字符组合成为一系列字符的通配符。例如,“th&-e”会选择“there”,而“th&e”则不会。
问号“?”:单个字符的通配符
到目前为止,我所做的只是测试这些字符,如果它们存在,我要么在星号的情况下将它们去掉,要么提醒用户他们必须手动搜索单词。不理想:-P
我已经尝试过 VBA 中的 .MatchWildcard 属性,但还没有让它工作。我觉得它与替换文本有关,而不是搜索文本。
工作宏将以下内容作为其输入(第一行被有意忽略,第二列是带有目标搜索词的行):
想象一下这在第二列的表格中(因为此处允许的 html 不允许 tr 和 td 等)
第一行:Word
第二行:Search
第三行:&earch1
第四行:Search2&
第五行:S-earch3
第六行:S?arch4
第七行:S&-ch5
它将搜索文档并替换为粗体和斜体内容,如下所示:
搜索 搜索1 搜索2 搜索3 搜索4 搜索5
注意:S-earch3 也可以选择 S.search3 并替换为 Search3
正如人们可能假设的那样,搜索词通常不会彼此相邻 - 宏应该找到所有实例。
在第一个工作宏之后,我也会包含我尝试过但没有功能的代码。
从今天(2009 年 9 月 17 日)开始,工作宏的代码将在 pastebin 上保存一个月,网址如下。
再次感谢您提供的任何想法和帮助!
萨拉
工作 VBA 宏:
Sub AllBold()
Dim tblOne As Table
Dim celTable As Cell
Dim rngTable As Range
Dim intCount As Integer
Dim celColl As Cells
Dim i As Integer
Dim rngLen As Integer
Dim bolWild As Boolean
Dim strWild As String
Set tblOne = ActiveDocument.Tables(1)
intCount = tblOne.Columns(2).Cells.Count
Set celColl = tblOne.Columns(2).Cells
strWild = ""
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)
rngLen = Len(rngTable.Text)
bolWild = False
If (Mid(rngTable.Text, rngLen, 1) = "&") Then 'remember to replace & with asterisk!'
rngTable.SetRange Start:=rngTable.Start, End:=rngTable.End - 1
End If
If (Mid(rngTable.Text, 1, 1) = "&") Then 'remember to replace & with asterisk!'
rngTable.SetRange Start:=rngTable.Start + 1, End:=rngTable.End
End If
If InStr(1, rngTable.Text, "-", vbTextCompare) > 0 Then
strWild = strWild + rngTable.Text + Chr$(13)
bolWild = True
End If
If InStr(1, rngTable.Text, "?", vbTextCompare) > 0 Then
strWild = strWild + rngTable.Text + Chr$(13)
bolWild = True
End If
If (bolWild = False) Then
Dim oRng As Word.Range
Set oRng = ActiveDocument.Range
With oRng.Find
.ClearFormatting
.Text = rngTable.Text
With .Replacement
.Text = rngTable.Text
.Font.Bold = True
.Font.Italic = True
End With
.Execute Replace:=wdReplaceAll
End With
End If
Next
If bolWild = True Then
MsgBox ("Please search the following strings with - or ? manually:" + Chr$(13) + strWild)
End If
End Sub
尝试的非功能性 VBA 宏:
Sub AllBoldWildcard()
Dim tblOne As Table
Dim celTable As Cell
Dim rngTable As Range
Dim intCount As Integer
Dim celColl As Cells
Dim i As Integer
Dim rngLen As Integer
Dim bolWild As Boolean
Dim strWild As String
Dim strWildcard As String
Set tblOne = ActiveDocument.Tables(1)
intCount = tblOne.Columns(2).Cells.Count
Set celColl = tblOne.Columns(2).Cells
strWild = ""
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)
rngLen = Len(rngTable.Text)
bolWild = False
If (Mid(rngTable.Text, 1, 1) = "&") Then 'remember to replace & with asterisk!'
rngTable.SetRange Start:=rngTable.Start + 1, End:=rngTable.End
End If
If InStr(1, rngTable.Text, "&", vbTextCompare) > 0 Then 'remember to replace & with asterisk!'
strWildcard = rngTable.Text
rngTable.Text = Replace(rngTable.Text, "&", "", 1) 'remember to replace & with asterisk!'
bolWild = True
End If
If InStr(1, rngTable.Text, "-", vbTextCompare) > 0 Then
strWildcard = Replace(rngTable.Text, "-", "[.-]", 1)
bolWild = True
End If
If InStr(1, rngTable.Text, "?", vbTextCompare) > 0 Then
strWild = strWild + rngTable.Text + Chr$(13)
strWildcard = Replace(rngTable.Text, "?", "_", 1)
bolWild = True
End If
If (bolWild = False) Then
Dim oRng As Word.Range
Set oRng = ActiveDocument.Range
With oRng.Find
.ClearFormatting
.Text = strWildcard
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchFuzzy = False
.MatchWildcards = True
With .Replacement
.Text = rngTable.Text
.Font.Bold = True
.Font.Italic = True
End With
.Execute Replace:=wdReplaceAll
End With
End If
Next
' If bolWild = True Then'
' MsgBox ("Please search the following strings with - or ? manually:" + Chr$(13) + strWild)'
' End If'
End Sub