Adding Keyboard Shortcuts to PowerPoint

Published 27 January 06 02:04 PM | Scott Mitchell

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.

Filed under:

Comments

No Comments

Leave a Comment

(required) 
(required) 
(optional)
(required) 

Archives

My Books

  • Teach Yourself ASP.NET 4 in 24 Hours
  • Teach Yourself ASP.NET 3.5 in 24 Hours
  • Teach Yourself ASP.NET 2.0 in 24 Hours
  • ASP.NET Data Web Controls Kick Start
  • ASP.NET: Tips, Tutorials, and Code
  • Designing Active Server Pages
  • Teach Yourself Active Server Pages 3.0 in 21 Days

I am a Microsoft MVP for ASP.NET.

I am an ASPInsider.