objWord.Selection.Find.Execute

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
            .Execute
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 }

Amien90Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

GrahamSkanRetiredCommented:
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

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Word

From novice to tech pro — start learning today.