With objWord.Selection.Find
            .Text = RegEx
            .Replacement.Text = Replacement
            .Forward = True
            .Wrap = Microsoft.Office.Interop.Word.WdFindWrap.wdFindContinue
            .Format = False
            .MatchCase = True
            .MatchWholeWord = False
            .MatchWildcards = False
            .MatchSoundsLike = False
            .MatchAllWordForms = False
End With

i use this regex to find/select the text that i need: \{TEXT*\}

after the text was found .. it will be replaced.. no problem there.

the problem is that its possible that i use a {TEXT} in a {TEXT} for example: {TEXT BLA BLA {TEXT BLA BLA } BLA }

When i execute the following text will be selected: {TEXT BLA BLA {TEXT BLA BLA } instead of {TEXT BLA BLA {TEXT BLA BLA } BLA }

how can i first select {TEXT BLA BLA } and then the higher one: {TEXT BLA BLA {TEXT BLA BLA } BLA }

Who is Participating?
GrahamSkanConnect With a Mentor RetiredCommented:
First of all I assume that the code that you are actually running has

.MatchWildcards = True

and hence uses Word's wildcarding functionality for Find and Replace.

You cannot always guarantee to find a pattern that will find in a single pass every string that you want to match and exclude those that you don't want.

You might have to search for the inner string first and to modify it so that it will not interfere with the outer find. The second pass could then find the whole string.
In this example the {} pairs are replaced by [] pairs or any other two characters strings which do not appear in the original text. The second pass will do the replacement and the last two will put back the original {} pairs that were not part of a nested set.

Find: \{([ABELTX ]{1,})\}
Replace: [(1)]
            .MatchWildcards = True
            .Text = "\{([ABELTX ]{1,})\}"
            .Replacement.Text = "\1"
            .Execute Replace:=wdReplaceAll
            .Text = "\{([\[\]ABELTX ]{1,})\}"
            .Replacement.Text = "NewText"
            .Execute Replace:=wdReplaceAll
            .MatchWildcards = False
            .Text = "["
            .Replacement.Text = "{"
            .Execute Replace:=wdReplaceAll
            .Text = "]"
            .Replacement.Text = "}"
            .Execute Replace:=wdReplaceAll

Open in new window

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.