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



我正在尝试调用子函数,但失败并显示错误:

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

错误位于SavePDFAsPng的第一行:

Dim oPres As Presentation

这是整个宏:

Sub Button1_Click()
Call SavePDFAsPng("C:Usersgfas1Desktopahm.pdf", "C:Usersgfas1DesktopMyTest.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://learn.microsoft.com/en-us/office/vba/api/overview/powerpoint/object-model

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

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

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

最新更新