Using VBA to replace text

J

Jeff Jones

I'm having an opportuniy meeting an objective. I have a character
string that I need to replace in many presesnations. I, frankly,
don't want to do this manually. It would be easy if PowerPoint
actually allowed me to record the key strokes to do a "replace all"
but it doesn't. I've created the following subroutine. The problem
is complicated by the fact that the text is on the Slidemaster. The
code below selects the first slide. The code doesn't get a hit on the
search string because it's on the slidemaster.

I can activate the slidemaster by including ActiveWindow.ViewType =
ppViewTitleMaster but haven't been able to figure out how to set the
oSld variable to the master. I get a type mismatch. Can anyone help
me to set the slide variable so I can spin through the shapes until
I've gotten to the one with the text I want to change. Any
alternative would do. Once I get this code working I can wrap in the
code to process all the slides in a selected folder.

Sub Change_Text()

Dim oSld As Slide
Dim oShp As Shape
Dim oTxtRng As TextRange
Dim oTmpRng As TextRange

Set oSld = Application.ActivePresentation.Slides(1)
On Error Resume Next

For Each oShp In oSld.Shapes
Set oTxtRng = oShp.TextFrame.TextRange
Set oTmpRng = oTxtRng.Replace(FindWhat:="original text.", _
Replacewhat:="replacement text", WholeWords:=True)
Do While Not oTmpRng Is Nothing
Set oTxtRng = oTxtRng.Characters(oTmpRng.Start +
oTmpRng.Length, _
oTxtRng.Length)
Set oTmpRng = oTxtRng.Replace(FindWhat:="original text", _
Replacewhat:="replacement text", WholeWords:=True)
Loop
Next oShp

End Sub

I'd sure appreciate any suggestions that anyone may have.

Thank you,
Jeff
 
G

Guest

Jeff,

I do not believe that the slide master is a member of the slide class. Just
use something like:

For Each shp in ActivePresentation.SlideMaster.Shapes

That should work, but I'm on an old Mac with PPT 98, so it might run into
trouble with multiple masters. If it doesn't work, let me know, and I'll try
to look at it if I have time tomorrow in my office when I have my PPT 2003
version available.

--David

David Marcovitz
Microsoft PowerPoint MVP
Author of _Powerful PowerPoint for Educators_
http://www.loyola.edu/education/PowerfulPowerPoint/
 
J

Jeff Jones

Thank you Dave. Your suggestion worked. I couldn't find anything in
the help explaining how to reference the SlideMaster. However, I have
another problem now. What I'm dealing with is a textbox with three
lins in it. When I set up the find and replace values, they both get
all strung together. Therefore I don't get a match.

When I store the current contents of the textbox I get all three lines
with line breaks included.

Here's an example.

FindWhat = "line1"
ReplaceWith = "line1"

When I have this code I dont get a hit because the TextFrame.TextRange
actually includes all three lines. When I copy the contents of the
stored value of TextFrame.TextRange I get the following.

"Line1
Line2
Line 3"

I've tried dong the following using just Chr(10) and the Chr(10) &
Chr(13) combination in my find and replace statements. For example:

FindWhat = "line1" & Chr(10) & Chr(13)
FindWhat = FindWhat & "Line 2" & Chr(10) & Chr(13)
FindWhat = FindWhat & "Line 3"

I still get the find and replace values run all together. Therefore
the resultant find doesn't match the stored TextFrame.TextRange value
and I don't get the replace to work.

I have multiple textboxes on the master and am uncertain that I can
identify the one I want to delete it and add a new one in the same
location.

Have you any additional ideas?

Thank you,
Jeff
 
S

Steve Rindsberg

I'm having an opportuniy meeting an objective. I have a character
string that I need to replace in many presesnations. I, frankly,
don't want to do this manually. It would be easy if PowerPoint
actually allowed me to record the key strokes to do a "replace all"
but it doesn't. I've created the following subroutine. The problem
is complicated by the fact that the text is on the Slidemaster. The
code below selects the first slide. The code doesn't get a hit on the
search string because it's on the slidemaster.

