如何在 VBA 中创建一个在加载相应的加载项时执行的 Sub?

发布于 2024-08-22 08:16:54 字数 89 浏览 7 评论 0原文

我在 VBA 中有一个子例程,当我们打开 PowerPoint 演示文稿时执行该子例程,但我想在加载外接程序时执行该子例程。

我怎样才能做到这一点?

I have a subroutine in VBA that is executed when we open a PowerPoint presentation but I want to execute that Sub when an Add-in is loaded instead.

How can I do that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

口干舌燥 2024-08-29 08:16:54

如果我理解正确,您有一个加载项,您希望仅在加载加载项时运行现有幻灯片的子例程。

如果是这样,以下是有关如何执行此操作的说明:

  1. 创建幻灯片,将其另存为
    “Presentation3.pptm”(启用宏
    2007年PPT)。打开VBE并输入
    在以下代码中:

    Sub AddText()
    昏暗的演示文稿
    设置 p = ActivePresentation
    暗淡的形状
    设置 sh = p.Slides(1).Shapes.AddTextbox(msoTextOrientationHorizo​​ntal, 100, 100, 100, 100)
    sh.TextFrame.TextRange.Text = "你好"
    结束子
    
  2. 立即创建一个加载项。创建另一个
    甲板,转到VBE并将其放入
    任何模块:

    子 Auto_Open()
    昏暗的演示文稿
    设置 p = 演示文稿("Presentation3")
    Application.Run (p.Name & "!AddText")
    结束子
    
  3. 现在将此加载项另存为 PowerPoint
    加载项 (.ppam)。安装并加载
    插件(如果它再次尝试卸载/加载
    不触发),你应该有
    在活动上创建的文本框
    演示文稿。

注意#2:Application.Run (p.Name & "!AddText")。表示名称(包括扩展名)和“!”需要使用子例程的名称才能在另一个演示文稿中运行宏。

If I'm understanding you correctly, you have an add-in that you want to run the sub-routine of an existing slide deck only when the addin is loaded.

If so, here are instructions on how to do this:

  1. Create a slide deck, save it as
    "Presentation3.pptm" (macro-enabled
    PPT for 2007). Open the VBE and put
    in the following code:

    Sub AddText()
    Dim p As Presentation
    Set p = ActivePresentation
    Dim sh As Shape
    Set sh = p.Slides(1).Shapes.AddTextbox(msoTextOrientationHorizontal, 100, 100, 100, 100)
    sh.TextFrame.TextRange.Text = "hello there"
    End Sub
    
  2. Create an add-in now. Create another
    deck, go to the VBE and put this in
    any module:

    Sub Auto_Open()
    Dim p As Presentation
    Set p = Presentations("Presentation3")
    Application.Run (p.Name & "!AddText")
    End Sub
    
  3. Now save this add-in as a PowerPoint
    Add-in (.ppam). Install and load the
    addin (try unload/load again if it
    doesn't trigger) and you should have
    a textbox created on the active
    presentation.

Notice in #2 : Application.Run (p.Name & "!AddText"). The presenation name (including extention) and "!" with the sub routine's name are required to run a macro in another presentation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文