VBA运行时错误438

时间:2017-11-13 09:44:31

标签: excel vba excel-vba powerpoint-vba

我正在尝试编写代码以从excel VBA打开PPTX文件并更新ppt中的链接。

下面是我已经获得的代码,但是当代码尝试更新链接时,我正在获取

  

运行时错误438对象不支持此属性或方法

Sub kunal()

Dim PPObj As Object

Set PPObj = CreateObject("PowerPoint.application")

With PPObj      
    .Presentations.Add

    .Presentations.Open Filename:="Y:\Desktop\Month End\One_Shot\Template AVP Report Package\ABD-OME SDeeson.pptx"

    .Visible = True    
    .UpdateLinks    
    .Presentation.Save

    .Quit    
    Set PPObj = Nothing    
End With

End Sub

2 个答案:

答案 0 :(得分:0)

438表示您正在尝试访问不存在的方法或对象的属性。因此,您需要一个表示对象而不是应用程序对象来更新链接。

试试这样:

Option Explicit

Sub kunal()

    Dim PPObj As Object
    Dim pptPresentation As Object

    Set PPObj = CreateObject("PowerPoint.application")
    Set pptPresentation = PPObj.presentations.Open("C:\test.pptx")

    With PPObj

        .presentations.Add

        .Visible = True
        pptPresentation.UpdateLinks
        pptPresentation.Save
        pptPresentation.Close

        .Quit
        'Set PPObj = Nothing - No need for this
    End With

End Sub

Application Object MSDN

Presentation Object MSDN

答案 1 :(得分:0)

首先,如果在以下行打开现有演示文稿,则不确定为什么需要.Presentations.Add

第二次.UpdateLinks行是Presentation的属性,而不是PowerPoint.Application

<强>代码

Option Explicit

Sub kunal()

Dim ppApp As Object
Dim ppPres As Object

Set ppApp = CreateObject("PowerPoint.application")

With ppApp
    .Visible = True

'    .Presentations.Add ' <-- not sure why you need to open a new Presentation ?
    Set ppPres = .Presentations.Open(Filename:="Y:\Desktop\Month End\One_Shot\Template AVP Report Package\ABD-OME SDeeson.pptx")

    ppPres.UpdateLinks
    ppPres.Save
    ppPres.Close

    .Quit
End With

Set ppPres = Nothing
Set ppApp = Nothing

End Sub