objWord.Selection.Find.Execute()

Code attached gives me 3x a msgbox .. {TEST1 HALLO} {TEST2 REALLY} and {TEST3 NIGHT} .. i see in the word document that the correct text is selected .. what i want to do now is to replace all 3 {TEST?} with the second word between the { }

so original word document: {TEST1 HALLO} is changed to HALLO ... {TEST2 REALLY} is changed to REALLY etc



 
objWord.Selection.Find.ClearFormatting()
        objWord.Selection.Find.Replacement.ClearFormatting()
        With objWord.Selection.Find
            .Text = "\{TEST*\}"
            .Forward = True
            .Wrap = Microsoft.Office.Interop.Word.WdFindWrap.wdFindContinue
            .Format = False
            .MatchCase = False
            .MatchWholeWord = False
            .MatchWildcards = True
            .MatchSoundsLike = False
            .MatchAllWordForms = False
        End With
 
 
        Do While True
           
            objWord.Selection.Find.Execute()
            If objWord.Selection.Find.Found Then
 
                MsgBox(objWord.Selection.Text)
 
            Else
                Exit Do
            End If
        Loop

Open in new window

Amien90Asked:
Who is Participating?
 
The_KingCommented:
something like

try extracting the bit you want
and use something like

.Replacement.Text = s2
0
 
The_KingCommented:
Dim s as string
Dim s2 as string

s = objWord.Selection.Text
s2 = s.substring(s.indexOf(" "), s.length - s.indexOf(" "))

something like that should do what you want
subString returns a string substring(startposition, numberofcharacters)
indexOf returns the first occurance of a character in a string so we look for the first space
and get the substring from here until the length minus the number of characters in we started

hope this helps
0
 
Amien90Author Commented:
the problem is not to extract HALLO and REALLY out of objWord.Selection.Text

the problem is to make objWord.Selection.Find.Replacement.Text a flexibel text .. based on objWord.Selection.Text

0
 
The_KingCommented:
I see you want to extract the text

and then write it over the words in the words in the original word document?
0
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.