How can I manipulate AutoShape properties from VBA?

G

Guest

I am trying to create an interactive presentation using VBA and ActiveX
controls, all of which is working well. However, I would like to have access
to AutoShape properties, much like I do with ActiveX controls so that I can
programatically change their name, fill color, position (Left, Top), and font
characteristics. Unfortunately, it does not look like the AutoShapes work
under the same object model as the ActiveX controls do, so I cannot access
their properties in the same way. Is there a way to do this? This would
enable me to do some fairly sophisticated animations using VBA code.
 
S

Steve Rindsberg

I am trying to create an interactive presentation using VBA and ActiveX
controls, all of which is working well. However, I would like to have access
to AutoShape properties, much like I do with ActiveX controls so that I can
programatically change their name, fill color, position (Left, Top), and font
characteristics. Unfortunately, it does not look like the AutoShapes work
under the same object model as the ActiveX controls do, so I cannot access
their properties in the same way. Is there a way to do this? This would
enable me to do some fairly sophisticated animations using VBA code.

To change the name of a selected shape:

ActiveWindow.Selection.ShapeRange(1).Name = "NewName"

To work with the shape, assuming it's on slide 1:

With ActivePresentation.Slides(1).Shapes("NewName")
.Left = 0
.Top = 0
.TextFrame.TextRange.Text.Font.Name = "Something Else"
' and so on
End With
 
G

Guest

Steve Rindsberg said:
To change the name of a selected shape:

ActiveWindow.Selection.ShapeRange(1).Name = "NewName"

To work with the shape, assuming it's on slide 1:

With ActivePresentation.Slides(1).Shapes("NewName")
.Left = 0
.Top = 0
.TextFrame.TextRange.Text.Font.Name = "Something Else"
' and so on
End With

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

Steve,

Thanks for your reply, but a few things remain unclear to me. The question
is, how do I know which shape is "ShapeRange(1)"? Is the code you provided
something that runs at design time, such that I have to run it somehow when a
particular shape is selected?

On my slide, I could have as many as 20 or more rectangles or other objects
that I need to manipulate. How do I know ahead of time how to refer to it so
that I can change its properties? Which one is "ShapeRange(1)"?

Also, does "Slides(1)" refer to the "page number" of the slide, or its name
in the properties window within the VBA editor? From within the VBA editor,
slides seem to be named almost randomly: slide42, slide 247, slide 63.

These things have baffled me for awhile now, and no amount of reading I have
done on the Microsoft web site (or any other) has been able to clear things
up for me.

Microsoft could have made this so much easier if all objects on a slide were
treated the same way as objects on a VisualBasic form within the VisualStudio
environment.

Thanks again for your help. I really appreciate it.
 
S

Steve Rindsberg

Steve,

Thanks for your reply, but a few things remain unclear to me. The question
is, how do I know which shape is "ShapeRange(1)"? Is the code you provided
something that runs at design time, such that I have to run it somehow when a
particular shape is selected?

Right. The first line of code simply lets you set the name of a selected shape so
that you can access it later using code like that in the next few lines of code.

By giving the shape a name in advance, it's much simpler to manipulate it directly
later rather than keeping track of numbers.
On my slide, I could have as many as 20 or more rectangles or other objects
that I need to manipulate. How do I know ahead of time how to refer to it so
that I can change its properties? Which one is "ShapeRange(1)"?

You tell me. <g> What are the criteria you use to determine what needs to be
manipulated and what doesn't?

You might want to pre-name the shapes so you can manipulate them directly

You might want to check all of the shapes on a slide and change only the ones that
meet some set of criteria - find all the RGB(0,0,255) ones and move them into the
upper left corner, for example.

You might want to get the first shape on the slide and make it the last or vice
versa.

Also, does "Slides(1)" refer to the "page number" of the slide, or its name
in the properties window within the VBA editor?

It refers to the slide's .Index property ... that is the ordinal number of the slide
within the presentation. In short, if it's the second slide in the presentation,
it will have an .Index property of 2, so it'd be .Slides(2)

From within the VBA editor,
slides seem to be named almost randomly: slide42, slide 247, slide 63.

They're given names in order of creation, but if somebody reorders the slides in the
presentation, the names don't change to reflect that.
These things have baffled me for awhile now, and no amount of reading I have
done on the Microsoft web site (or any other) has been able to clear things
up for me.

Microsoft could have made this so much easier if all objects on a slide were
treated the same way as objects on a VisualBasic form within the VisualStudio
environment.

Sure, it's always easier if the model we understand is used universally, but that
doesn't necessarily make it the best model for all possible uses. I might
understand chainsaws really well, but when the job calls for welding ... um. Darn.
;-)

But I suppose it might help to consider that:

A VB project has a forms collection; on each form there are controls and the
controls have properties. You can refer to Form(n).Controls(x).Property for
example.

In PowerPoint, a presentation has a Slides collection and on each slide there are
Shapes. You can work through Slides(n).Shapes(x).Properties in the same way.
Thanks again for your help. I really appreciate it.

No problem. I'm sure you'll have more questions ... don't hesitate.
 
G

Guest

Steve,

Thanks for all your help. I think I understand this much better now.

My point of comparison to VB is that it would be nice if shapes could have
their properties displayed in the properties window for easy editing of
initial values like their VB control counterparts.

I guess I'll just have to write my own properties box to control things like
this more easily. :)
 
S

Steve Rindsberg

My point of comparison to VB is that it would be nice if shapes could have
their properties displayed in the properties window for easy editing of
initial values like their VB control counterparts.

Ah, now THAT would be a nice touch. There are a couple of add-ins that address
parts of this problem; Shyam Pillai and Chirag Dalal each have them; one of
mine displays some of the shape properties but doesn't permit editing values.
I guess I'll just have to write my own properties box to control things like
this more easily. :)

Sounds like a salable add-in to me!
 

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