I can activate the slidemaster by including ActiveWindow.ViewType =
ppViewTitleMaster but haven't been able to figure out how to set the
oSld variable to the master. I get a type mismatch. Can anyone help
me to set the slide variable so I can spin through the shapes until
I've gotten to the one with the text I want to change. Any
alternative would do. Once I get this code working I can wrap in the
code to process all the slides in a selected folder.

You can get around this by using:

Dim oSld as Object ' instead of Slide

You could then:

' Do the slides
For each oSld in ActivePresentation.Slides
call FixUp(oSld)
Next ' slide

' Do the Slide and Title masters
Set oSld = ActivePresentation.SlideMaster
call Fixup(oSld)

If ActivePresentation.HasTitleMaster Then
Set oSld = ActivePresentation.TitleMaster
call FixUp(oSld)
End if

Sub FixUp (oSld as Object)
' here's where you pop in the code you're already using for the
' search/replace
Dim oSh as Shape
For each oSh in oSld.Shapes
' do yer stuff
Next
End Sub
As David's mentioned, it gets a bit more complex if you have multiple masters,
but this is a start.
 
S

Steve Rindsberg

You're using PPT 2000 or higher? If so, try using Replace instead.

With oSh.TextFrame.TextRange
.Text = Replace(.Text,"Replace This","With That")
End With
 
J

Jeff Jones

Steve and David,
The only problem I'm having is that I need to replace a phrase and not
single words. I don't have a one to one match between the original
and the needed replacement.

I've tried placing double quotes around both the find and the
replacement text.

I've tried including all three line in the text-frame and don't get a
match because the current text has line breaks in it.. I therefore
added Chr(10) and Chr(13) to the end of the first two lines. The
lines are still run together and therefore I don't get a hit with the
find.

I've tried taking a subset of the line that needs to be changed and
still don't get a match. However, when I take a word, the
find/replace works fine.

Does PP or the find/replace not allow me to replace a phrase?

Do have any ideas? Am I relegated to changing 150+ presentation
manually? Sigh.....

Thank you for your time,
Take care,
Jeff
 
D

David M. Marcovitz

I just did a little experiment, and had no problem doing something like
what you want. Here is my code:

Sub ReplaceTwoThree()
With ActivePresentation.SlideShowWindow.View.Slide.Shapes(2)
.TextFrame.TextRange.Replace _
"Two" & Chr$(13) & "Three", "Seven" & Chr$(13) & "Eight"
End With
End Sub

I simply put in Shape 2 of a slide:

One
Two
Three
Four
Five

I assigned the macro to a button that ran this code, and it turned it
into:

One
Seven
Eight
Four
Five

If this basic idea doesn't work for you, then perhaps your line breaks
are caused by something other than a standard Chr$(13). What is different
about what you are doing than what I am doing?

--David
--
David M. Marcovitz
Microsoft PowerPoint MVP
Director of Graduate Programs in Educational Technology
Loyola College in Maryland
Author of _Powerful PowerPoint for Educators_
http://www.loyola.edu/education/PowerfulPowerPoint/
 
S

Steve Rindsberg

Have a look at David's post if you haven't already, but if that doesn't help,
post the relevant bits of code you're using at this point and show us an
example or two of the specific text you're searching/replacing (in context)

Steve and David,
The only problem I'm having is that I need to replace a phrase and not
single words. I don't have a one to one match between the original
and the needed replacement.

I've tried placing double quotes around both the find and the
replacement text.

I've tried including all three line in the text-frame and don't get a
match because the current text has line breaks in it.. I therefore
added Chr(10) and Chr(13) to the end of the first two lines. The
lines are still run together and therefore I don't get a hit with the
find.

I've tried taking a subset of the line that needs to be changed and
still don't get a match. However, when I take a word, the
find/replace works fine.

