Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

save pptx as pdf through excel

I am trying to convert all pptx files in a give path to pdf files.

my code:

Sub pptxtopdf()

    Dim ppt As Object
    Dim objFSO As Object
    Dim objFolder As Object
    Dim objFile As Object


    Dim i As Integer
    On Error Resume Next

    Set ppt = GetObject(, "PowerPoint.Application")
    If ppt Is Nothing Then
    Set ppt = CreateObject("PowerPoint.Application")
    End If
    On Error GoTo 0


    'Create an instance of the FileSystemObject
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    'Get the folder object
    Set objFolder = objFSO.GetFolder("P:\Operations\Data & Deliverables\Projects\Amica\presentation_workspace\1_ spring 2015\Presentations\Volvo")
    i = 1
    'loops through each file in the directory 
    For Each objFile In objFolder.Files

        Set WDReport = ppt.Presentations.Open(objFile.Path)

        Dim FileName2 As String
        FileName2 = Replace(objFile.Path, "pptx", "pdf")

        'WDReport.ExportAsFixedFormat FileName2, ppFixedFormatTypePDF
        WDReport.SaveAs FileName2, ppSaveAsPDF

        WDReport.Close
        ppt.Quit

        Set ppt = Nothing
        Set WDReport = Nothing


        i = i + 1
    Next objFile


End Sub

error msg

Presentation.SaveAs :  Invalid enumeration value. 

Cannot see what I'm doing wrong?

same problem as here but the solution didnt work for me - Excel macro to save pptx as pdf; error with code

like image 484
Boosted_d16 Avatar asked May 01 '15 10:05

Boosted_d16


1 Answers

You are late binding PowerPoint.Application so its enumeration values are not exposed or available in the global VBA namespace.

As you have not added option explicit to warn you of undeclared variables your use of the undeclared ppSaveAsPDF causes no error but has no value.

Add:

const ppSaveAsPDF as long = 32

To the top of the module to provide the expected value to SaveAs.

like image 81
Alex K. Avatar answered Nov 12 '22 18:11

Alex K.