vba PowerPoint 2007 - 如何使用 Application.NewPresentation 事件处理程序?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3724094/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 12:05:38  来源:igfitidea点击:

PowerPoint 2007 - How to use Application.NewPresentation event handler?

vbaevent-handlingpowerpointpowerpoint-vbapowerpoint-2007

提问by Kenny Bones

so I've found that PowerPoint 2007 does indeed support event handlers on the Application level. For example Application.NewPresentationor even Application.AfterNewPresentation

所以我发现 PowerPoint 2007 确实支持应用程序级别的事件处理程序。例如Application.NewPresentation甚至Application.AfterNewPresentation

It's described here http://msdn.microsoft.com/en-us/library/ff745073.aspx

它在这里描述http://msdn.microsoft.com/en-us/library/ff745073.aspx

But the real question is, how do I use these? Where do I use them? To tell you my scenario, what I want is to insert dynamic text into a few textboxes. I have the macro code to do this and it's working exactly as I want it to. But I need the macro to fire once a new presentation is created from a potm template. And only on that event. Just like it does in Word 2007.

但真正的问题是,我该如何使用这些?我在哪里使用它们?告诉你我的场景,我想要的是在几个文本框中插入动态文本。我有执行此操作的宏代码,它完全按照我的意愿工作。但是,一旦从 potm 模板创建新的演示文稿,我就需要宏来触发。并且仅在该事件上。就像它在 Word 2007 中所做的那样。

Where do I start? I tried to just create a sub looking like this and saving it as a potm file and open a new presentation based on that template. And nothing happened.

我从哪里开始?我试图创建一个看起来像这样的子并将其保存为一个potm文件并基于该模板打开一个新的演示文稿。什么也没发生。

Private Sub App_NewPresentation(ByVal Pres As Presentation)
    MsgBox "Running!"
End Sub

Edit: It is possible to open any Office 2007 file with an XML editor. I use the Custom UI Editor For Microsoft Office and in that I add a Office 2007 Custom UI.XML part following the guide presented here: http://www.pptalchemy.co.uk/PowerPoint_Auto_Open_Code.html

编辑:可以使用 XML 编辑器打开任何 Office 2007 文件。我使用 Microsoft Office 的自定义 UI 编辑器,并按照此处提供的指南添加了 Office 2007 自定义 UI.XML 部分:http://www.pptalchemy.co.uk/PowerPoint_Auto_Open_Code.html

But I run into problems when PowerPoint creates a new presentation based on that template. Opening the template in itself works just fine. The event handler is there and the code is run beautifully. But a new presentation based on it? No way, the handler is there as well. But it says it cannot find the macro. Even though the macro is in the new presentation as well since I can open Visual Basic Editor and find the macro and then run it. It's just the autopart that doesn't seem to be working like it should.

但是当 PowerPoint 基于该模板创建新的演示文稿时,我遇到了问题。打开模板本身就可以正常工作。事件处理程序在那里,代码运行得很漂亮。但是基于它的新演示文稿?没办法,处理程序也在那里。但它说它找不到宏。即使宏也在新的演示文稿中,因为我可以打开 Visual Basic 编辑器并找到宏然后运行它。只是汽车零件似乎没有像它应该的那样工作。

回答by Todd Main

The onlyway to create an auto macro in PowerPoint VBA is to have your file as an Add-in (.ppa or .ppam - not .pptx/.pptm/.potm/etc). And the way to create it is:

在 PowerPoint VBA 中创建自动宏的唯一方法是将文件作为加载项(.ppa 或 .ppam - 而不是 .pptx/.pptm/.potm/等)。创建它的方法是:

  1. Create a class module. At the top (after any Option XXX), put Public WithEvents App As Applicationand then put your routine above below that.
  2. Create a module of any name and put:

    Dim X As New Class1
    Sub AutoOpen()
        Set X.App = Application
        ''# Code to create new presentation
    End Sub
    
  1. 创建一个类模块。在顶部(在 any 之后Option XXX),放置 Public WithEvents App As Application然后将您的例程置于其下方。
  2. 创建一个任何名称的模块并放置:

    Dim X As New Class1
    Sub AutoOpen()
        Set X.App = Application
        ''# Code to create new presentation
    End Sub
    

Again, this will not work from your requirement of .potm. Another way that you can consider has been depreciated, but it still works, which is to create a Wizard file.

同样,这不会满足您对 .potm 的要求。您可以考虑的另一种方法已折旧,但它仍然有效,即创建向导文件。

回答by Meihua

The macro in the presentation that was created has no way to run, as the auto_open macro only works with addins. Based on the way you're doing it, you'd have to reload the ribbon to kick the event in your new presentation you want to run.

创建的演示文稿中的宏无法运行,因为 auto_open 宏仅适用于插件。根据您的操作方式,您必须重新加载功能区才能在要运行的新演示文稿中启动该事件。