Add-Ins: PowerPoint s AutoOpen And AutoClose


Add-Ins: PowerPoint's AutoOpen And AutoClose

Now that Larry has his full set of macros, he would like to have the toolbar always available. To do this, he needs to turn the macros and the toolbar into a PowerPoint add-in.

An add-in is a special type of PowerPoint file. It contains no slides, only compiled macro code and the toolbars to run that code. Whenever you create an add-in, save two versions of the add-in file. The first version is a regular PowerPoint file with the macro code and any slides you need. The second version is the add-in itself. When you create the add-in file, you will be unable to edit anything within the file. In order to make changes or additions in the future, you need have a regular PowerPoint file with the code and slides where you can make the changes.

In Larry's case, part of the reason for splitting the code off into an add-in is to separate the code from his static presentation. Once the code has been split, he can use it on any presentation file that has a title slide with company contact information and a content slide with goals information. By using the add-in, he doesn't have to worry about always starting with the same presentation file, he just has to be sure the file he is using is set up to enable the macros to run.

So, how do we turn our macros into an add-in? Once the macros are finished, it is a two-step process:

  1. Create Auto Open and Auto Close routines that will load and unload the toolbars

  2. Create and test the add-in file

Create AutoOpen And AutoClose Routines

Many Office applications have the ability to create macros that run whenever the application is opened or closed. These macros can be stored in the default template. In PowerPoint, you can create these same macros, but they must be placed in an add-in before PowerPoint will run them.

When the add-in is loaded, it won't automatically run or load toolbars unless an AutoOpen macro is created. In the same manner, if you don't close the add-in when you close PowerPoint, you won't be able to use it next time PowerPoint opens. To get around this problem, you need to create an AutoClose macro that closes the add-in and removes any toolbars it has created.

These macros can't be recorded, as they aren't action-based macros. They have to be hand-coded . Sounds like a real pain, huh? Lucky for you, PowerPoint MVP Steve Rindsberg agrees. He has created a page on the FAQ containing the exact code needed to create these two macros. In addition, the page contains links to two other great resources on creating add-ins. Read the pages and you will fill in all the blanks on turning the macro into an add-in. The FAQ entry can be found at

http://www.rdpslides.com/pptfaq/FAQ00031.htm

Larry's AutoOpen And AutoClose

After Larry read through the process given in the FAQ, he added these two macros to his PowerPoint file:

 Sub Auto_Open()     Dim oToolbar As CommandBar     Dim oButton As CommandBarButton     Dim pButton As CommandBarButton     Dim qButton As CommandBarButton     Dim rButton As CommandBarButton     Dim sButton As CommandBarButton     Dim LarrysToolbar As String     ' Give the toolbar a name     LarrysToolbar = "UpdateClientInfo"     ' First, delete the toolbar if it already exists     On Error Resume Next ' so that it doesn't stop on the next line if the toolbar doesn't exist     Application.CommandBars(LarrysToolbar) .Delete     On Error GoTo errorhandler ' turns error handling back on     ' Build the command bar     Set oToolbar = CommandBars.Add(Name:=LarrysToolbar, Position:=msoBarFloating, Temporary:=True)     ' Now add a button to the new toolbar     Set oButton = oToolbar.Controls.Add(Type:=msoControlButton)     Set pButton = oToolbar.Controls.Add(Type:=msoControlButton)     Set qButton = oToolbar.Controls.Add(Type:=msoControlButton)     Set rButton = oToolbar.Controls.Add(Type:=msoControlButton)     Set sButton = oToolbar.Controls.Add(Type:=msoControlButton)     ' And set some of the button's properties          With sButton         .DescriptionText = "Run all the macros to update a presentation" 'Tooltip text when mouse if placed over button     .Caption = "UpdateToNewClient" 'Text if Text in Icon is chosen     .OnAction = "UpdateToNewClient" 'Runs the Sub UpdateToNewClient() code when clicked     .Style = msoButtonIcon ' Button displays as icon, not text or both     .FaceId = 22     End With     ' And set some of the button's properties     With oButton     .DescriptionText = "Create the Client Name" 'Tooltip text when mouse if placed over button     .Caption = " CreateClientName" 'Text if Text in Icon is chosen"     .OnAction = "CreateClientName" 'Runs the Sub CreateClientName() code when clicked     .Style = msoButtonIcon ' Button displays as icon, not text or both     .FaceId = 66     End With ' And set some of the button's properties     With pButton     .DescriptionText = "Create the Client Contact Information" 'Tooltip text when mouse if placed over button     .Caption = "CreateClientContact" 'Text if Text in Icon is chosen     .OnAction = "CreateClientContact" 'Runs the Sub CreateClientContact() code when clicked"     .Style = msoButtonIcon ' Button displays as icon, not text or both     .FaceId =33     End With ' And set some of the button's properties     With qButton     .DescriptionText = "Add the goals" 'Tooltip text when mouse if placed over button     .Caption = "CreateGoals" 'Text if Text in Icon is chosen     .OnAction = "CreateGoals" 'Runs the Sub CreateGoals() code when clicked     .Style = msoButtonlcon ' Button displays as icon, not text or both     .FaceId = 52     End With ' And set some of the button's properties     With rButton     .DescriptionText = "Skip to next slide" ' Tooltip text when mouse if placed over button     .Caption = "ChangePage" 'Text if Text in Icon is chosen     .OnAction = "ChangePage" 'Runs the Sub ChangeSlide() code when clicked     .Style = msoButtonIcon ' Button displays as icon, not text or both     .FaceId = 55     End With     ' You can set the toolbar position and visibility here if you like     ' By default, it'll be visible when created     oToolbar.Top = 150     oToolbar.Left = 150     oToolbar.Visible = True     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 End Sub Sub Auto_Close() ' This will run when PowerPoint closes and it will delete the toolbar.     Dim oToolbar As CommandBar     Dim LarrysToolbar As String     ' Note: MyToolbar should be the same value here as in the code that created the toolbar     LarrysToolbar = "UpdateClientInfo"     On Error Resume Next     Application.CommandBars(MyToolbar).Delete End Sub 

