【问题标题】:How to close an active presentation that is open in MS PowerPoint in VB.net如何关闭在 VB.net 的 MS PowerPoint 中打开的活动演示文稿
【发布时间】:2022-10-06 14:42:58
【问题描述】:

我们为 MS PowerPoint 2019 设计了 ​​Microsoft 插件,用 VB.net 编写。在那里,我们采用了一个计数器 (pptCounter),每当一个演示文稿(现有的或新的)打开时,它就会增加。所以我们想在计数器大于 1 后删除这个打开的演示文稿。但是它并没有关闭打开的文件。

我们在下面使用了这个给定的 sn-p :

Private Sub WorkWithDocument(ByVal Doc As Microsoft.Office.Interop.PowerPoint.Presentation) Handles Application.PresentationOpen

 If (pptCounter > 1) Then

*Globals.Connect.Application.ActivePresentation.Close()*

End If

End Sub

以下是有关我正在工作的环境的更多信息:

操作系统: 微软视窗 10 专业版

代码编辑器: 视觉工作室 2019

技术: Vb.net(.Net Framework 4.8)

微软办公版: 2019(32 位) : Microsoft Windows 10 Pro

【问题讨论】:

    标签: vb.net powerpoint vsto office-addins powerpoint-addins


    【解决方案1】:

    PowerPoint 演示文稿的实例作为参数传递给该方法,因此您需要在代码中使用该对象而不是获取活动对象:

    Private Sub WorkWithDocument(ByVal Doc As Microsoft.Office.Interop.PowerPoint.Presentation) Handles Application.PresentationOpen
    
      If (pptCounter > 1) Then
        Doc.Close()
      End If
    
    End Sub
    

    在某些情况下,在 Open 事件处理程序中关闭演示文稿是不正确的。因此,我建议让事件处理程序完成其工作,然后在事件处理程序完成后立即关闭刚刚打开的演示文稿。您可以在 VBA 中使用计时器来安排延迟操作。

    【讨论】:

      【解决方案2】:

      在同一个处理程序中关闭打开的文档是不正确的:Application.PresentationOpen因为处理程序可能仍在进行某些处理,所以添加一些额外的处理程序打开演示文稿后执行。

      例如。就像您分享了以下方法一样

      Private Sub WorkWithDocument(ByVal Doc As Microsoft.Office.Interop.PowerPoint.Presentation) Handles Application.PresentationOpen
      

      启动时必须有处理程序AddHandler Application.NewPresentation, AddressOf WorkWithDocument

      现在做一件事添加额外的处理程序AddHandler Application.NewPresentation, AddressOf AfterPresentationOpen 并调用下面的方法来关闭演示文稿。以供参考:

      Private Sub AfterPresentationOpen(ByVal Doc As Microsoft.Office.Interop.PowerPoint.Presentation) Handles Application.AfterPresentationOpen
        If (pptCounter > 1) Then
            Doc.Close()
        End If
      End Sub
      

      打开演示文稿后肯定会调用它,因此很有可能关闭演示文稿。

      【讨论】:

        猜你喜欢
        • 2020-11-03
        • 1970-01-01
        • 1970-01-01
        • 2012-07-03
        • 2023-02-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-05-23
        相关资源
        最近更新 更多