Search code examples
htmlvbams-wordstring-search

Find a string that contains a cross reference in Word VBA


The objective on my code is to be able to search for a set of key terms using VBA in order to be able to apply a bold formatting. I am trying to set the code to expect strings of the following formats:

  • Item #
  • Item #@
  • Item #@@
  • Item ##
  • Item ##@
  • Item ##@@

This is there the # sign will represent a number and the @ will represent a lowercase later. I believe that the code that I have written is able to find the generic strings for the entire document, however the #'s and @'s will be cross referenced to some place in the document and it seems that my script is unable to find the strings because of the cross reference.

Note: I put spaces in my HTML tags because I wanted to show what the tags look like before they execute.

The purpose of this code is to create a dynamic document that has Items updated as the document changes, but I am going to be converting the document to HTML and I have JavaScript that looks for the text to create tags and apply a code overlay. I have run into this need to preprocess the Word document to ensure that formatting is consistent regardless of how the bolding was applied. The formatting issue occurs if the Item is bolded then the # is bolded causing the html to look like < b>Item< /b>< b> #< /b> and I have found it very difficult to find these strings with the JavaScript to apply coding.

Sub FindTheText()
Dim test As Variant
Set test = ActiveDocument.Range.Find
With Selection.Find
    .ClearFormatting
    .Text = "Item [0-9]"
    .MatchWildcards = True
    .MatchCase = False
    .MatchWholeWord = True
    .Wrap = wdFindContinue
End With
Selection.Find.Execute
End Sub

This is just a sample code so I would need to create a series of commands in a loop to look for each type of #/@ combination that I expect. That is at least my plan once I can get the basics to work

To demonstrate my required output I am going type an example paragraph and then a sample of bad HTML code and good HTML code.

Sample Paragraph

My sample code should be able to find Item 3aa and apply bolding such that a single bold tag is applied in the html. The Items can vary but I expect that I can see one or two number and possible one or two letters that would vary from Item 1 to Item 1z to 99zz so I need to prepare for all cases.

Bad HTML Output

< p>My sample code should be able to find < b>Item< /b> < b>3aa< /b> and apply bolding such that a single bold tag is applied in the html. The Items can vary but I expect that I can see one or two number and possible one or two letters that would vary from < b>Item < /b>< b>1< /b> to < b>Item< /b> < b>1< /b>< b>z< /b> to < b>Item < /b>< b>99< /b>< b>zz< /b> so I need to prepare for all cases.

Good HTML Output

< p>My sample code should be able to find < b>Item 3aa< /b> and apply bolding such that a single bold tag is applied in the html. The Items can vary but I expect that I can see one or two number and possible one or two letters that would vary from < b>Item 1< /b> to < b>Item 1z< /b> to < b>Item 99zz< /b> so I need to prepare for all cases.< /p>


Solution

  • If you are doing a wildcard search then you can use "(Item [0-9]@)( )" to find the text 'Item' followed by any number of numbers.

    You can use "(Item [0-9]{1,n})( )" to find the text 'Item' followed by numbers where n is replaced by the maximum number of numbers you want to find.

    You can extend the search field by adding [a-z]@ or [a-z]{1,n} to include lowercase characters.

    e.g. "(Item [0-9]{1,2}[a-z]{1,3})( )" will find Item followed by one or two numbers then 1 or 2 or 3 lower case letters.

    In the above search texts brackets () have been used to enclose text. The () are known as fields. 1 to 9 fields can be used in the search text. You can reference these fields in the replace text using \1 to \9. So in all the cases above the replacement text will be "< b>\1\2< /b>" (like you I inserted a space to bake the bold tags visible.

    You can find more information on wildcard searches here

    https://wordmvp.com/FAQs/General/UsingWildcards.htm