Sunday 15 June 2008

PowerPoint macro to rename drawing objects

This week I created a slide to show a process flow.  I decided the best way to explain how our process works was with lots of animation, showing how incident tickets pass from one group to another.  When all the drawing objects on the slide have un-helpful names like Group 53 or Callout 12 it can be pretty confusing working out the custom animation sequence.  I've known for a while it's possible to rename drawing objects pretty easily with VBA, but it strikes me as a glaring omission from the User Interface that we have to drop into code to achieve it.

 

Anyway, I was getting into enough of a mess with this particular animation sequence that it was worth spending the time writing the following bit of VBA and using it to give my drawing objects more meaningful names like Stickman 1 and Flying ticket 1.

 

This works for me in PowerPoint 2007.  As usual, I didn't do much testing, and I have no idea if/how it will work on other versions.  I'd suggest trying this out on a copy of your slides rather than risk wrecking the real thing if the code doesn't behave as expected (which I realised would have been a good idea just after I told it to rename a series of about 15 objects on my insanely complicated slide... but this time I got away with it).

Sub RenameShape()

 

Dim n As Integer, NewName As String

 

With ActiveWindow.Selection

 

    If .Type = ppSelectionShapes Then

   

        NewName = InputBox("New name for shape:", "Rename Shape", .ShapeRange(1).Name)

   

        With .ShapeRange

            If .Count > 1 Then

                For n = 1 To .Count

                    .Item(n).Name = NewName & " " & n

                Next n

            Else

                    .Name = NewName

            End If

        End With

       

    Else

        MsgBox "Selection is not a Shape", vbOKOnly, "Rename Shape"

    End If

End With

 

End Sub

If you have one or more drawing objects selected, you'll get a popup dialog asking for the new name for the shape(s).  If you only have one object selected, it will be renamed with that name.  If you have more than one object selected, they'll be renamed NewName 1, NewName 2, etc. in the order you selected them.  (If your selection isn't drawing objects you'll just get an error.)

 

In case you're wondering, renaming the objects doesn't require you to recreate the custom animations.  You'll see the new names appear in the Custom Animation pane as soon as you've renamed the objects.  When I tried it, my animations (including entrance, exit and motion path) all carried on working just as before.

 

Enjoy!

1 comment:

  1. I was looking for something else, though similar, and found this. Though not was I was originally looking for, it's VERY useful! Thank you! (I've only tried it in 2007, also.)

    ReplyDelete

Creative Commons License This work by TechieBird is licensed under a Creative Commons Attribution-No Derivative Works 2.0 UK: England & Wales License.