Sei sulla pagina 1di 3

20/6/2014 Create an ADD-IN with TOOLBARS that run macros

http://www.pptfaq.com/FAQ00031_Create_an_ADD-IN_with_TOOLBARS_that_run_macros.htm 1/3
Support the
PPT FAQ
PPTools
Image Export
converts PowerPoint
slides to high-quality
images.
PPT2HTML exports
HTML even from
PowerPoint 2010 and
2013, gives you full
control of
PowerPoint HTML
output, helps meet
Section 508
accessibility
requirements
Merge Excel data
into PowerPoint
presentations to
create certificates,
awards
presentations,
personalized
presentations and
more
Resize your
presentations quickly
and without
distortion
Language Selector
switches the text in
your presentation
from one language
to another
FixLinks prevents
broken links when
you distribute
PowerPoint
presentations
Shape Styles brings
styles to
PowerPoint. Apply
complex formatting
with a single click.
Prep4PDF preserves
interactivity in
PowerPoint
presentations when
you convert to PDF
Supercharge your PowerPoint productivity with
Previous Back to PPT FAQ Index Next
Create an ADD-IN with TOOLBARS that run macros
Unlike the other Office programs, PowerPoint doesn't have a simple way to
automatically run VBA code when you open a particular PPT file or create a
new document based on a template.
However, if you create an add-in that includes an Auto_Open subroutine,
PowerPoint will run the code in Auto_Open when the add-in loads. It will
also run any code in an Auto_Close subroutine when the add-in is unloaded
manually via the Tools, Addins dialog or when you quit PowerPoint.
Once loaded, an add-in will load itself every time PowerPoint starts up.
Running code once can be useful, but what if you want to make your
macros available at the click of a button? To do that, your add-in has to
create a toolbar and add buttons to it. Each button can be associated with
other macros in your add-in.
So how do you create an add-in that does all this wonderful stuff?
A Must-See Site: PowerPoint MVP Shyam Pillai's PowerPoint Add-in FAQ
You may also want to read this MS Knowledgebase article on creating
PowerPoint add-ins
Here are the basics. This shows you how to create an add-in and a toolbar
with buttons that you can click to run macros. In PowerPoint 2007 and
onward, your toolbar will appear on the Add-ins tab. If you want to create
your own tabs or groups on the ribbon, the process is a bit more
complicated. In that case, PowerPoint MVP John Wilson has a tutorial and
more for you here.
Start with a new PowerPoint file
Start a new PowerPoint file. Press ALT+F11 to start the VB Editor. Choose
Insert, New Module.
Add this code to the new module:
Sub Auto_Open()
Dim oToolbar As CommandBar
Dim oButton As CommandBarButton
Dim MyToolbar As String
' Give the toolbar a name
MyToolbar = "Kewl Tools"
On Error Resume Next
' so that it doesn't stop on the next line if the toolbar's already there
' Create the toolbar; PowerPoint will error if it already exists
Set oToolbar = CommandBars.Add(Name:=MyToolbar, _
Position:=msoBarFloating, Temporary:=True)
If Err.Number <> 0 Then
' The toolbar's already there, so we have nothing to do
Exit Sub
End If
On Error GoTo ErrorHandler
' Now add a button to the new toolbar
Set oButton = oToolbar.Controls.Add(Type:=msoControlButton)
' And set some of the button's properties
With oButton
.DescriptionText = "This is my first button"
'Tooltip text when mouse if placed over button
.Caption = "Do Button1 Stuff"
20/6/2014 Create an ADD-IN with TOOLBARS that run macros
http://www.pptfaq.com/FAQ00031_Create_an_ADD-IN_with_TOOLBARS_that_run_macros.htm 2/3
'Text if Text in Icon is chosen
.OnAction = "Button1"
'Runs the Sub Button1() code when clicked
.Style = msoButtonIcon
' Button displays as icon, not text or both
.FaceId = 52
' chooses icon #52 from the available Office icons
End With
' Repeat the above for as many more buttons as you need to add
' Be sure to change the .OnAction property at least for each new button
' You can set the toolbar position and visibility here if you like
' By default, it'll be visible when created. Position will be ignored in PPT 2007 and later
oToolbar.Top = 150
oToolbar.Left = 150
oToolbar.Visible = True
NormalExit:
Exit Sub ' so it doesn't go on to run the errorhandler code
ErrorHandler:
'Just in case there is an error
MsgBox Err.Number & vbCrLf & Err.Description
Resume NormalExit:
End Sub
Sub Button1()
' This code will run when you click Button 1 added above
' Add a similar subroutine for each additional button you create on the toolbar
' This is just some silly example code.
' You'd put your real working code here to do whatever
' it is that you want to do
MsgBox "Will you PLEASE stop clicking me? I have a headache already!"
End Sub
' That's it. Don't add anything past this point to your code.
Make sure the add-in code compiles
Choose Debug, Compile from the menu bar. This helps ensure that there
are no errors in your code that will later prevent PowerPoint from saving it
as an add-in. If PowerPoint finds any errors, it will stop with the problem
code highlighted. If necessary, correct any problems and keep compiling
again until you get a clean compile ... that is, PowerPoint stops objecting
to things.
SAVE your PPT file
Once you've added the code above and perhaps modified it a bit to suit
your own needs, save the presentation as a PPT file. You'll save it as an
add-in (PPA) file later, but you cannot ... repeat ... CANNOT open and edit
PPA files. If you don't save as a PPT also, you won't be able to edit your
macros.
Create the Add-in
Once you've saved your work safely in PPT format, choose File, Save As and
pick PowerPoint Add-In (*.PPA) from the Files of Type dropdown listbox.
Give it a file name and save.
(If using PPT2007 or later, you may prefer to choose PPAM instead of PPA)
You now have a new PowerPoint Add-in. Until you load it into PowerPoint,
it's not going to do you much good, so the next step is:
Load the Add-in
In PowerPoint 2003 and previous:
Choose Tools, Add-Ins.
In the Add-Ins dialog box, click Add New and browse to the
new Add-in (PPA) file you just created.
Choose it and click OK.
Your new toolbar and buttons should appear.
Close the Add-Ins dialog box.
In PowerPoint 2007:
20/6/2014 Create an ADD-IN with TOOLBARS that run macros
http://www.pptfaq.com/FAQ00031_Create_an_ADD-IN_with_TOOLBARS_that_run_macros.htm 3/3
Click the Office Button in 2007
Click PowerPoint Options in 2007
Click Add-ins
Next to Manage: choose PowerPoint Add-Ins then click Go
Click Add New and browse to your add-in
In PowerPoint 2010:
Click the File tab.
Click Options
Click Add-ins
Next to Manage: choose PowerPoint Add-Ins then click Go
Click Add New and browse to your add-in
In PowerPoint 2003 and earlier, your new toolbar appears in the middle of
the PowerPoint screen. You can drag and dock with the other toolbars if
you like.
In PowerPoint 2007 and later, a new Add-ins tab appears. Click it to see
your toolbar buttons.
Click the buttons on your new toolbar to test your macros.
Enjoy!
Also see Shyam Pillai's PowerPoint Add-in FAQ
See How do I use VBA code in PowerPoint? to learn how to use this
example code.
Did this solve your problem? If so, please consider supporting the PPT FAQ with a
small PayPal donation.
Previous Back to PPT FAQ Index Next
Contents 1995 - 2014 Stephen Rindsberg, Rindsberg Photography, I nc. and members of
the MS PowerPoint MV P team. You may link to this page but any form of unauthorized
reproduction of this page's contents is expressly forbidden.
Espaol Deutsch Franais Portugus I taliano Nederlands Greek Japanese Korean Chinese
Supercharge your PPT Productivity with PPTools
content authoring & site maintenance by
Friday - The A utomatic FA Q Maker
Create an ADD-IN with TOOLBARS that run macros
http://www.pptfaq.com/FAQ00031_Create_an_ADD-IN_with_TOOLBARS_that_run_macros.htm
Last update 28 August, 2013
Created:

Potrebbero piacerti anche