Excel宏将pptx保存为pdf;代码错误



我知道这个问题可能被问了1000次,但在过去的3个小时里,我一直在努力通过excel vba将pptx转换为pdf(这是我的报告生成器所必需的,为了保持布局整洁,我决定使用PowerPoint,因为单词经常把事情搞砸)。

这是我正在使用的代码:

Dim ppt As Object
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
Set WDReport = ppt.Presentations.Open("C:UsersUser1DocumentsFolderFinal Report Template.pptx")
WDReport.UpdateLinks
Dim FileName2 As String
FileName2 = "C:UsersUser1DocumentsFolderComplete Report" & Sheet14.Range("Q3").Text & " No " & Sheet14.Range("U21") & " Report" & Sheet17.Range("E10").Text & ".pdf"
WDReport.ExportAsFixedFormat FileName2, ppFixedFormatTypePDF, ppFixedFormatIntentScreen
WDReport.Close
ppt.Quit
Set ppt = Nothing
Set WDReport = Nothing 

但我一直在WDReport.ExportAsFixedFormat FileName2, ppFixedFormatTypePDF, ppFixedFormatIntentScreen线上收到一条错误消息"13类型不匹配"。我试图用ActivePresentation替换WDReport,但收到错误"429ActiveX组件无法创建对象"。

我已经包含了所有必要的库(Microsoft PowerPoint Object Library 15.0,与MS Word相同),但到目前为止没有效果。

UPD:

为了澄清FileName2字符串,Ranges用于获取以下变量数据:

Range("Q3") is Name (e.g. Test Company)
Range("U21") is Number (e.g. 1234567891011)
Range("E10") is Date (e.g. Feb-15)

因此,最终的文件名应该是"测试公司编号1234567891011 Report Feb-15.pdf"。再次,当我将.docx转换为pdf 时,它运行良好

如果有人能帮我解决这个问题,我将不胜感激。

我能够重现您的错误。以下解决方案对我有效。请确保您已启用对Microsoft Powerpoint对象库的引用。

WDReport.SaveAs FileName2, ppSaveAsPDF

最新更新