How to create a Sub in VBA that is executed when its correspondent Add-In is loaded?
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.
开发者_Python百科How can I do that?
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:
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
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
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.
精彩评论