【问题标题】:Powerpoint VBA to update links on excel files open by someone else without alertsPowerpoint VBA 更新其他人打开的 excel 文件上的链接而不会发出警报
【发布时间】:2014-12-27 19:51:06
【问题描述】:

我有一个链接到多个大型 Excel 电子表格的 powerpoint 演示文稿。我想更新链接,以便更新我的整个演示文稿。但是,如果单击Update All Link,我的PowerPoint 崩溃,可能是因为链接的excel 文件很大。所以我必须手动更新每个链接,这样它就不会崩溃。

下面的这个宏将循环每个链接对象并更新其链接,但是,如果文件当前打开,如果我想取消、打开只读或通知,它将弹出警报。设置为 false 的显示警报将不会对它做任何事情。

Sub linkupdate()
    Application.DisplayAlerts = False 
    Dim osld As Slide
    Dim oshp As Shape
    For Each osld In ActivePresentation.Slides
        osld.Select
        For Each oshp In osld.Shapes
            If oshp.Type = msoLinkedOLEObject Then
                oshp.Select
                oshp.LinkFormat.Update
            End If
        Next oshp
    Next osld
    Application.DisplayAlerts = True
End Sub

我发现 powerpoint 在它下面打开了一个不可见的 excel 应用程序来更新链接,但是我无法用我的 vba 抓取这个打开的实例并告诉它以只读方式打开链接。

但是,如果文件被其他人打开,我希望不会发生这种情况,但是我希望它以只读方式自动打开文件以进行更新。

【问题讨论】:

    标签: vba powerpoint


    【解决方案1】:

    查看您的代码,我假设所有这些都是在 PowerPoint VBA 编辑器中完成的。话虽如此,让我解释一下为什么您会从 Excel 应用程序中获得弹出窗口。当你写这行代码时:

    Application.DisplayAlerts = False 
    

    您指的是 PowerPoint 应用程序,您的意思是指 Excel 应用程序,因为我假设弹出窗口正在那里发生。为了解决这个问题,我们需要创建对 Excel 应用程序的引用。如果我添加以下代码我们可以关闭 Excel 应用程序的显示

    'Declare Excel Variables
    Dim xlApp As Excel.Application
    Dim xlWorkBook As Excel.Workbook
    
    'Create a new Excel Application, make it invisible, set the Excel Display alerts to False.
    Set xlApp = New Excel.Application
        xlApp.Visible = False
        xlApp.DisplayAlerts = False
    

    既然我们已经处理了弹出窗口,让我们来解决您的链接问题。为了解决链接问题,我们将在后台打开链接所引用的 Excel 工作簿。这样更新链接会更快。 如果我们不打开工作簿,我有一个实例,更新链接过程很容易需要超过 5 分钟。

    但是,如果对象是链接图表,则获取正确的文件名可能会有点困难。 我们需要解析文件,以便它删除工作表名称和图表名称。我们是这样做的:

    'Get the Source File of the shape.
     SourceFile = PPTShape.LinkFormat.SourceFullName
    
    'We may need to parse the Source file because if it's linked to a chart, for example, we can get the following:
    'C:\Users\NAME\ExcelBook.xlsx!Chart_One!
    'We want it to look like the following:
    'C:\Users\NAME\ExcelBook.xlsx
    
    'This will parse the source file so that it only includes the file name.
    Position = InStr(1, SourceFile, "!", vbTextCompare)
    FileName = Left(SourceFile, Position - 1)
    

    接下来,我们要打开工作簿,更新链接,然后关闭工作簿。所以我们将添加这部分代码:

    'This will open the file as read-only, and will not update the links in the Excel file.
    Set xlWorkBook = xlApp.Workbooks.Open(FileName, False, True)
    
        'Update the link
        PPTShape.LinkFormat.Update
    
    'Close the workbook and release it from memory.
    xlWorkBook.Close
    Set xlWorkBook = Nothing
    

    为了更新所有不同链接的 OLEObjects 的链接,您的代码将呈现出这样的外观。

    Sub UpdateLink()
    
        'Declare PowerPoint Variables
        Dim PPTSlide As Slide
        Dim PPTShape As Shape
    
        'Declare Excel Variables
        Dim xlApp As Excel.Application
        Dim xlWorkBook As Excel.Workbook
    
        'Create a new Excel Application, make it invisible, set the Excel Display alerts to False.
        Set xlApp = New Excel.Application
            xlApp.Visible = False
            xlApp.DisplayAlerts = False
    
        'Loop through each slide in the Presentation.
        For Each PPTSlide In ActivePresentation.Slides
    
            'Loop through Each Shape in the slide
            For Each PPTShape In PPTSlide.Shapes
    
                'If the Shape is a linked OLEObject.
                If PPTShape.Type = msoLinkedOLEObject Then
    
                    'Get the Source File of the shape.
                    SourceFile = PPTShape.LinkFormat.SourceFullName
    
                    'We may need to parse the Source file because if it's linked to a chart, for example, we can get the following:
                    'C:\Users\NAME\ExcelBook.xlsx!Chart_One!
                    'We want it to look like the following:
                    'C:\Users\NAME\ExcelBook.xlsx
    
                    'This will parse the source file so that it only includes the file name.
                    Position = InStr(1, SourceFile, "!", vbTextCompare)
                    FileName = Left(SourceFile, Position - 1)
    
                    'This will open the file as read-only, and will not update the links in the Excel file.
                    Set xlWorkBook = xlApp.Workbooks.Open(FileName, False, True)
    
                        'Update the link
                        PPTShape.LinkFormat.Update
    
                    'Close the workbook and release it from memory.
                    xlWorkBook.Close
                    Set xlWorkBook = Nothing
    
                End If
    
            Next PPTShape
        Next PPTSlide
    
        'Close the Excel App & release it from memory
        xlApp.Quit
        Set xlApp = Nothing
    
    End Sub
    

    在将对象粘贴到 PowerPoint 幻灯片时也要小心,如果您使用 VBA 来实现此目的,有时它可能会错误地粘贴链接信息!当您去更新链接时,什么都不会改变,因为链接不正确并且实际上并没有引用您要更新的项目。

    【讨论】:

    • 请在运行此 VBA 代码之前检查您的任务管理器并终止所有 Excel 实例。
    猜你喜欢
    • 1970-01-01
    • 2014-05-15
    • 2012-11-30
    • 2011-08-23
    • 2019-01-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-04
    相关资源
    最近更新 更多