How to auto execute a macro when opening a Powerpoint presentation?

故事扮演 提交于 2019-11-28 02:01:36

Try to write this function.

Sub Auto_Open()
   MsgBox("welcome")
End Sub

Replace the msgbox in your code.

While Auto_Open doesn't run in a PowerPoint presentation, you can fake it. Add a CustomUI part to the presentation, then use the CustomUI OnLoad callback to run code when the presentation opens. The CustomUI part needs no more than just the CustomUI tags.

Get the Custom UI Editor from here: http://openxmldeveloper.org/articles/customuieditor.aspx

Open the presentation in the Custom UI Editor. Insert a CustomUI part from the Insert menu:

Now enter some simple RibbonX code, like this:

<customUI xmlns="http://schemas.microsoft.com/office/2006/01/customui" 
onLoad="MyOnloadProcedure" >
</customUI>

Now write your on-open procedure:

Sub MyOnloadProcedure()
    MsgBox "Hello"    
End Sub

If you have both this and the Auto_Open procedure in an add-in, Auto_Open runs first.

Full disclosure: while I thought of using this approach and have used it in Excel, I waited until I first encountered it on the PPT Alchemy web site: Run Code When PowerPoint Opens.

I am using PowerPoint 2016 so I can't speak for earlier versions although the documentation indicates this works for PowerPoint versions 2016, 2013, 2010, 2007. This method DOES NOT require any add-ins or application hooks to handle events.

Reference link: https://support.office.com/en-us/article/command-line-switches-for-microsoft-office-products-079164cd-4ef5-4178-b235-441737deb3a6?ocmsassetID=HA010153889&CTT=1&CorrelationId=ea39d200-aa81-4d6e-8302-afff4c65859e&ui=en-US&rs=en-US&ad=US#ID0EAABAAA=PowerPoint,_PowerPoint_Viewer)

Start PowerPoint from command line and use the /M switch to have PowerPoint run a specified macro when it starts a named presentation file.

The easiest way to do this is to create a shortcut to the PowerPoint application. Then go to the Properties window for the shortcut and select the Shortcut tab. Next, add the /M switch, your presentation file name (including path), and the name of the macro to run (case sensitive and must be part of the presentation) to the end of the Target field. Double-click the shortcut and voila!

Example: My presentation is C:\myPPTpres.pptm and the macro is Run_Slide_Show so I will need to add /M "C:\myPPTpres.pptm" "Run_Slide_Show" to the end of the existing text in the Target field.

"C:\Program Files (x86)\Microsoft Office\root\Office16\POWERPNT.EXE" /M "C:\myPPTpres.pptm" "Run_Slide_Show"

Make sure you are running a macro-enabled version of the presentation (in 2016 it has the .pptm extension.)

Important note if you are trying to start a slide show automatically using the macro. Add a delay of one or more seconds at the very beginning of your macro to allow the application to finish its startup sequence. If you do not the slideshow will start but the application will steal focus as it completes its startup, pushing your slideshow to the background.

Assuming the type of macro to be Auto Events related such as Auto_open(), Auto_close(), Auto_print() etc., for powerpoint to execute macros on opening powerpoint file we require Powerpoint Add-Ins to be included or other option could be to save the file as .ppa or .ppam.

For more details and to download the AutoEvents zip file refer the link: http://skp.mvps.org/autoevents.htm

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!