Shape index for macro

C

C Heath

Hi There,

Looking for some help with being able to identify the a shape index ID in
Powerpoint 2003.

I want to write a macro and need to include the index number for each
shape/text/etc on a slide. An example of coding is below and you can see the
shape index is 6.

ActivePresentation.Slides(2).Shapes(6).Visible = True

The coding works, and previously I have been able to identify the index when
using office 97. How can I identify the index number for each shape/text/etc
on a slide in Powerpoint 2003?

I am no expert, so any detailed help would be appreciated.

Cheers
Chris
 
G

Guest

You'll find that using shape (x) is fraught with danger! The number will
change if you e.g. send an item to the back or change the slide.

Better would be to name the shapes and use this property

To name a shape select in edit mode and run this

Sub namer()
On Error GoTo err
Dim namestr As String
namestr = InputBox("Name of shape")
ActiveWindow.Selection.ShapeRange.Name = namestr
Exit Sub
err:
MsgBox ("sorry theres been an error is anything selected?")
End Sub

Your code should then look like this

ActivePresentation.Slides(2).Shapes("whatever you named it") _
..Visible = True

-- If you know how to add the "select multiple objects" tool to a toolbar
from customise this will show the name you have given the shape.

Did that answer the question / help?
_____________________________
John Wilson
Microsoft Certified Office Specialist
http://www.technologytrish.co.uk/ppttipshome.html
 
D

David M. Marcovitz

What John said and ...

While you're naming things, you might as well name the slide as well.
Check out Example 8.7 on my site for shape and slide naming procedures:

http://www.PowerfulPowerPoint.com/

Also, I'll mention this since the line of code you are using looks like
it might be something from my book...If I were to write a second edition
of the book, the main thing I would change is to put the stuff about
naming shapes much earlier and not use shape numbers at all. When I teach
VBA in PowerPoint, naming shapes is one of the first things I teach now
because it saves a lot of headaches.

--David

--
David M. Marcovitz
Microsoft PowerPoint MVP
Director of Graduate Programs in Educational Technology
Loyola College in Maryland
Author of _Powerful PowerPoint for Educators_
http://www.PowerfulPowerPoint.com/
 
B

Brian Reilly, MVP

Hi David,
While named shapes are one of my well-used tools, I have switched to
favoring tags on shapes since one can have multiple tags but only one
shape.name. Way more powerful due to flexibility.

Brian Reilly, MVP
 
D

David M. Marcovitz

Tags are great. Thanks. However, can you EASILY select something based on
a tag. I capitalize "easily" not to yell, but to emphasize how easy I
need it to be to work for my students. You and I can (lower-case) easily
right a routine to find the shape with a particular tag set to a
particular thing, but my beginning students probably can't. So, my
question is can tags be used as easily as shape numbers and names as in:

ActivePresentation.Slides(2).Shapes(7)

or

ActivePresentation.Slides(2).Shapes("MySmileyFace")

I'm asking because I'm always looking for a better way. Thanks.

--David

--
David M. Marcovitz
Microsoft PowerPoint MVP
Director of Graduate Programs in Educational Technology
Loyola College in Maryland
Author of _Powerful PowerPoint for Educators_
http://www.PowerfulPowerPoint.com/
 
S

Steve Rindsberg

David M. said:
Tags are great. Thanks. However, can you EASILY select something based on
a tag. I capitalize "easily" not to yell, but to emphasize how easy I
need it to be to work for my students. You and I can (lower-case) easily
right a routine to find the shape with a particular tag set to a
particular thing, but my beginning students probably can't. So, my
question is can tags be used as easily as shape numbers and names as in:

ActivePresentation.Slides(2).Shapes(7)

or

ActivePresentation.Slides(2).Shapes("MySmileyFace")

Nope. But you could pretty easily write a function to do the job for you (and
in the process teach your students the value of using functions for commonly
used ... um ... well ... functions. <g>)

Actually it's even handy to isolate getting a shape of a given name into a
function so you can do:

Dim oSh as Shape
Set oSh = GetShapeNamed("BlahBlah",oSl) ' oSl is the slide you're searching
If Not oSh Is Nothing Then
' do yer stuff with oSh
End If

