我有Microsoft Word中,我试图提高VBA宏。
宏的目的是大胆和斜体文档中的匹配在文档的第一个表中的搜索词的所有单词。
问题是搜索项包含通配符其中有以下几种:
连字符“ - ”:字母之间为空格或一个周期一个通配符
星号“&”:(该网站是不是让我把星号,因为这是斜体降价,所以我会在&符号,而不是让过滤器周围)通配符用于任何数量的字符之初一个字或在末端。 虽然不像一般的编程语言,当它在单词中间使用它需要用连字符相结合,是一个范围的字符的通配符。 例如,“日与-e”便拿起“有”,而“日&E”不会。
问号“?”:通配符单个字符
我在做什么,到目前为止只是测试了这些字符,如果存在的话,我不是垂耳他们关中星号的情况下,还是我提醒他们必须手动搜索这个词的用户。 不理想:-P
我曾尝试在VBA中.MatchWildcard财产,但还没有得到它的工作。 我有什么做的替换文本,而不是搜索文本的感觉。
一个工作宏将采取以下作为其输入(在第一行被有意忽略,第二列是一个与目标搜索字词):
所有在第二列的表想象一下这样的(这里允许HTML不允许TR和TD等)
第一行:字
第二行:搜索
第三排:earch1
第四行:搜索2&
第五行:S-earch3
第六行:?s arch4
第七行:S&-ch5
它会搜索文档,并与像这样粗斜体内容替换:
搜索搜索1搜索2 Search3 Search4 Search5
注:Search3也可以拿起S.earch3与Search3取代
正如人们可能会认为搜索词通常不会被旁边的对方 - 宏应该找到所有实例。
我将我的包括企图,但非功能代码以及之后的第一个工作日宏。
对于工作的宏的代码将在引擎收录从今天一个月,这是09年9月17日,在下面的网址 。
再次感谢您的任何想法,并帮助你可能必须提供的!
萨拉
工作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