Does PP or the find/replace not allow me to replace a phrase?

Do have any ideas? Am I relegated to changing 150+ presentation
manually? Sigh.....

Thank you for your time,
Take care,
Jeff
 
J

Jeff Jones

Here's the code I'm using. The line break is still not getting into
the find and replace values. At least when I copy their values to the
notrepad along with the actual TextRange I get line breaks in the
TextRange and not in the find and replace strings. Pretty weird. I'm
running PP 2002 SP3. The code was pulled directly from the VBA help
file for the Replace Method.

I have to use the find value because I have multiple textboxes on the
masters and can't depend on knowing the physical number of the
textbox..

Sub Change_Legal_Line()

Dim oShp As Shape
Dim FindWhat As String
Dim ReplaceWith As String

FindWhat = "EDS is a registered mark and the EDS logo is a trademark
of Electronic Data Systems Corporation." & Chr(13) & "EDS is an equal
opportunity employer and values the diversity of its people. " &
Chr(13)
& "(c) 2004 Electronic Data Systems Corporation. All rights reserved."

ReplaceWith = "EDS and the EDS logo are registered trademarks of
Electronic Data Systems Corporation." & Chr(13) & "EDS is an equal
opportunity employer and values the diversity of its people." &
Chr(13)
& "(c) 2005 Electronic Data Systems Corporation. All rights reserved."

For Each oShp In ActivePresentation.TitleMaster.Shapes
Call ReplaceText(oShp, FindWhat, ReplaceWith)
Next oShp

End Sub


Sub ReplaceText(oShp As Object, FindString As String, ReplaceString As
String)

Dim oTxtRng As TextRange
Dim oTmpRng As TextRange
Dim strTest As String
On Error Resume Next

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

Thank you both for your help.

Take care,
Jeff
 
G

Guest

11

That is, your number for a new line is not 13 or 10 or 10 followed by 13. It
is 11. How do I know:

For Each mychar In oShp.TextFrame.TextRange.Characters
If getReady Then
MsgBox Asc(mychar) & " is the number."
End If
If mychar = "." Then
getReady = True
Else
getReady = False
End If
Next mychar

Each of your new lines came after a period, so I wrote this little script to
figure out what the ASCII value of the character after period was, and it
came back with 11. Also, change your WholeWords to False (I don't know if
that is necessary, but I changed it while I was testing something, and it
made something work).

Just be careful that all your text is identical, including, not having any
extra spaces.

--David

David Marcovitz
Microsoft PowerPoint MVP
Author of _Powerful PowerPoint for Educators_
http://www.loyola.edu/education/PowerfulPowerPoint/
 
J

Jeff Jones

Whew......
I finally got it, with the help of your snippet of code. I ran it
against the textbox and the line breaks were Chr$(13)'s with a Chr$32)
thrown in. That was a good idea! I'll remember it the next time I
have such an opportunity.

I cut the find string back to just the line I wanted to change along
with the Chr$(13) and I finally got a match.

This has certainly been a pain in the database. I truly appreciate
your patience and sticking with this wonderfulness until I finally got
it. Don'tcha just love technology? <giggle>

Thank you again.

Take care,
Jeff
 
D

David M. Marcovitz

Great. I'm glad this worked for you, and I'm glad my little code snippet
helped. I just wonder why I got 11, and you got 13. Oh well...as long as
it's working.
--David

--
David M. Marcovitz
Microsoft PowerPoint MVP
Director of Graduate Programs in Educational Technology
Loyola College in Maryland
Author of _Powerful PowerPoint for Educators_
http://www.loyola.edu/education/PowerfulPowerPoint/
 
J

Jeff Jones

I suspect that the character ended up being changed when the lines
were added to the newsgroup message. Either that or in the Outlook
message I used to send to code from one computer to this one.

The code gave me exactly what I needed although I don't know why I
needed to include the end character in the find criteria. I can find
just the year and change it with no problems. I could also change a
single word in the line with no problems. I changed "registered" to
"doofratz" to test the code. Apparently changing a phrase needs the
line delimiter. Pretty weird.