I'm sure there are more elegant ways of doing it, but this lets you put any
necessary errorhandling code (what if there's no shape by that name, etc.) in
the function instead of repeating it every time you go looking for the shape.

Names vs Tags ... each has its uses. You can't have more that one shape on a
slide of a given name (except when PPT's been taking its bugs out for a walk)
but you can tag any number of shapes with the same tag values. Kinda nice to
know you have something unique with names, at least for some purposes.

And it's pretty simple to write code that tags every shape with its name or
names it whatever the shape's "MyName" tag is. Then when you need to rummage
in PPT's Big Bin 'o Shapes, you can use either method you like.
 
D

David M. Marcovitz

Nope. But you could pretty easily write a function to do the job for
you (and in the process teach your students the value of using
functions for commonly used ... um ... well ... functions. <g>)

Actually it's even handy to isolate getting a shape of a given name
into a function so you can do:

Dim oSh as Shape
Set oSh = GetShapeNamed("BlahBlah",oSl) ' oSl is the slide you're
searching If Not oSh Is Nothing Then
' do yer stuff with oSh
End If

I'm sure there are more elegant ways of doing it, but this lets you
put any necessary errorhandling code (what if there's no shape by that
name, etc.) in the function instead of repeating it every time you go
looking for the shape.

Names vs Tags ... each has its uses. You can't have more that one
shape on a slide of a given name (except when PPT's been taking its
bugs out for a walk) but you can tag any number of shapes with the
same tag values. Kinda nice to know you have something unique with
names, at least for some purposes.

And it's pretty simple to write code that tags every shape with its
name or names it whatever the shape's "MyName" tag is. Then when you
need to rummage in PPT's Big Bin 'o Shapes, you can use either method
you like.

-----------------------------------------
Steve Rindsberg, PPT MVP
PPT FAQ: www.pptfaq.com
PPTools: www.pptools.com
================================================

Thanks for the clear explanation.
--David

--
David M. Marcovitz
Microsoft PowerPoint MVP
Director of Graduate Programs in Educational Technology
Loyola College in Maryland
Author of _Powerful PowerPoint for Educators_
http://www.PowerfulPowerPoint.com/
 
C

ciw2otv

I'm sure there are more elegant ways of doing it, but this lets you put any
necessary errorhandling code (what if there's no shape by that name, etc.) in
the function instead of repeating it every time you go looking for the shape.

Names vs Tags ... each has its uses. You can't have more that one shape on a
slide of a given name (except when PPT's been taking its bugs out for a walk)
but you can tag any number of shapes with the same tag values. Kinda nice to
know you have something unique with names, at least for some purposes.

And it's pretty simple to write code that tags every shape with its name or
names it whatever the shape's "MyName" tag is. Then when you need to rummage
in PPT's Big Bin 'o Shapes, you can use either method you like.

OK, I'm it. You tagged me. But how did you tag me, either in edit mode
or writing code? I have been trying to figure this out since you used
the word "vagaries" in a sentence. I like the ideas of writing
functions, being able to access more than one shape by barking one
phrase, and doing that error handling trick. Could you point me to a
tutorial that will flatten out the learning curve?
Many thanks,
Eldon
 
S

Steve Rindsberg

OK, I'm it. You tagged me. But how did you tag me, either in edit mode
or writing code? I have been trying to figure this out since you used
the word "vagaries" in a sentence. I like the ideas of writing
functions, being able to access more than one shape by barking one
phrase, and doing that error handling trick. Could you point me to a
tutorial that will flatten out the learning curve?

OK, a vaga-rant.

To tag the currently selected shape:

With ActiveWindow.Selection.ShapeRange(1)
.Tags.Add "ShapeName", "Moose"
End With

With the function below you can do:

Dim oSh as Shape
Set oSh = GetShapeTaggedWith("ShapeName", "Moose", _
ActiveWindow.Selection.SlideRange(1))
If Not oSh Is Nothing Then ' we found it
' move it an inch to the right
oSh.Left = oSh.Left + 72
End If


Function GetShapeTaggedWith(sTagName as String, _
sTagValue as String, _
oSl as Slide) as Shape
' Returns a reference to the shape
' whose sTagName tag is sTagValue
' on slide oSl

Dim oSh as Shape
Dim oTemp as Shape

On Error GoTo ErrorHandler

Set oTemp = Nothing

' look at each shape
For Each oSh in oSl.Shapes
If oSh.Tags(sTagName) = sTagValue then
' It's the shape we're after
Set oTemp = oSh
Exit For
End if
Next
' oTemp either contains a reference to the shape
' we're after or is nothing
' The function returns it either way:
Set GetShapeTaggedWith = oTemp

NormalExit:
Exit Function
ErrorHandler:
MsgBox "Error: " & Err.Number & " " & Err.Description
Resume NormalExit
End Function
 
C

ciw2otv

WOW Thanks, that is some tutorial. With a few changes here and there
this will be among a plethora of presentations at my house. So many
useful tools!!
 
D

David M. Marcovitz

Is this going on the FAQ?
--David

--
David M. Marcovitz
Microsoft PowerPoint MVP
Director of Graduate Programs in Educational Technology
Loyola College in Maryland
Author of _Powerful PowerPoint for Educators_
http://www.PowerfulPowerPoint.com/
 
S

Steve Rindsberg

WOW Thanks, that is some tutorial. With a few changes here and there
this will be among a plethora of presentations at my house. So many
useful tools!!

My pleasure, sir.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top