首页 > 解决方案 > 在 PowerPoint 中使用 VBA 更新 msoLinkedOLEObject 时如何绕过 Excel 弹出窗口

问题描述

我有一个包含多个 Excel 电子表格链接的 PowerPoint。我想用宏更新链接对象。下面的宏将生成 2 种类型的弹出窗口。在我的情况下,每个链接都会出现弹出窗口以更新大约 30 次。单击取消将允许宏继续。1) Microsoft Excel 已停止工作(关闭程序) 2) 文件正在使用(只读、通知或取消选项)

有没有办法绕过这些消息?

Sub linkupdate()

Dim osld  As Slide
Dim oshp  As Shape

    For Each osld  In ActivePresentation.Slides
         For Each oshp  In osld.Shapes
            If oshp.Type = msoLinkedOLEObject Then
              If LCase(oshp.LinkFormat.SourceFullName) Like "*defect 95R*" Then
                   oshp.LinkFormat.AutoUpdate = ppUpdateOptionManual
                    oshp.LinkFormat.Update
                    oshp.LinkFormat.AutoUpdate = ppUpdateOptionAutomatic
              End If
            End If
         Next
      Next
  MsgBox "Finished updating Charts", , "Update Complete"
End Sub

标签: vbapowerpoint

解决方案


此代码阻止了以下警报的发生
1) Microsoft Excel 已停止工作(关闭程序)
2) 文件正在使用(只读、通知或取消选项)

完成宏弹出后,用户可能需要一分钟才能控制 PowerPoint。我假设 excel 警报正在后台关闭,因为有超过 30 个链接图表。

我是 VBA 的新手,所以这段代码可能效率不高。

Sub linkUpdate()

Const xFile = "C:\temp\defect 95R.xlsx"
Dim pptPresentation As Presentation
Dim osld As Slide
Dim oshp As PowerPoint.Shape
Dim xlApp As Excel.Application

Set xlApp = New Excel.Application
xlApp.Visible = True
xlApp.Workbooks.Open xFile, ReadOnly:=True, Notify:=False
xlApp.Workbooks.Application.DisplayAlerts = False

Set pptPresentation = ActivePresentation
    'Loop through each slide in the presentation
    For Each osld In pptPresentation.Slides
        'Loop through each shape in each slide
         For Each oshp In osld.Shapes
           'Find out if the shape is a msoLinkedOLEObject type=10
            If oshp.Type = msoLinkedOLEObject Then
              'Only update shape if file name contains defect 95r
              If LCase(oshp.LinkFormat.SourceFullName) Like "*defect 95r*" Then
                   oshp.LinkFormat.AutoUpdate = ppUpdateOptionManual
                   xlApp.Workbooks.Application.DisplayAlerts = False
                    oshp.LinkFormat.Update
                    oshp.LinkFormat.AutoUpdate = ppUpdateOptionAutomatic
              End If
            End If
         Next
      Next

xlApp.Workbooks.Close
xlApp.Workbooks.Application.Quit
Set xlApp = Nothing

MsgBox "Finished updating Charts", , "Update Complete"
End Sub

推荐阅读