I ran a test against multiple presentations in a folder and it changed
all that had title and slide masters. I need to capture the situation
where I don't have either but I don't expect that to be a problem.
(Famous last words. <giggle>)

Thank you again.


Jeff
 
D

David M. Marcovitz

Did you change MatchWholeWords to be False? I was able to do the replace
in my test run without the end character when I did that.
--David

--
David M. Marcovitz
Microsoft PowerPoint MVP
Director of Graduate Programs in Educational Technology
Loyola College in Maryland
Author of _Powerful PowerPoint for Educators_
http://www.loyola.edu/education/PowerfulPowerPoint/
 
J

Jeff Jones

I had tried that along with just leaving the option off altogether to
no avail.

I just found out that the following doesn't work if there's no title
master. I still get a pop-up error message.

Application.DisplayAlerts = ppAlertsNone

Although, the following "if" statement can test for the presence of a
TitleMaster and if non is present, a msgbox will display the fil name
so I know which presentations I get to either do by hand or
programmatically add a title master. I'm inclined to just skip those
presentations rather than fiddle with adding the TitleMaster via the
code.

Maybe I can add a called procedute to the beginning of the change
Legal Line code to test for tyitle master and bypass the error message
with a big honking negative number.
 
S

Steve Rindsberg

David M. said:
Great. I'm glad this worked for you, and I'm glad my little code snippet
helped. I just wonder why I got 11, and you got 13.

David,

Something[ENTER]
Something

gives you 13

Something[SHIFT+ENTER]
Something

gives you 11

Paragraph ending vs Line Break.
 
D

David M. Marcovitz

OK. I think I've lost track of what you are doing. I thought you were
scanning through the masters (title and slide) and the text boxes to find
the text and replace it with the appropriate text. Why do you need to add
a title master if there is none. Can't you simply check for a title
master; if one is present, scroll through it's text boxes and do the
Replace thing. Then do the same for the Slide master. Then do the same
for all the slides. That should cover you. It seems silly to add a title
master just so you can do something that does not need to be done.
Additionally, if the copyright statement is just as you describe, you
should have no need to do anything by hand. What am I missing?
--David

--
David M. Marcovitz
Microsoft PowerPoint MVP
Director of Graduate Programs in Educational Technology
Loyola College in Maryland
Author of _Powerful PowerPoint for Educators_
http://www.loyola.edu/education/PowerfulPowerPoint/
 
J

Jeff Jones

Just in case, I built a function that tests for the TitleMaster and
returns a "No" if there is none after displaying the presentation
name. I then close the presentation and move on.
 
J

Jeff Jones

LOL. I don't blame you. I have these presentations that may or may
not follow corporate standards. Those that do need to have the legal
statement changed. Those that aren't up to standards either have to
be brought up to standards if we "own" them or the people who do have
to make the changes. I don't yet know if I need to add a TitleMaster
and apply the corporate standard so I won't do so until I've
determined who owns the presentation available via our processes.

Jeff
 
J

Jeff Jones

Yep. In MS Word the first is represented by the "^p" character string
and the second is represented by the "^l" character string. I
frequently reformat text to add a tab character, convert the text to a
table, generally I delete a column, reconvert the table back to text
and then change the tab "^t" character back to paragraph characters.
I just needed to be able to find out what character I was dealing with
and David code let me do that.

Thank you Steve.

Jeff

David M. said:
Great. I'm glad this worked for you, and I'm glad my little code snippet
helped. I just wonder why I got 11, and you got 13.

David,

Something[ENTER]
Something

gives you 13

Something[SHIFT+ENTER]
Something

gives you 11

Paragraph ending vs Line Break.



-----------------------------------------
Steve Rindsberg, PPT MVP
PPT FAQ: www.pptfaq.com
PPTools: www.pptools.com
================================================
 

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