Naming shapes and running macros in PowerPoint
Helen Bradley explains how to name shapes in PowerPoint and how to use them to trigger actions in her last-ever macros column.
PowerPoint VBA lacks some features which are readily available in other applications. It doesn’t include a macro recorder, so it can be tricky to find the information you need to code macro solutions. This month and in my final macro column for APC, we’ll focus on finding the name of a PowerPoint shape, renaming a shape and programming shapes to do something at presentation time.
NAMES AND SHAPES
To find the name of a shape in PowerPoint, you can do one of two things; you can locate it via the Selection pane or you can write some code to do the work for you. This code will return the name of a shape that you click:
Sub shapename(selectedShape As Shape)
MsgBox “The name of the shape you clicked is: “+ vbCrLf + selectedShape.Name
End Sub
Add this code to a module in a PowerPoint presentation. To run it, you will need to attach it to a shape or a picture. You do this by selecting the shape and, from the Insert tab, choose ‘Action > Mouse Click’ tab, click the ‘Run Macro’ option and click to select the macro name. Now, when you run your slideshow, you can click the shape or picture to view its name.
RENAME A SHAPE
You can use code to rename a shape to give it a more meaningful name. This code, when attached to a picture or shape using the ‘Action’ options will, when you click the shape, show you the current shape’s name and allow you to alter it to suit:
Sub renameshape(selectedShape As Shape) crlf = Chr(13) shapename = selectedShape.Name
shapename = InputBox(“Type a new name or click Cancel”, “Rename Shape”, shapename) If shapename <> “” Then selectedShape.Name = shapename
End If End Sub
INTERACTIVITY USING SHAPES
One advantage of the Action feature in PowerPoint is that you can attach the same macro to multiple shapes and process them all using the same code. We’ll use this feature to include a mini quiz on a PowerPoint slide. Using your slide, you can pose a question and offer one or more answers in the form of shapes with text added to them. Depending on the shape/ answer that the user clicks, you can reply with a different response. The step by step below shows how to configure a simple Question/ Answer slide and this is the code behind the solution — the one piece of code should be attached to all the shapes :
Sub QuizAnswer(selectedShape As Shape)
If selectedShape.Name = “DoNotSteamIron” Then
MsgBox “Correct! You can iron, but not steam iron this garment.”
Else MsgBox “Incorrect! Try again” End If End Sub