0
votes

long story short I'm using a script that highlights words that use a certain format and then exporting them to excel. I can't seem to use Word's built in wildcard system to get the results I'm looking for (I'm not very familiar with it), but I am close!

I'm looking to extract definitions that fall in a certain format inline:

This is the sample text (the "sample") for you ("helpful person" or "HP") to use, this "should be" possible.

My string should capture sample, helpful person, and HP above (but not "should be"). Which is to say all text in quotes between parenthesis.

Currently I can manage to pull everything between smart or straight quotes with this:

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

[the char numbers are for angled quotes]

This of course returns strings that are in quotes even without those quotes being nested in parenthesis.

Can anyone get me on the right track? Thanks so much!

Note that the rest of the script is using MSwords wildcard system, not regex, so changing to regex is out :(.

The full script here just highlights the words that match the string:

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

Thanks again.

2
So you have a macro you're running in Word? You should share the code so we can understand what you're really doing.n8.
Excellent point. Adding it to the OP now.clearlynotstefan
Everything works, I get the highlights and later the exports I want. The only problem I'm having is writing the wildcard string to capture all words within quotes that are within parentheses. So far I can return everything in quotes, or I can return everything within parenthesis, but not both!clearlynotstefan

2 Answers

0
votes

The following works for me. But I don't believe it can be done with a "simple" Find/Replace. What I had to do was locate each instance, then "walk" the range backwards to see if I can find an opening parenthesis, then forwards to find a closing parenthesis - always checking that no intervening closing resp. opening parenthesis is present. Only then can the highlighting be applied.

In order to do this, three separate Range objects are required: one for the search, one for the found range and one to test going 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
0
votes

Maybe run your function twice in your sub:

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

I don't see how any of this exports to Excel though