Scott on Writing

Musings on technical writing...

Adding Keyboard Shortcuts to PowerPoint

Caveat: I am pretty green when it comes to VBA and Office automation, so take my comments as my (probably incorrect) educated guesses. I could be, and likely am, misspeaking in a number of ways in this blog entry, but hopefully the comments here will help someone else struggle through some of the limitations of PowerPoint. I do heartily recommend the PowerPoint newsgroup - good volume and knowledgeable folks.

If you use PowerPoint a fair bit like yours truly, you'll quickly discover that there are no configurable keyboard shortcuts in PowerPoint. Constrast that to Word, for example, which makes it easy to assign various actions to keyboard shortcuts (go to the Tools menu, choose Customize, and then click the Keyboard button in the lower right corner of the Customize dialog box). For example, in Word I have the keyboard combo Ctrl+C mapped to applying a particular style that provides a nice-looking format for source code (Courier New in a smaller font size).

In my PowerPoint presentations I like to format classes, properties, methods, events, and so on using the same Courier New font in a smaller font size. But in PowerPoint I've had to always use the mouse to select the text, go to the font drop-down list, choose Courier New, and then click on the icon to decrease the font size. This is a pain when creating, over a number of weeks, several hundred slides with typically three to five words needing to be formatted in this manner per slide. Double ick.

After putting up with it long enough (for years, now), I figured there has to be a better way. I went out and searched the microsoft.public.powerpoint newsgroup and found some helpful hints. While you cannot create customizable keyboard shortcuts like in Word, you can create a macro and then add the macro as a Toolbar icon that has a keyboard shortcut. This technique is discussed by poster Jeff Chapman in this newsgroup thread. Therefore I created the following macro in a Module in one of my PowerPoint presentations (you can get to the Macro Editor by hitting Alt+F11):

Sub CodeFormat()
    ActiveWindow.Selection.TextRange.Font.Name = "Courier New"
    ActiveWindow.Selection.TextRange.Font.Size = ActiveWindow.Selection.TextRange.Font.Size - 4
End Sub

This macro, when executed, sets the selected text's font to Courier New and knocks down the font size by 4 pts. With this macro created, I was able to customize the Toolbar in PowerPoint and add an icon to run my CodeFormat macro and set its Text to &Code Format, which made Alt+C keyboard shortcut for the macro (Alt+X becomes the shortcut, where X is the character following the ampersand (&).). In short, I could now click Ctrl+C and have the selected text formatted in the font/style I like for code. Wonderful.

One problem with the macro-based approach is that, in PowerPoint, it seems that macros are assigned to a particular PowerPoint file. That is, I couldn't reuse a macro defined in one PowerPoint presentation in another one without going through the steps of recreating the macro. In order to make “global” macros, from what I've read you need to use an Add-In. This FAQ - Create an Add-In with Toolbars that Run Macros - proved to be helpful. Check out the FAQ for information on how to create and program the Macro/Add-In. In the end, I ended up using the following code for the Add-In:

Sub Auto_Open()
    Dim oToolbar As CommandBar
    Dim oButton As CommandBarButton
    Dim MyToolbar As String

    ' Give the toolbar a name
    MyToolbar = "Code Format"

    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:=msoBarTypeNormal, Temporary:=True)
       
    If Err.Number <> 0 Then
          ' The toolbar's already there, so we have nothing to do
          Exit Sub
    End If
   
    oToolbar.Top = 0

    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 = "Format the selected text as code."
          'Tooltip text when mouse if placed over button
         .Caption = "&Code Format"
         'Text if Text in Icon is chosen
         .OnAction = "Button1"
          'Runs the Sub Button1() code when clicked
         .Style = msoButtonIconAndCaption
          ' Button displays as icon, not text or both
         .FaceId = 52
          '52 is my favorite pig;
          ' 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
    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
   
   
    ActiveWindow.Selection.TextRange.Font.Name = "Courier New"
    ActiveWindow.Selection.TextRange.Font.Size = ActiveWindow.Selection.TextRange.Font.Size - 4
End Sub

I then added the Add-In through the Tools menu's AddIns menu item. And now I have a little pig icon in all my PowerPoint presentations that can be executed by hitting Ctrl+C, making PowerPoint much more user-friendly for yours truly.

posted on Friday, January 27, 2006 2:04 PM

Feedback

No comments posted yet
Title:  
Name:  
Url:
Protected by Clearscreen.SharpHIPEnter the code you see:
Comments   

Add To Your Reader

My Links

Archives

Post Categories

 

I am a Microsoft MVP for ASP.NET.
I am an ASPInsider.
<May 2008>
SMTWTFS
27282930123
45678910
11121314151617
18192021222324
25262728293031
1234567

Comment Stats

DayTotal% of Total
Sunday 1866.8%
Monday 37913.9%
Tuesday 45316.7%
Wednesday 50418.5%
Thursday 53519.7%
Friday 49418.2%
Saturday 1666.1%
Total 2717100.0%

Hour1Total% of Total
12:00 AM 652.4%
1:00 AM 682.5%
2:00 AM 622.3%
3:00 AM 742.7%
4:00 AM 572.1%
5:00 AM 1033.8%
6:00 AM 1084.0%
7:00 AM 1585.8%
8:00 AM 1716.3%
9:00 AM 1475.4%
10:00 AM 1716.3%
11:00 AM 1816.7%
12:00 PM 1886.9%
1:00 PM 1696.2%
2:00 PM 1605.9%
3:00 PM 1324.9%
4:00 PM 1073.9%
5:00 PM 923.4%
6:00 PM 913.3%
7:00 PM 963.5%
8:00 PM 833.1%
9:00 PM 782.9%
10:00 PM 792.9%
11:00 PM 772.8%
Total 2717100.0%

Comments by Blog Entry Date/Time

Day Entry MadeAvg.Total
Sunday 5.54144
Monday 5.22339
Tuesday 4.28419
Wednesday 7.67637
Thursday 6.90607
Friday 5.48411
Saturday 5.33160
Total 5.842717

Hour1 Entry MadeAvg.Total
12:00 AM 5.0035
1:00 AM 1.002
5:00 AM 0.000
7:00 AM 7.0035
8:00 AM 5.35107
9:00 AM 6.32278
10:00 AM 6.47246
11:00 AM 4.41181
12:00 PM 6.88330
1:00 PM 3.00111
2:00 PM 5.41222
3:00 PM 8.64285
4:00 PM 4.0589
5:00 PM 5.92154
6:00 PM 4.52113
7:00 PM 9.67174
8:00 PM 9.80147
9:00 PM 5.05111
10:00 PM 5.4265
11:00 PM 4.5732
Total 5.842717

Learn More About Comment Stats
1 - All times GMT -8...


Blog Stats

Favorite Web Sites

My Books

My MSDN Articles