Using Selection.Find.Text with Wildcards in Word 2007 VBA

I am writing a macro to search a document for all instances of [TBR-XXX] and [TBD-YYY] ( X's and Y's are numbers, like 001, 002, etc., and yes, the square brackets are part of the item I'm looking for ). Once I find the instances, I put them into a table along with the corresponding section number from whence they came. 

 

The brackets and the hyphen are throwing me as they are special characters.  I've tried the following ( snippet of code ):

 

Selection.GoTo What:=wdGoToBookmark, Name:="bmStartOfBody"  ' Start at the beginning of the main body section


Selection.Collapse


    With Selection.Find
        .Text = "TB??^#^#^#"
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindStop
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
   
    Selection.Find.Execute

 

My document starts with the word, "The" and of course, the .Text search string above picks up on the capital T and thinks that is the first TBR/TBD item. From there, the whole thing breaks down. 

 

I've also tried the following for the .Text string:

 

"[TB??^#^#^#]"    (same result)

"\[TB??^#^#^#\]" (Same result)

"[[]TB??^#^#^#[]]" (Didn't work)

 

But when I used, "[TBR?^#^#^#]"  I found all the TBR values.

 

I could simply search for the TBR values and then the TBD values, but I'm just curious about the correct way to do the search using a string with the wildcards in there. Hopefully someone can shed some light on this for me. Thanks! By the way, I tried using the ^# with the "Find" feature in word with the "Use Wildcards" selected just to see if I could learn anything from the built in feature, but it said it can't use the ^# wildcard in a find using the built in feature. Ugh.


 

Answer
Answer

Thanks to all who provided answers. I figured it out after two days of trial and error. Here is my inelegant solution. I am certain there is a better way, but at least this works.

 

Since the numbers were part of a sequence rather than just three character digits, there was apparently no way to make the Find.Text with wildcards to work. So, here's what did work:

 

----snipped out of my previous post-------

While .Execute

searchRange.SetRange Start:=searchRange.Start, End:=searchRange.End + 3

strValue = searchRange.Text & searchRange.Fields.Item(1).Result.Text & "]"

etc.

 

------End Snip

 

This successfully parses the document and finds all the [TBR- and [TBD- values, appends the sequence numbers and the close bracket and places it in the correct column in the table.

 

Ugh.

 

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

 
 

Question Info


Last updated October 5, 2021 Views 6,591 Applies to: