Microsoft Word VBA 通配符字符串
Microsoft Word VBA Wildcard String
长话短说我正在使用一个脚本来突出显示使用特定格式的单词,然后将它们导出到 excel。我似乎无法使用 Word 内置的通配符系统来获得我正在寻找的结果(我不是很熟悉),但我很接近了!
我正在寻找符合特定内联格式的定义:
这是供您("helpful person" 或 "HP")使用的示例文本("sample"),这 "should be" 可能。
我的字符串应该包含示例、帮助者和 HP 以上(但不是 "should be")。也就是说,所有文本都在括号之间用引号引起来。
目前我可以设法在智能引号或直引号之间拉动一切:
(" & ChrW(8220) & ")(*)(" & ChrW(8221) & ")
[字符编号用于斜引号]
这当然是 returns 引号中的字符串,即使这些引号没有嵌套在括号中。
任何人都可以让我走上正轨吗?非常感谢!
请注意,脚本的其余部分使用的是 MSwords 通配符系统,不是 正则表达式,因此更改为正则表达式是错误的:(。
此处的完整脚本仅突出显示与字符串匹配的单词:
Sub findfunction()
If (findHL(ActiveDocument.Content, "(" & ChrW(8220) & ")(*)(" & ChrW(8221) & ")")) = True Then _
MsgBox "Done", vbInformation + vbOKOnly, "Result"
End Sub
Function findHL(r As Range, s As String) As Boolean
Options.DefaultHighlightColorIndex = wdYellow
r.Find.Replacement.Highlight = True
r.Find.Execute FindText:=s, MatchWildcards:=True, _
Wrap:=wdFindContinue, Format:=True, _
replacewith:="", Replace:=wdReplaceAll
findHL = True
End Function
再次感谢。
也许 运行 你的函数在你的子程序中有两次:
Sub findfunction()
found = False
If findHL(ActiveDocument.Content, [wildcard string for condition A]) = True Then found = True
If findHL(ActiveDocument.Content, [wildcard string for condition B]) = True Then found = True
If found = True Then MsgBox "Done", vbInformation + vbOKOnly, "Result"
End Sub
虽然
,但我看不出其中的任何内容如何导出到 Excel
以下适合我。但我不相信它可以用 "simple" Find/Replace 来完成。我必须做的是找到每个实例,然后向后 "walk" 范围以查看是否可以找到一个左括号,然后向前查找一个右括号 - 始终检查没有中间的右括号。存在左括号。只有这样才能应用高亮。
为此,需要三个独立的 Range 对象:一个用于搜索,一个用于找到范围,一个用于测试 backwards/forwards。
Sub findfunction()
If (findHL(ActiveDocument.content, _
"(" & ChrW(8220) & ")(*)(" & ChrW(8221) & ")")) = True Then
MsgBox "Done", vbInformation + vbOKOnly, "Result"
End If
End Sub
Function findHL(r As Range, s As String) As Boolean
Dim success As Boolean
Dim foundRange As word.Range
Dim testRange As word.Range
Dim moved As Long
success = False
Set foundRange = r.Duplicate
Options.DefaultHighlightColorIndex = wdYellow
r.Find.Replacement.Highlight = True
Do
success = foundRange.Find.Execute(findText:=s, MatchWildcards:=True, _
wrap:=wdFindStop, Format:=True)
If success Then
r.Start = foundRange.End
Set testRange = foundRange.Duplicate
moved = testRange.MoveStartUntil("()", wdBackward)
If moved < 0 Then
testRange.MoveStart wdCharacter, -1
If Left(testRange, 1) = "(" Then
moved = testRange.MoveEndUntil(")", wdForward)
If moved > 0 Then
testRange.MoveEnd wdCharacter, 1
If Right(testRange, 1) = ")" Then
foundRange.HighlightColorIndex = wdYellow
End If
End If
End If
End If
End If
Loop While success = True
findHL = True
End Function
长话短说我正在使用一个脚本来突出显示使用特定格式的单词,然后将它们导出到 excel。我似乎无法使用 Word 内置的通配符系统来获得我正在寻找的结果(我不是很熟悉),但我很接近了!
我正在寻找符合特定内联格式的定义:
这是供您("helpful person" 或 "HP")使用的示例文本("sample"),这 "should be" 可能。
我的字符串应该包含示例、帮助者和 HP 以上(但不是 "should be")。也就是说,所有文本都在括号之间用引号引起来。
目前我可以设法在智能引号或直引号之间拉动一切:
(" & ChrW(8220) & ")(*)(" & ChrW(8221) & ")
[字符编号用于斜引号]
这当然是 returns 引号中的字符串,即使这些引号没有嵌套在括号中。
任何人都可以让我走上正轨吗?非常感谢!
请注意,脚本的其余部分使用的是 MSwords 通配符系统,不是 正则表达式,因此更改为正则表达式是错误的:(。
此处的完整脚本仅突出显示与字符串匹配的单词:
Sub findfunction()
If (findHL(ActiveDocument.Content, "(" & ChrW(8220) & ")(*)(" & ChrW(8221) & ")")) = True Then _
MsgBox "Done", vbInformation + vbOKOnly, "Result"
End Sub
Function findHL(r As Range, s As String) As Boolean
Options.DefaultHighlightColorIndex = wdYellow
r.Find.Replacement.Highlight = True
r.Find.Execute FindText:=s, MatchWildcards:=True, _
Wrap:=wdFindContinue, Format:=True, _
replacewith:="", Replace:=wdReplaceAll
findHL = True
End Function
再次感谢。
也许 运行 你的函数在你的子程序中有两次:
Sub findfunction()
found = False
If findHL(ActiveDocument.Content, [wildcard string for condition A]) = True Then found = True
If findHL(ActiveDocument.Content, [wildcard string for condition B]) = True Then found = True
If found = True Then MsgBox "Done", vbInformation + vbOKOnly, "Result"
End Sub
虽然
,但我看不出其中的任何内容如何导出到 Excel以下适合我。但我不相信它可以用 "simple" Find/Replace 来完成。我必须做的是找到每个实例,然后向后 "walk" 范围以查看是否可以找到一个左括号,然后向前查找一个右括号 - 始终检查没有中间的右括号。存在左括号。只有这样才能应用高亮。
为此,需要三个独立的 Range 对象:一个用于搜索,一个用于找到范围,一个用于测试 backwards/forwards。
Sub findfunction()
If (findHL(ActiveDocument.content, _
"(" & ChrW(8220) & ")(*)(" & ChrW(8221) & ")")) = True Then
MsgBox "Done", vbInformation + vbOKOnly, "Result"
End If
End Sub
Function findHL(r As Range, s As String) As Boolean
Dim success As Boolean
Dim foundRange As word.Range
Dim testRange As word.Range
Dim moved As Long
success = False
Set foundRange = r.Duplicate
Options.DefaultHighlightColorIndex = wdYellow
r.Find.Replacement.Highlight = True
Do
success = foundRange.Find.Execute(findText:=s, MatchWildcards:=True, _
wrap:=wdFindStop, Format:=True)
If success Then
r.Start = foundRange.End
Set testRange = foundRange.Duplicate
moved = testRange.MoveStartUntil("()", wdBackward)
If moved < 0 Then
testRange.MoveStart wdCharacter, -1
If Left(testRange, 1) = "(" Then
moved = testRange.MoveEndUntil(")", wdForward)
If moved > 0 Then
testRange.MoveEnd wdCharacter, 1
If Right(testRange, 1) = ")" Then
foundRange.HighlightColorIndex = wdYellow
End If
End If
End If
End If
End If
Loop While success = True
findHL = True
End Function