How to access / select / color shape by using it's id ?

T

Tom Asken

Hello,

i would like to color a shape by using it's unique id


I know how to color it:

Public Sub setColorOfShape(oShp As Shape)
zOrderPos = oShp.zOrderPosition
SlideShowWindows(1).View.Slide.Shapes(zOrderPos).Fill.Visible =
msoTrue
SlideShowWindows(1).View.Slide.Shapes(zOrderPos).Fill.ForeColor.RGB
= RGB(255, 204, 0)
SlideShowWindows(1).View.Slide.Shapes(zOrderPos).Fill.OneColorGradient
msoGradientVertical, 2, 0.33

End Sub

I know how to get it's unique id if someone clicked it:

Public Sub getIdOfClickedShape(oShp As Shape)
MsgBox oShp.id
End Sub

I used the macro recorder to see how it colors a shape:

Sub setColorOfShape()
' created from macro recorder
ActiveWindow.Selection.SlideRange.Shapes("Text Box 18").Select
ActiveWindow.Selection.ShapeRange.TextFrame.TextRange.Select
With ActiveWindow.Selection.ShapeRange
.Fill.Visible = msoTrue
.Fill.Solid
.Fill.ForeColor.RGB = RGB(0, 204, 255)
.Fill.Transparency = 0#
End With
End Sub

Because the name of a shape ("Text Box 18") and its z-OrderPosition
can change i would like to manipulate it by its unique id.

Like in this Pseudo-Code:

ActivePresentation.Shapes.id(467).Select ' <- this is just an example,
it doesn't work

1) So How do i acces an manipulate a shape by talking / selecting it
via it's ID ?

2) How do i count the number of shapes on the activewindow / slide ?
I found:
Dim sSlide As Slide
MsgBox sSlide.Shapes.Count
but i did not work.

Thanks a lot

Best regards

Tom
 
C

Chirag

The following function could help you get started:

---
Function FindShapeById(ByVal Shps As Object, ByVal Id As Long) As Shape
Dim I As Long

For I = 1 To Shps.Count
If Shps(I).Id = Id Then
Set FindShapeById = Shps(I)
Exit For
End If

If Shps(I).Type = msoGroup Then
Set FindShapeById = FindShapeById(Shps(I).GroupItems, Id)
If Not (FindShapeById Is Nothing) Then
Exit For
End If
End If
Next
End Function

Sub Test()
MsgBox FindShapeById(ActivePresentation.Slides(1).Shapes, 2050).Name
End Sub
---

- Chirag

PowerShow - View multiple shows simultaneously
http://officeone.mvps.org/powershow/powershow.html
 
T

Tom Asken

1) So How do i acces an manipulate a shape by talking / selecting it

Chirag said:
The following function could help you get started:

---
Function FindShapeById(ByVal Shps As Object, ByVal Id As Long) As Shape
Dim I As Long
[SNIP]

Sub Test()
MsgBox FindShapeById(ActivePresentation.Slides(1).Shapes, 2050).Name
End Sub

Thanks for your answer. But my problem is not to find a shape, but to
select and manipulate it by using it's id.

For instance i can select and manipulate a certain shape by using it's
name with the following code:

ActiveWindow.Selection.SlideRange.Shapes("Text Box 18").Select

But because the name of a shape can be used many times (name is not a
primary key) and the name also can change i would like to use the id.

Again in Pseudo-Code:

Const idShapeOne = 33245
ActiveWindow.Shape(idShapeOne).Select ' <- this is wrong and just an
example


Thanks for your time

Best Regards

Tom
 
G

Guest

I don't think there is an easy way to do what you want, but it is possible to scroll through the shapes to find which one has the ID you want. However, you mentioned that the name of the shape changes. This is only true if you change the name of the shape. Once a shape is created on a slide, it stays the same unless it is deleted or unless you use code to change it
--Davi

David M. Marcovit
Director of Graduate Programs in Educational Technolog
Author of _Powerful PowerPoint for Educators
http://www.loyola.edu/education/PowerfulPowerPoint

----- Tom Asken wrote: ----

Hello

i would like to color a shape by using it's unique i


I know how to color it

Public Sub setColorOfShape(oShp As Shape
zOrderPos = oShp.zOrderPositio
SlideShowWindows(1).View.Slide.Shapes(zOrderPos).Fill.Visible
msoTru
SlideShowWindows(1).View.Slide.Shapes(zOrderPos).Fill.ForeColor.RG
= RGB(255, 204, 0
SlideShowWindows(1).View.Slide.Shapes(zOrderPos).Fill.OneColorGradien
msoGradientVertical, 2, 0.3

End Su

I know how to get it's unique id if someone clicked it

Public Sub getIdOfClickedShape(oShp As Shape
MsgBox oShp.i
End Su

I used the macro recorder to see how it colors a shape

Sub setColorOfShape(
' created from macro recorde
ActiveWindow.Selection.SlideRange.Shapes("Text Box 18").Selec
ActiveWindow.Selection.ShapeRange.TextFrame.TextRange.Selec
With ActiveWindow.Selection.ShapeRang
.Fill.Visible = msoTru
.Fill.Soli
.Fill.ForeColor.RGB = RGB(0, 204, 255
.Fill.Transparency = 0
End Wit
End Su

Because the name of a shape ("Text Box 18") and its z-OrderPositio
can change i would like to manipulate it by its unique id

Like in this Pseudo-Code

ActivePresentation.Shapes.id(467).Select ' <- this is just an example
it doesn't wor

1) So How do i acces an manipulate a shape by talking / selecting i
via it's ID

2) How do i count the number of shapes on the activewindow / slide
I found
Dim sSlide As Slid
MsgBox sSlide.Shapes.Coun
but i did not work

Thanks a lo

Best regard

To
 
S

Steve Rindsberg

See below:
Thanks for your answer. But my problem is not to find a shape, but to
select and manipulate it by using it's id.

Never select anything if you don't have to. It's slow and usually ugly.
Instead, work with a reference to the shape, which is what Chirag's function returns.

For example:

MsgBox FindShapeById(ActivePresentation.Slides(1).Shapes, 2050).Name

translated means:

From the collection of shapes on Slide 1 of the active presentation, find shape 2050 and
show me its name in a message box.
For instance i can select and manipulate a certain shape by using it's
name with the following code:

ActiveWindow.Selection.SlideRange.Shapes("Text Box 18").Select

But because the name of a shape can be used many times (name is not a
primary key) and the name also can change i would like to use the id.

Again in Pseudo-Code:

Const idShapeOne = 33245
ActiveWindow.Shape(idShapeOne).Select ' <- this is wrong and just an
example

Try:

FindShapeById(ActivePresentation.Slides(1).Shapes, idShapeOne).FillColor.RGB = whatever

or set whatever other property you like.
 
B

Bill Dilworth

Or if the slide is copied.

--
Bill Dilworth, Microsoft PPT MVP
===============
Please spend a few minutes checking vestprog2@
out www.pptfaq.com This link will yahoo.
answer most of our questions, before com
you think to ask them.

Change org to com to defuse anti-spam,
ant-virus, anti-nuisance misdirection.
..
..

David M. Marcovitz said:
I don't think there is an easy way to do what you want, but it is possible
to scroll through the shapes to find which one has the ID you want.
However, you mentioned that the name of the shape changes. This is only
true if you change the name of the shape. Once a shape is created on a
slide, it stays the same unless it is deleted or unless you use code to
change it.
 

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