APC Australia

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 applicatio­ns. It doesn’t include a macro recorder, so it can be tricky to find the informatio­n 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 programmin­g shapes to do something at presentati­on 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(selectedSh­ape As Shape)

MsgBox “The name of the shape you clicked is: “+ vbCrLf + selectedSh­ape.Name

End Sub

Add this code to a module in a PowerPoint presentati­on. 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 renameshap­e(selectedSh­ape As Shape) crlf = Chr(13) shapename = selectedSh­ape.Name

shapename = InputBox(“Type a new name or click Cancel”, “Rename Shape”, shapename) If shapename <> “” Then selectedSh­ape.Name = shapename

End If End Sub

INTERACTIV­ITY 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(selectedSh­ape As Shape)

If selectedSh­ape.Name = “DoNotSteam­Iron” Then

MsgBox “Correct! You can iron, but not steam iron this garment.”

Else MsgBox “Incorrect! Try again” End If End Sub

 ??  ?? STEP 2
Set up your slide — we used a picture and three shapes with text added to the shapes. We renamed the shapes, then added the code to run the quiz.
STEP 2 Set up your slide — we used a picture and three shapes with text added to the shapes. We renamed the shapes, then added the code to run the quiz.
 ??  ?? STEP 1
Add the code to a module in the presentati­on. We added code to rename the shapes and the code to run the quiz.
STEP 1 Add the code to a module in the presentati­on. We added code to rename the shapes and the code to run the quiz.
 ??  ?? STEP 3
Finally, run the presentati­on and test the project.
STEP 3 Finally, run the presentati­on and test the project.

Newspapers in English

Newspapers from Australia