VBA Save PPT as PDF through Excel

mkmiller

New Member
Joined
Dec 14, 2011
Messages
7
I have a macro that writes specific charts and ranges to a powerpoint shell. I would like to then have the macro save the powerpoint as a pdf with the file name specified in the excel file and close it. I have pasted my existing macro below. Can you please help??



Private Sub CommandButton2_Click()
Dim ppApp As PowerPoint.Application
Dim PPPres As PowerPoint.Presentation
Dim ppSlide As PowerPoint.Slide
Dim PresentationFileName As Variant
Dim SlideCount As Long
Dim rowCnt As Integer
Dim Startingrow As Integer
Dim Endingrow As Integer
Dim SlideNumber As Long
Dim pptfilepath As String
Dim Top As Integer
Dim Left As Integer
Dim Width As Integer
Dim Height As Integer
Dim DataType As String

pptfilepath = Cells(5, 3).Value
Startingrow = Cells(2, 3).Value
Endingrow = Cells(3, 3).Value

Set ppApp = CreateObject("PowerPoint.Application")
ppApp.Visible = True
Set PPPres = ppApp.Presentations.Open(pptfilepath)

' Reference existing instance of PowerPoint
Set ppApp = GetObject(, "Powerpoint.Application")
' Reference active presentation
Set PPPres = ppApp.ActivePresentation
ppApp.ActiveWindow.ViewType = ppViewSlide

For rowCnt = Startingrow To Endingrow
' set parameters of first chart
DataType = Cells(rowCnt, 3).Value
chtname = Cells(rowCnt, 4).Value
Worksheetname = Cells(rowCnt, 5).Value
SlideNumber = Cells(rowCnt, 6).Value
Top = Cells(rowCnt, 7).Value
Left = Cells(rowCnt, 8).Value
Width = Cells(rowCnt, 9).Value
Height = Cells(rowCnt, 10).Value


' copy chart as a picture
If DataType = "Chart" Then
Worksheets(Worksheetname).ChartObjects(chtname).Chart.CopyPicture _
Appearance:=xlScreen, Size:=xlScreen, Format:=xlPicture
End If

' copy range as a picture
If DataType = "Range" Then
Worksheets(Worksheetname).Range(chtname).CopyPicture _
Appearance:=xlScreen, Format:=xlPicture
End If

' Add a new slide and paste in the chart
SlideCount = PPPres.Slides.Count
Set ppSlide = PPPres.Slides(SlideNumber)
ppApp.ActiveWindow.View.GotoSlide ppSlide.SlideIndex
With ppSlide
' paste and select the chart picture
.Shapes.Paste.Select
' align the chart
ppApp.ActiveWindow.Selection.ShapeRange.Left = Left
ppApp.ActiveWindow.Selection.ShapeRange.Top = Top

If Width <> 0 Then
ppApp.ActiveWindow.Selection.ShapeRange.Width = Width
End If

If Height <> 0 Then
ppApp.ActiveWindow.Selection.ShapeRange.Height = Height
End If

End With

Next

' Clean up
Set ppSlide = Nothing
Set PPPres = Nothing
Set ppApp = Nothing



End Sub
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.

Forum statistics

Threads
1,215,248
Messages
6,123,869
Members
449,130
Latest member
lolasmith

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