user3194519
user3194519

Reputation: 51

Save powerpoint presentation as as a pdf in vba

I have looked at a lot of answers to this question but can't figure out what I have done wrong. I am trying to create a pdf file. I get my data from an excel file and copy it into powerpoint. I then try to save as pdf but it keeps giving me an error (object required) at the saving pdf section of the macro (see below). I tried changing it multiple times but still can't get it to work. Have attached code below. After I fix this problem, I need to be able to change the size of the object I pasted in - how do I do that.

Sub CreatePDFfiles_4()


Dim PPapp As Object
Dim PPPres As Object
Dim first_file As Boolean
Dim investorname As String
Dim path As String


Sheets("printing").Select
Range("g2").Select
file1 = ActiveCell.Value
Range("g3").Select
path = ActiveCell.Value
Range("g8").Select
investorname = ActiveCell.Value
Range("i8").Select
cor_file_name = ActiveCell.Value
DestinationPPT = "C:\Users\name\Documents\company\Investment Model\printing macro\template.pptx"


While investorname <> "end"
    ActiveCell.Offset(0, -1).Select
    print_data = ActiveCell.Value
    If print_data = "Yes" Then

        ' Initialize PowerPoint Object Library
        Set PPapp = CreateObject("Powerpoint.Application")
        PPapp.Visible = True

        ' Open presentation
        Set PPPres = PPapp.Presentations.Open(DestinationPPT)

        'Copy excel file data
        Windows(file1).Activate
        Sheets(investorname).Select
        Range("b1:r46").Select
        Selection.Copy

        'Paste into existing powerpoint template slide that is open
        PPPres.slides(1).Shapes.Paste

        'Save as pdf
        PPPres.ExportAsFixedFormat ActivePresentation.path & "\" & cor_file_name & ".pdf", ppFixedFormatTypePDF, ppFixedFormatIntentPrint



        PPapp.Quit
        Set PPapp = Nothing

Upvotes: 2

Views: 11253

Answers (3)

Hafi Uddin
Hafi Uddin

Reputation: 1

I tried the code in the answer by LCCJR, but it gave me:

Run-time error '438': Object doesn't support this property or method

I just removed the ActivePresentation part from save as command and it worked for me and created the PDF.

So the command for me is as below:

PPT.SaveAs Path & PdfFileNm & ".pdf", 32

Upvotes: 0

Swapnil
Swapnil

Reputation: 1

You need to update your code as below. It works for me.

`

' clear the print range and set it to just the current slide

ppt.PrintOptions.Ranges.ClearAll
Set pr = ppt.PrintOptions.Ranges.Add(Start:=sld.slideNumber, End:=sld.slideNumber)

 ppt.ExportAsFixedFormat Path:=folderPath & fn, _
                        FixedFormatType:=2, _
                        RangeType:=1, _
                        PrintRange:=pr, _
                        Intent:=1, _
                        FrameSlides:=msoFalse`

Upvotes: 0

LCCJR
LCCJR

Reputation: 41

None of that worked for me. it was as simple as:

file_name = (path and name of the file you want to open)
Path = (where you want to save it)
PdfFileNm = (name of the file)

Set PPT = CreateObject("PowerPoint.Application")
Set Pres = PPT.presentations.Open(file_name)
PPT.ActivePresentation.SaveAs Path & PdfFileNm & ".pdf", 32

Upvotes: 4

Related Questions