2008年7月24日星期四

How do I use VBA code in PowerPoint?

You'll find lots of useful snippets of VB or VBA code on the internet and in newsgroups, but unless you know how to get the code into PowerPoint, you can't very well use them. Here's a quick tutorial on the subject.

Where in PPT do I put the code?
Here's how you use VBA code in PowerPoint. First, a bit of prep work to make sure your security settings don't get in the way:

1.Open a PowerPoint presentation.

2.Set Macro Security to Medium or lower. If Security's set higher, PowerPoint disables your macros when you open the file that contains them. With security set to Medium, PowerPoint asks whether to enable macros when you open a file that contains them.
•PowerPoint 2003 or previous: Choose Tools, Macro, Security and set security to Medium.
•PowerPoint 2007: Click the Office button (the great big circular thing, upper left of the PPT screen), click PowerPoint options at the bottom of the menu-like thing that appears, click Trust Center on the left of the PowerPoint Options dialog box, click Trust Center Settings on the Trust Center dialog, click Macro Settings on the left of the dialog then choose Enable All Macros. Whew. Close any open dialog boxes.
•PowerPoint Mac: You don't need to do anything about macro security but it's a good idea to make sure that it's enabled. Choose PowerPoint, Preferences, General and use the "enable macro virus protection" checkbox to turn the macro warning on.

3.Start the VBA editor
•PowerPoint 2003 or previous: Press ALT+F11 to start PowerPoint's VBA editor. Or choose View, Toolbars, Visual Basic to get shortcut buttons to the editor and for running macros. Or choose Tools, Macro, Visual Basic Editor to start the editor.
•PowerPoint 2007: Press ALT+F11 to start the VBA editor. Or click the Office button, choose PowerPoint Options, click Popular and put a check next to "Show Developer tab in Ribbon". Close the options dialog box, click the Developer tab then click "Visual Basic" to start the editor.
•Mac PowerPoint: Use View, Toolbars, Visual Basic then click the Visual Basic Editor button on that toolbar.

4.In the VBA editor, make sure that your presentation is highlighted in the left-hand pane. Choose Insert, Module from the menu bar to insert a new code module into your project (project = presentation in VBAspeak). Modules are one of the several "containers" that can hold VBA code.

5.If your code snippet already starts with "Sub XXX()" and ends with "End Sub", simply click in the new module you just inserted and paste in the code. Otherwise, you'll have to type in "Sub XXX" (where XXX is the name you want to give the subroutine (aka "macro"). When you press Enter, PowerPoint adds the parentheses and End Sub for you automatically. Then position the cursor between the Sub XXX and End Sub lines and paste in your code.

6.To make sure there are no serious syntax problems with the code, choose Debug, Compile from the menu bar. If there's a problem, you'll see a message explaining (well ... explaining in a geeky, obtuse way that usually won't make any sense to you) what VBA doesn't like about the code. Click OK and the problem line will be highlighted for you. Fix it and compile again until you get no error messages.

7.Now click the Run button (a right-facing arrowhead icon), choose Run, Run Sub/User Form from the menu bar or press F5 to run your code.

8.Once your code's working properly, you can run it directly from within PowerPoint without having to start the VBA editor. Choose Tools, Macros, Macros to get a list of available macros ( = subroutines, remember?) in the current presentation. Highlight the one you want to run and click Run (or simply doubleclick the one you want). You can also view and run macros from other open presentations; choose the presentation where your macro is stored from the Macros In dropdown listbox.

You can also use Tools, Customize to create your own toolbar and assign a macro to it. This won't work reliably unless the PPT file that contains the macros is open in PPT at the same time, but it's a handy way to invoke a macro with one click rather than having to chase through the Tools, Macros menu to get there.

Note: Code that compiles may still not do what you expect it to do when you run it, or it may still produce errors. If it errors out, VBA will show you a message box that lets you either End the code or Debug it. If you choose Debug, you'll be returned to the VBA editor with the problem line highlighted in yellow so you can correct the problem.

If you want to run your code without having to load the PowerPoint file it's in every time, see Create an ADD-IN with TOOLBARS that run macros

Housekeeping
As you accumulate additional code snippets, you'll want to figure out where you'll store them.

You could print them, but then you'd have to re-type later (and possibly introduce errors that keep the code from running correctly).

Instead, when you see some useful-looking code, select it, copy it to the clipboard, then start Notepad (Windows) or TextEdit (Mac) and paste the code into it. Then save the file, possibly to a special folder you've created just for VB/VBA code snippets. Getting the code into PowerPoint is then a simple matter of opening the Notepad file, selecting the text and copying it into PowerPoint.

If the code turns red when you paste it into a module, the likely cause is extra characters (invisible) copied from the web browser or newsgroup reading program. Delete everything between line breaks and get rid of spaces before indented items to correct this problem.

----Quoted from http://www.pptfaq.com/FAQ00033.htm

Related links:
Powerpoint & DVD Knowledge Centre

没有评论: