Excel VBA无法在Office 2016中保存为嵌入式PowerPoint演示文稿

我们在Excel中嵌入pptx文件,然后使用Excel VBA代码(如下所示)打开,然后将ptx文件另存为用户的驱动器,然后以编程方式修改基于Excel计算的pptx内容.

下面的Excel VBA代码可以很好地控制PowerPoint 2010和2013,但不再适用于PowerPoint 2016.

注意:我有类似的Word代码,它适用于Word 2016(和以前的版本).

Sub OpenCopyOfEmbeddedPPTFile() 'works with Office 2010 and 2013, but not on Office 2016
    Dim oOleObj As OLEObject
    Dim PPTApp As Object
    Dim PPTPres As Object
    Dim sFileName As String
    Set oOleObj = ActiveSheet.Shapes("PPTObj").OLEFormat.Object 'name of the embedded pptx object
    Set PPTApp = CreateObject("Powerpoint.Application")
    PPTApp.Visible = True
    sFileName = "C:\Users\Me\Documents\testPPT.pptx"
    OleObj.Verb Verb:=xlVerbOpen 'it opens successfully
    Set PPTPres = oOleObj.Object
    PPTPres.SaveAs Filename:=sFileName 'fails here (in Office 2016)
    PPTPres.Close
    GetObject(, "PowerPoint.Application").Presentations.Open sFileName
    'code to modify the Presentation (copy of the embedded pptx) based on Excel calculations
End Sub

错误:
运行时错误’-2147467259(80004005)’:
Presentation.SaveAs:PowerPoint保存文件时发生错误.

此外,以下PowerPoint VBA(不是Excel VBA)适用于普通文档(未嵌入Excel),但在打开的嵌入式pptm中运行时失败.在2013年和2010年嵌入式pptm工作正常.

ActivePresentation.SaveAs FileName:="C:\Users\Me\Documents\testPPT.pptm"

错误:
运行时错误’-2147467259(80004005)’:演示文稿(未知成员):PowerPoint保存文件时发生错误.

Windows操作系统版本似乎并不重要.在Office for Mac上不起作用.

有任何方法可以解决或解决此错误吗?或者是否有另一种方法来做同样的事情(修改嵌入式pptx的副本,以便嵌入式pptx不被修改)?或者此错误仅发生在我们的Office 2016 PC上?

最佳答案 对嵌入式演示文稿使用SaveAs或SaveCopyAs时,PowerPoint 2016似乎失败.

解决方法是打开演示文稿,创建新演示文稿,然后将内容从嵌入演示文稿复制到新演示文稿.然后,您可以关闭嵌入的演示文稿,并根据需要保存新演示文稿.

我已经演示了复制幻灯片,但您可能需要以编程方式复制BuiltInDocumentProperties和其他非幻灯片内容.

Option Explicit

Sub OpenCopyOfEmbeddedPPTFile() 'works with Office 2010 and 2013, but not on Office 2016
    Dim oOleObj As OLEObject
    Dim PPTApp As Object
    Dim PPTPres As Object
    Dim PPTNewPres As Object
    Dim sFileName As String
    Set oOleObj = ActiveSheet.Shapes("PPTObj").OLEFormat.Object 'name of the embedded pptx object

    oOleObj.Verb 3
    Set PPTPres = oOleObj.Object

    Set PPTApp = PPTPres.Application
    PPTApp.Visible = True

    'We can't save the embedded presentation in 2016, so let's copy the clides to a new presentation
    Set PPTNewPres = PPTApp.Presentations.Add
    PPTPres.Slides.Range.Copy
    PPTNewPres.Slides.Paste

    'We may need to copy other things, like BuiltInDocumentProperties
    'TODO

    'Close the original
    PPTPres.Close

    sFileName = "C:\Users\Me\Documents\testPPT121.pptx"
    sFileName = "C:\users\andrew\desktop\testPPT12111-FOOJANE.pptx"
    PPTNewPres.SaveAs sFileName

    'code to modify the Presentation (copy of the embedded pptx) based on Excel calculations

    'Quit PPT
    'PPTNewPres.Close
    'PPTApp.Quit

End Sub
点赞