簡體   English   中英

Microsoft Word VBA通​​配符字符串

[英]Microsoft Word VBA Wildcard String

長話短說,我正在使用一個腳本來突出顯示使用某種格式的單詞,然后將其導出到excel。 我似乎無法使用Word的內置通配符系統來獲得所需的結果(我對此不太熟悉),但是我很接近!

我正在尋找提取特定格式的內聯定義:

這是供您(“幫助人員”或“ HP”)使用的示例文本(“示例”),“應該”是可能的。

我的字符串應該包含示例,有用的人和上面的HP(但不能“應該”)。 也就是說,所有文字之間用引號引起來。

目前,我可以使用以下方法在智能引號或直引號之間取得一切:

    (" & ChrW(8220) & ")(*)(" & ChrW(8221) & ") 

[字符號用於斜引號]

當然,這會返回用引號引起來的字符串,即使這些引號沒有嵌套在括號中也是如此。

誰能讓我走上正確的軌道? 非常感謝!

請注意,腳本的其余部分使用的是MSwords通配符系統, 而不是 regex,因此更改為regex是:(。

這里的完整腳本僅突出顯示與字符串匹配的單詞:

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

以下對我有用。 但是我不相信可以通過“簡單”的查找/替換來完成。 我要做的是找到每個實例,然后向后“遍歷”范圍以查看是否可以找到一個圓括號,然后向前找到一個圓括號-始終檢查是否有中間的圓括號。 存在左括號。 只有這樣,才可以應用突出顯示。

為此,需要三個單獨的Range對象:一個用於搜索,一個用於找到的范圍以及一個用於測試向后/向前的對象。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM