繁体   English   中英

方法中未定义用户类型宏错误 vba

[英]User type not defined macro error vba in method

我正在尝试调用子 function,但失败并出现错误:

此行的 saveas 方法上未定义用户类型。 也许演示文稿没有在 vba 中处理

错误出现在SavePDFAsPng的第一行:

Dim oPres As Presentation

这是整个宏:

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

你用“Excel”标记了这个问题,所以我假设你试图在 Excel 中运行这个宏,这不起作用,因为Presentation类型默认不在 Excel 中。

它在 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

您需要在 PowerPoint 中运行此宏,而不是 Excel。

可以在 Excel 中运行它,但您需要将 PowerPoint 类型库导入 Excel VBA 项目,但这是一个单独的问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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