Replace text not working

G

Guest

Hello there,

I've written a subroutine to search and replace text within all shapes in
the Slidemaster, TitleMaster, HandoutMaster and NoteMaster.

The subroutine works well when I try to find and replace one word. As soon
as I search for several words or load the search string via a config file,
nothing is replaced.

The subroutine is below and has been taken from previous posts.

Any ideas what I might be doing wrong ? Should I be doing something to the
search and replace strings before comparing them ?

If oShp.HasTextFrame Then
If oShp.TextFrame.HasText Then
oTxtRng = oShp.TextFrame.TextRange
oTmpRng = oTxtRng.Replace(FindWhat:=FindString, _
Replacewhat:=ReplaceString, MatchCase:=True, _
WholeWords:=True)
Do While Not oTmpRng Is Nothing
oTxtRng = oTxtRng.Characters(oTmpRng.Start +
oTmpRng.Length, _
oTxtRng.Length)
oTmpRng = oTxtRng.Replace(FindWhat:=FindString, _
Replacewhat:=ReplaceString, MatchCase:=True, _
WholeWords:=True)
Loop
End If
End If

Thanks
 
S

Steve Rindsberg

Hello there,

I've written a subroutine to search and replace text within all shapes in
the Slidemaster, TitleMaster, HandoutMaster and NoteMaster.

The subroutine works well when I try to find and replace one word. As soon
as I search for several words or load the search string via a config file,
nothing is replaced.

How are you trying to search for several words? The routine will only
search/replace one string at a time. You'd need to invoke it once for each
string you need to replace.

Hint: you've posted the part of the code that doesn't change; the problem is
in the part that does change. ;-)
 
G

Guest

Hi Steve,

I'm calling the subroutine using the code below, where oPresMaster is a
reference to the SlideMaster, TitleMaster etc., FindString is the string to
replace and Classification is the replacement string.

For Each oShp In oPresMaster.Shapes
Call ReplaceText(oShp, FindString, Classification)
Next oShp

When FindString is "Commercial" the replacement works fine. When it is
"Commercial in Confidence", nothing happens.

Any ideas ?

Thanks

Weegie
 
S

Steve Rindsberg

Hi Steve,

I'm calling the subroutine using the code below, where oPresMaster is a
reference to the SlideMaster, TitleMaster etc., FindString is the string to
replace and Classification is the replacement string.

For Each oShp In oPresMaster.Shapes
Call ReplaceText(oShp, FindString, Classification)
Next oShp

When FindString is "Commercial" the replacement works fine. When it is
"Commercial in Confidence", nothing happens.

Any ideas ?

Shoot it? I've never had much luck with this function.

Does this get it for you?

If oShp.HasTextFrame Then
If oShp.TextFrame.HasText Then
oShp.TextFrame.TextRange.Text = _
Replace(oShp.TextFrame.TextRange.Text,FindString,ReplaceString)
end if
End If
 
G

Guest

Shooting it works for me !

Ended up doing something similar to what you suggested by getting the text
from the shape, replacing the string using the VB.net replace function and
then re-assigning the replacment text to the shape.

The code is below for reference.

Thanks for your help Steve.

If oShp.HasTextFrame Then
If oShp.TextFrame.HasText Then
strTest = oShp.TextFrame.TextRange.Text
strTest = Replace(strTest, FindString, ReplaceString)
oShp.TextFrame.TextRange.Text = strTest
End If
End If
 
S

Steve Rindsberg

Shooting it works for me !

Great!

Not criticizing or complaining but just curious: why assign the TextRange.Text to a
variable, process, then reassign back to the TextRange.Text?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top