For each macro in the process, Larry created a button definition and a With clause in the Auto_Open routine connecting the button to the correct macro. You will need to do the same thing.

Create And Test The Add-In File

Create a copy of the PowerPoint file you have been working with. Make sure it contains a few blank slides and the macros you want in the final add-in.

Go to the VB editor and comment the code. You need to make sure when you come back next time, you understand what you did and why you did it. You can't add too many comments to code.

Save the presentation file and run the macros. This makes sure when you added the comments you didn't mess up any of the existing code.

When finished testing, remove all the slides created during testing and save the presentation one more time.

You are now ready to save the file with just the code as an addin. Go to File Save As. From the file type drop down list, select, PowerPoint Add-In. Notice the save location changes. PowerPoint wants to save the add-in with all the other add-ins on the computer. This is usually the place where you want to save your add-in as well. If it isn't, change the location. When you have set the location, click OK to do the save.

Why would you want to change the save location for the file? If creating an add-in for distribution, you will probably want to store a copy somewhere on the hard drive is easier to find than the add-in folder

Close the PowerPoint file. Go to Tools Add-Ins. The new add-in may already be on the list of available add-ins. If it is, select it and click Load.

If it isn't on the list, add it to the list. Click the Add New button and select the add-in file. Once you have clicked OK and added the add-in to the list, you may still need to load it. Once it is loaded, the toolbar should appear.

start sidebar
Tip 54: Nothing happening when you click New?

If nothing happens when you click New, check the security settings. You can run your own macros at a much higher security setting than you can load add-ins.

Use the Macro Security button on the Tools Options, Security tab to change the setting. You need it no higher than medium to run your addins.

end sidebar
 

Now it is time for the final test. Close PowerPoint and re-open it. Open a new presentation and add slides so there are at least two slides. Check that the toolbar is available. Click the button that runs the shell macro, UpdateToNewClient. Did it work?

If it worked, you are finished. If it didn't, you need to

  • Unload the add-in (Tools Add-Ins, click yours and unload it)

  • Open the presentation (PPT) version of the file containing the macro

  • Make your changes and save the presentation (PPT) file

  • Re-create the add-in, re-load it and re-test it.




Kathy Jacobs On PowerPoint
Kathy Jacobs On PowerPoint
ISBN: 972425861
EAN: N/A
Year: 2003
Pages: 166

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net