"Find and Replace" in PowerPoint using Excel VBA

smit3446

New Member
Joined
Nov 16, 2015
Messages
46
The title is pretty self explanatory. Any idea how to do this? Here's the code I have so far which calls some functions to paste excel charts into the PowerPoint:

Code:
Sub ExcelToPres()

Dim PPT As PowerPoint.Application
Dim MyPPT As PowerPoint.Presentation


Set PPT = New PowerPoint.Application
PPT.Visible = True
Set MyPPT = PPT.Presentations.Open(Filename:="H:\HC Assignments\Macros\State Study Template.pptx")
Dim State_Name As String
Dim Directory_File As String


copy_chart "Sheet1", 3, "Chart 1"
copy_range "Sheet1", 2, "A1:B3"
copy_group "Sheet1", 2, "Group 4"


State_Name = Worksheets("Sheet1").range("A1").Value


Directory_File = "H:\Test\" & State_Name & ".pptx"
MyPPT.SaveAs Directory_File


End Sub

I need to replace any instance of the string "_STATE_" with my variable, State_Name.


​Thanks!
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
Where exactly are the strings to be replaced?

Code:
' Excel module
Sub SlidesAndTables()
Dim sld As Slide, objppt As powerpoint.Application, shp As powerpoint.Shape, _
act As Presentation, i%, j%, sname, n$
Set objppt = CreateObject("PowerPoint.Application")
Set act = objppt.ActivePresentation
sname = Worksheets("Sheet1").[a1]
n = "_STATE_"
For Each sld In act.Slides
    For Each shp In sld.Shapes
        If shp.HasTextFrame Then
            If shp.TextFrame.HasText Then shp.TextFrame.TextRange.Text = _
            Replace(shp.TextFrame.TextRange.Text, n, sname)
        End If
        If shp.HasTable Then
            For i = 1 To shp.Table.Rows.Count
                For j = 1 To shp.Table.Columns.Count
                    shp.Table.Rows.Item(i).Cells(j).Shape.TextFrame.TextRange.Text = _
                    Replace(shp.Table.Rows.Item(i).Cells(j).Shape.TextFrame.TextRange.Text, n, sname)
            Next j, i
        End If
    Next
Next
End Sub
 
Upvote 0
is there a way to find and replace in powerpoint using a list of words in one column in excel and replacing it with another list of words?
 
Upvote 0
Like this

VBA Code:
' Excel module
Sub SlidesAndTables()
Dim sld As Slide, objppt As PowerPoint.Application, shp As PowerPoint.Shape, _
act As Presentation, i%, j%, k%
Set objppt = CreateObject("PowerPoint.Application")
Set act = objppt.ActivePresentation
For k = 1 To [a1].End(xlDown).Row
For Each sld In act.Slides
    For Each shp In sld.Shapes
        If shp.HasTextFrame Then
            If shp.TextFrame.HasText Then shp.TextFrame.TextRange.Text = _
            Replace(shp.TextFrame.TextRange.Text, Cells(k, 1), Cells(k, 2))
        End If
        If shp.HasTable Then
            For i = 1 To shp.Table.Rows.Count
                For j = 1 To shp.Table.Columns.Count
                    shp.Table.Rows.Item(i).Cells(j).Shape.TextFrame.TextRange.Text = _
                    Replace(shp.Table.Rows.Item(i).Cells(j).Shape.TextFrame.TextRange.Text, Cells(k, 1), Cells(k, 2))
            Next j, i
        End If
    Next
Next
Next
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,819
Messages
6,181,153
Members
453,021
Latest member
Justyna P

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