• No results found

In Chapter 6 we discussed how to ref erence ob jects by their names, and we noted how dif ficult it is to re member the name of an ob ject. The fol lowing scripts can be used to find the name of an ob ject and set the name of an ob ject. Note that all other scripts in this book are de signed to be run in Slide Show View. These scripts are de signed to be run in Edit View.

The two procedures that we need are GetObjectName and

SetObjectName. GetObjectName finds out what the name of an ob ject is.

SetObjectName asks you to type a new name for an ob ject.

If you run the GetObjectName script while an ob ject is selected, a

MsgBox will pop up with the ob ject’s name. If you run SetObjectName, an

InputBox will al low you to en ter a name for an object. These scripts check to make sure that one and only one ob ject is selected, because you can’t get or change the name of more than one object at a time.

Sub GetObjectName()

If ActiveWindow.Se lection.Type = ppSelectionShapes _ Or ActiveWindow.Se lection.Type = ppSelectionText Then If ActiveWindow.Se lection.ShapeRange.count = 1 Then MsgBox (ActiveWindow.Se lection.ShapeRange.Name) Else

MsgBox ("You have se lected more than one shape.") End If

Else

MsgBox ("No shapes are se lected.") End If

End Sub

Sub SetObjectName()

Dim objectName As String

If ActiveWindow.Se lection.Type = ppSelectionShapes _ Or ActiveWindow.Se lection.Type = ppSelectionText Then If ActiveWindow.Se lection.ShapeRange.count = 1 Then

objectName = InputBox(prompt:="Type a name for the ob ject") objectName = Trim(objectName)

If objectName = "" Then

MsgBox ("You did not type any thing. " & _ "The name will re main " & _

ActiveWindow.Se lec tion.ShapeRange.Name) Else

ActiveWindow.Se lec tion.ShapeRange.Name = objectName End If

Else

MsgBox _

("You can not name more than one shape at a time. " _ & "Se lect only one shape and try again.")

End If Else

MsgBox ("No shapes are se lected.") End If

End Sub

If you are try ing to un derstand these pro cedures, pay careful attention to the nestedIf statements and how they are in dented in the example.

The heart of these pro cedures is ActiveWindow.Se lec tion. ShapeRange.Name. This looks at the Name prop erty of the currently se lected shape. In GetObjectName, we simply re turn this name in a MsgBox. In

SetObjectName, we set this with what ever is typed in an InputBox. The rest of each of the procedures is to make sure an ob ject is se lected and to clean up what you typed for the ob ject’s name.

If you run the GetObjectName script while an ob ject is selected, a

MsgBox will pop up with the ob ject’s name. You can then use this name in quotes in stead of an ob ject’s number. For ex ample, if you wanted to hide an ob - ject named “Pic ture 6,” you can use:

ActivePresentation.SlideShowWindow.View.Slide. _ Shapes("Pic ture 6").Vis ible = False

As you recall from Chap ter 6, once you add an ob ject to your slide, its name, un - like its number, will not change un less you change it, so this line of code will al- ways work even if you change the an imation or der or delete other ob jects on the slide. Even if you don’t name your own ob jects, each new ob ject that is added to a slide is given a name that is different from all other objects that have ever been added to that slide.

When you run SetObjectName, an InputBox will al low you to en ter a name for an object.Trim is used to delete any ex tra spaces before and af ter the name you type. The pro cedure also checks to make sure you typed something, because you don’t want to give an ob ject a blank name.

GetObjectName and SetObjectName check to make sure that one and only one ob ject is selected, because you can’t get or set the name of more than one ob ject at a time. If you are look ing for a simpler way to do the same things, you can try the following scripts, but you are responsible for mak ing sure that you have selected one and only one object.

Sub GetObjectName()

MsgBox (ActiveWindow.Se lection.ShapeRange.Name) End Sub

Sub SetObjectName()

Dim objectName As String

objectName = InputBox(prompt:="Type a name for the ob ject") objectName = Trim(objectName)

If objectName = "" Then

MsgBox ("You did not type any thing. The name will re main " & _ ActiveWindow.Se lection.ShapeRange.Name)

Else

ActiveWindow.Se lection.ShapeRange.Name = objectName End If

End Sub

If you try to run either of these pro cedures with out hav ing one ob ject selected, you will get an er ror message. If you try to give an ob ject the same name as an- other ob ject on that slide, you will also get an er ror message, so be sure to give each object on a slide a different name.

Because these procedures run in Edit View in PowerPoint (not from Slide Show View or from the VBA Ed itor), we cannot create a but ton on a slide to run them. The eas iest way to run a script in Edit View is to se lect “Macro” from the Tools menu and choose “Macros” from the flyout menu (or hit Alt-F8 on a Win - dows computer or Op tion-F8 on a Macintosh). Se lect the procedure name that you want to run, and click on the Run but ton (see Figure 8.1).

Figure 8.1. Run ning a Macro in Edit View