Help find and replace in Powerpoint with VBA

pirespt

New Member
Joined
Jun 1, 2012
Messages
4
Hi Everyone,

This is my first post and I hope you can help me.

I've been stuck with this for a while now.

In bold is where the code breaks and I don't know how to fix it. Can anyone help?

Here's the code


Sub CreatePowerPoint()<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:eek:ffice:eek:ffice" /><o:p></o:p>
<o:p></o:p>
'Add a reference to the Microsoft PowerPoint Library by:<o:p></o:p>
'1. Go to Tools in the VBA menu<o:p></o:p>
'2. Click on Reference<o:p></o:p>
'3. Scroll down to Microsoft PowerPoint X.0 Object Library, check the box, and press Okay<o:p></o:p>
<o:p></o:p>
'First we declare the variables we will be using<o:p></o:p>
Dim pptx As PowerPoint.Application<o:p></o:p>
Dim activeSlide As Slide<o:p></o:p>
Dim cht As Excel.ChartObject<o:p></o:p>
<o:p></o:p>
'Look for existing instance<o:p></o:p>
On Error Resume Next<o:p></o:p>
Set pptx = GetObject(, "PowerPoint.Application")<o:p></o:p>
On Error GoTo 0<o:p></o:p>
<o:p></o:p>
'Let's create a new PowerPoint<o:p></o:p>
<o:p></o:p>
Set pptx = New PowerPoint.Application<o:p></o:p>
pptx.Visible = msoCTrue<o:p></o:p>
<o:p></o:p>
pptx.Presentations.Open Filename:="……………………<o:p></o:p>
<o:p></o:p>
'Show the PowerPoint & save as the current selection on excel into the outputs folder<o:p></o:p>
pptx.Visible = True<o:p></o:p>
<o:p></o:p>
a = Format(Sheets("Tables").Range("D2").Value, "mmm-yy")<o:p></o:p>
<o:p></o:p>
b = Sheets("Tables").Range("D4").Value<o:p></o:p>
<o:p></o:p>
c = Format(Date, "yymmdd")<o:p></o:p>
<o:p></o:p>
pptx.ActivePresentation.SaveAs Filename:="……………………….. <o:p></o:p>
<o:p></o:p>
Dim oShp As Shape<o:p></o:p>
Dim oTxtRng As TextRange<o:p></o:p>
Dim oTmpRng As TextRange<o:p></o:p>
Dim strWhatReplace As String, strReplaceText As String<o:p></o:p>
<o:p></o:p>
' write find text<o:p></o:p>
strWhatReplace = "Member"<o:p></o:p>
' write change text<o:p></o:p>
strReplaceText = "b"<o:p></o:p>
<o:p></o:p>
' go during each slides<o:p></o:p>
For Each activeSlide In pptx.ActivePresentation.Slides<o:p></o:p>
' go during each shapes and textRanges<o:p></o:p>
For Each oShp In activeSlide.Shapes "it triggers an error 13, type mismatch"
' replace in TextFrame<o:p></o:p>
Set oTxtRng = oShp.TextFrame.TextRange<o:p></o:p>
Set oTmpRng = oTxtRng.Replace( _<o:p></o:p>
FindWhat:=strWhatReplace, _<o:p></o:p>
Replacewhat:=strReplaceText, _<o:p></o:p>
WholeWords:=True)<o:p></o:p>
<o:p></o:p>
Do While Not oTmpRng Is Nothing<o:p></o:p>
<o:p></o:p>
Set oTxtRng = oTxtRng.Characters _<o:p></o:p>
(oTmpRng.Start + oTmpRng.Length, oTxtRng.Length)<o:p></o:p>
Set oTmpRng = oTxtRng.Replace( _<o:p></o:p>
FindWhat:=strWhatReplace, _<o:p></o:p>
Replacewhat:=strReplaceText, _<o:p></o:p>
WholeWords:=True)<o:p></o:p>
Loop<o:p></o:p>
Next oShp<o:p></o:p>
Next activeSlide<o:p></o:p>
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
I too am stuck on the exact same thing. The weird part is that it worked fine in PowerPoint but when I transfered the code to Excel, made my tweaks it stopped.

I can see all of the "Shapes" in the active slide. Why the mismatch?
 
Upvote 0
I have come up with a work around.

A macro in Powerpoint that requires an inputbox with the data I want to replace with.

Then I run that from excel, not perfect though.
 
Upvote 0
I keep thinking it has something to do with the fact that the Slide I and looping through isn't activated????
 
Upvote 0

Forum statistics

Threads
1,214,923
Messages
6,122,289
Members
449,077
Latest member
Rkmenon

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top