Hello, I need to solve a problem (search and select text range in VBA macro) with some constraints.
1. I am using regex because of some requirements which could not be handled by word Find.
2. The search range may contain Content control Tags.

The problem is that regex match does not account for Content Control tags characters, however when I use range object to select range based on index of the regex match, the selection is off by number of content control tags * 2. Each cc tag has 2 characters, start and end.

If the search range has not Content Controls, the code works fine, if I put one or more content controls into the search range the match range selection is off. I am working on code trying to move match range based on CC counts, however the challenge is that real examples could contain nested content controls and search word could appear within a content control.

Is there a way for regex to account for content controls or for range select to skip content control tag when selecting range?


here is a test code:
Sub regexHelp()

Dim RegEx As New RegExp
Dim Matches, match As match
Dim searchRange, matchRange As Word.range

'search example: hello world!
RegEx.pattern = "world"
RegEx.IgnoreCase = False
RegEx.Global = False

Set searchRange = selection.range
Set Matches = RegEx.Execute(searchRange)
Set match = Matches.Item(0)
Debug.Print "search range: ", searchRange.start, searchRange.End


Debug.Print " match range: ", match.value, match.FirstIndex, match.Length
Set matchRange = ActiveDocument.range(searchRange.start + match.FirstIndex, searchRange.start + match.FirstIndex + match.Length)
matchRange.Select

End Sub


thank you