简体   繁体   中英

User type not defined macro error vba in method

I am trying to call a sub function, but fails with an error:

User type not defined on the saveas method at this line. Maybe presentation isn't being processed in vba

The error is on the first line of SavePDFAsPng :

Dim oPres As Presentation

Here is the whole macro:

Sub Button1_Click()
    Call SavePDFAsPng("C:\Users\gfas1\Desktop\ahm.pdf", "C:\Users\gfas1\Desktop\MyTest.PNG")
End Sub

Sub SavePDFAsPng(sPathToPDF As String, sPathToPNG As String)

            Dim oPres As Presentation
            Dim oSh As Shape

            ' Height/Width are hardcoded here
            ' You could get trickier and bring the PDF into any presentation
            ' once to get its proportions, delete it, set the slide size to the same
            ' proportions, then re-insert the PDF
            Dim sngWidth As Single
            Dim sngHeight As Single
            sngWidth = 612
            sngHeight = 792

            Set oPres = Presentations.Add
            With oPres
                With .PageSetup ' set it to 8.5x11
                    .SlideHeight = sngHeight  ' 11in * 72 points per inch
                    .SlideWidth = sngWidth
                End With
                .Slides.AddSlide 1, .SlideMaster.CustomLayouts(1)
                With .Slides(1)
                    Set oSh = .Shapes.AddOLEObject(0, 0, sngWidth, sngHeight, , sPathToPDF)
                    Call .Export(sPathToPNG, "PNG")
                End With
                .Saved = True
                .Close

            End With

        End Sub

You tagged the question with "Excel", so I assume you're trying to run this macro in Excel, which won't work because the Presentation type is not in Excel by default.

It's in PowerPoint:

https://docs.microsoft.com/en-us/office/vba/api/overview/powerpoint/object-model

https://docs.microsoft.com/en-us/office/vba/api/powerpoint.presentation

You need to run this macro in PowerPoint, not Excel.

You can run it in Excel, but you need to import the PowerPoint type-library into your Excel VBA project, but that's a separate question.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM