【问题标题】:Excel macro to save pptx as pdf; error with codeExcel宏将pptx保存为pdf;代码错误
【发布时间】:2015-02-27 01:44:53
【问题描述】:

我知道这个问题可能被问了 x1000 次,但在过去的 3 个小时里我一直在努力通过 excel vba 将 pptx 转换为 pdf(这是我的报告生成器所必需的,并且为了保持布局干净的广告tidy 我决定使用 PowerPoint,因为 word 总是把事情搞砸)。

这是我正在使用的代码:

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:\Users\User1\Documents\Folder\Final Report Template.pptx")

WDReport.UpdateLinks

Dim FileName2 As String
FileName2 = "C:\Users\User1\Documents\Folder\Complete 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,但收到错误“429 ActiveX 组件无法创建对象”。

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

更新:

只是为了澄清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)

所以最终文件名将类似于 “Test Company No 1234567891011 Report Feb-15.pdf”。再一次,当我将 .docx 转换为 pdf 时,它运行良好

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

【问题讨论】:

  • 这可能是你的FileName2= 任务吗?特别是Sheet14.Range("U21")...你错过了.Text吗?
  • 嗨 Porcupine911。感谢您注意到我错过了.Text,但不幸的是,这不是解决方案:(而且,奇怪的是,当我最初使用 MS 时,FileName2 没有任何问题(即使没有.Text :D) Word而不是PPT。我不确定问题是否出在FileName2,因为我将那行改为FileName2 = "C:\Users\User1\Documents\Folder\Complete Report\Report.pdf",但仍然收到13和429错误...
  • 我尝试从 PowerPoint Dim FileName2 As String: FileName2 = "C:\Users\User1\Documents\Folder\Complete Report\Report.pdf"; ActivePresentation.ExportAsFixedFormat FileName2, ppFixedFormatTypePDF, ppFixedFormatIntentScreen 运行这段代码,它工作得很好,所以我猜 Excel-PowerPoint 命令执行存在问题。虽然不知道如何修复它...

标签: excel pdf powerpoint vba


【解决方案1】:

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

WDReport.SaveAs FileName2, ppSaveAsPDF

【讨论】:

  • 嗨 Porcupine911。谢谢,确实有效!如此简单但有效的解决方案!让我想知道为什么大多数消息来源都建议使用ActivePresentation.ExportAsFixedFormat FileName2, ppFixedFormatTypePDF。感谢您花时间复制我的宏并找到解决方案!你救了我的一天! :)
  • 不客气!我这样做只是为了帮助别人。我很失望,我不知道为什么 ExportAsFixedFormat 不起作用,但我很高兴这个解决方法成功了!
  • 有关实际解决 ExportAsFixedFormat 问题的答案,请参阅此处:stackoverflow.com/questions/63817805
猜你喜欢
  • 2015-07-11
  • 2020-01-30
  • 2021-05-30
  • 1970-01-01
  • 2020-08-28
  • 1970-01-01
  • 2018-08-22
  • 2020-12-15
  • 1970-01-01
相关资源
最近更新 更多