How to initialize an array in VBA of named shapes on a slide?

G

Guest

I am trying to create an array of shapes (while presentation is running)
which have been given specific names (MyShape0, MyShape1, MyShape2...) on a
named slide ("MySlide").

I have tried something like the following but I keep getting runtime errors:

'Global array variable:
Dim MyShapeArray(0 to 11) as Shape

'From within a subroutine in the same module:
With ActivePresentation.Slides("MySlide")
MyShapeArray(0) = .Shapes("MyShape0")
MyShapeArray(1) = .Shapes("MyShape1")
MyShapeArray(2) = .Shapes("MyShape2")
MyShapeArray(3) = .Shapes("MyShape3")
MyShapeArray(4) = .Shapes("MyShape4")
MyShapeArray(5) = .Shapes("MyShape5")
MyShapeArray(6) = .Shapes("MyShape6")
MyShapeArray(7) = .Shapes("MyShape7")
MyShapeArray(8) = .Shapes("MyShape8")
MyShapeArray(9) = .Shapes("MyShape9")
MyShapeArray(10) = .Shapes("MyShapeA")
MyShapeArray(11) = .Shapes("MyShapeB")
End With

For i = 0 To 11
MyShapeArray(i).TextFrame.TextRange.Text = "00"
Next i

The code above gives me an error stating that an object or with variable has
not been set with the "MyShapeArray(0) = .Shapes("MyShape0")" line
highlighted.

Any idea what I'm doing wrong? I have also tried this without using a With
block, by spelling out the full reference to the shape on each line, but that
generates the same error.
 
A

Austin Myers

redhorse said:
I am trying to create an array of shapes (while presentation is running)
which have been given specific names (MyShape0, MyShape1, MyShape2...) on
a
named slide ("MySlide").

I have tried something like the following but I keep getting runtime
errors:

'Global array variable:

I *think* the problem is that an array is not a shape.

Try this:

Dim MyShapeArray(11) As Variant
 
G

Guest

Thanks for the reply.

When I try that, I get the error "object doesn't support this property or
method".

I have tried making the array of shape, shapes, shapeRange, and variant, but
nothing seems to work. I always get some kind of error.
 
S

Steve Rindsberg

I am trying to create an array of shapes (while presentation is running)
which have been given specific names (MyShape0, MyShape1, MyShape2...) on a
named slide ("MySlide").

I have tried something like the following but I keep getting runtime errors:

'Global array variable:
Dim MyShapeArray(0 to 11) as Shape

'From within a subroutine in the same module:
With ActivePresentation.Slides("MySlide")
MyShapeArray(0) = .Shapes("MyShape0")
MyShapeArray(1) = .Shapes("MyShape1")
MyShapeArray(2) = .Shapes("MyShape2")
MyShapeArray(3) = .Shapes("MyShape3")
MyShapeArray(4) = .Shapes("MyShape4")
MyShapeArray(5) = .Shapes("MyShape5")
MyShapeArray(6) = .Shapes("MyShape6")
MyShapeArray(7) = .Shapes("MyShape7")
MyShapeArray(8) = .Shapes("MyShape8")
MyShapeArray(9) = .Shapes("MyShape9")
MyShapeArray(10) = .Shapes("MyShapeA")
MyShapeArray(11) = .Shapes("MyShapeB")
End With

For i = 0 To 11
MyShapeArray(i).TextFrame.TextRange.Text = "00"
Next i

The code above gives me an error stating that an object or with variable has
not been set with the "MyShapeArray(0) = .Shapes("MyShape0")" line
highlighted.

Any idea what I'm doing wrong? I have also tried this without using a With
block, by spelling out the full reference to the shape on each line, but that
generates the same error.

Austin must have been taking the Brian Reilly Flying Short Course on Arrays.
Luckily, he slept through most of the classes, so his mind isn't totally
poisoned.

There's no problem with declaring an array of shapes.

If you've got multiple presentations open, be sure the right one is active, of
course.

And you're certain that the slide's name is MySlide?

What if you type this into the immediate window:

? ActivePresentation.Slides("MySlide").Slides.Count

If that gives you an error, there's some kind of problem referencing MySlide.

======================================================================

Oh, and you could simplify the code/maintenance a bit by doing:

Dim x as Long
With ActivePresentation.Slides("MySlide")
For x = Lbound(MyShapeArray) to Ubound(MyShapeArray) - 1
MyShapeArray(x) = .Shapes("MyShape" & cstr(x))
Next
End With

You'd have to name the last two shapes 10 and 11 instead of A and B of course,
but you could add more shapes w/o having to muck with the code beyond re-dimming
the array.
 
G

Guest

Steve,

Thanks for the tip on the immediate window. I get an error "Method or data
member not found".

The strange thing is that this syntax works in my code:
ActivePresentation.Slides("MySlide").Shapes("MyShape0").TextFrame.TextRange.Text = "00"
but I can't use the same syntax to assign the shape to an array. I'm sure
the slide name and the shape names are correct because I have been using them
extensively without errors. It is only this array assignment that is giving
me troubles.
 
G

Guest

Just an FYI, I tried changing .Slides.Count in the immediate window to
..Shapes.Count and got the correct answer of 73.
 
S

Shyam Pillai

If you are going to perform an operation which is common to several similar
shapes on a given slide, you might want to create a shape range instead.
--------------------------------------------------------------------------------------
Public oSldRng As ShapeRange

Sub Init()
' 1, 2, 3 represent the z-order value of the shapes on the slide
Set oSldRng = ActivePresentation.Slides(1).Shapes.Range(Array(1, 2,
3,6,7,8))
oSldRng.TextFrame.TextRange.Text = "00"
End Sub
 
S

Shyam Pillai

Since you are assigning shape objects to the array elements you need to use
the SET keyword.
......
With ActivePresentation.Slides("MySlide")
Set MyShapeArray(0) = .Shapes("MyShape0")
Set MyShapeArray(1) = .Shapes("MyShape1")
.......
 
S

Steve Rindsberg

Since you are assigning shape objects to the array elements you need to use
the SET keyword.
......
With ActivePresentation.Slides("MySlide")
Set MyShapeArray(0) = .Shapes("MyShape0")
Set MyShapeArray(1) = .Shapes("MyShape1")
.......

Slaps self ... HARD ... on forehead.

Of course.
 
A

Austin Myers

Well, this is the very reason I have you guys as my saftey net. <g>


Austin Myers
MS PowerPoint MVP Team

PowerPoint Video and PowerPoint Sound Solutions www.pfcmedia.com
 

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