add shape and immediatly name and set properties

  • Thread starter Thread starter MeSteve
  • Start date Start date
M

MeSteve

I am trying to automate the creation of a slide show and need to create
shapes. I can create shapes and I can modify shapes that I know the name or
reference of. how do I add a shape and immediatly set the name so I can
continue to modify its properties? The problem as I am seeing it is the
AddShape method is part of Shapes whereas Name method is part of Shape and
requires a reference. Here is what I have so far.

With ppt
With Slides(1)
With Shapes
.AddShape Type:=msoShapeRectangle, Left:=10, Top:=0, Width:=100,
Height:=58
End With
With Shape(1)
.Name = "Title"
End With
End With
 
Hi,

This worked for me in ppt2003,

With ActivePresentation.Slides(1)
With .Shapes.AddShape(Type:=msoShapeRectangle, Left:=10, Top:=0,
Width:=100, Height:=58)
.Name = "Title"
End With
End With

Cheers
Andy
 
Try using an object variable

Sub addashape()
Dim oshp As Shape 'object variable
'set oshp to added shape
Set oshp = ActivePresentation.Slides(1).Shapes _
..AddShape(msoShapeRectangle, 10, 0, 100, 58)
'use oshp as reference
With oshp
..Name = "myshape"
'any other properties here
End With
End Sub
--
Amazing PPT Hints, Tips and Tutorials

http://www.PPTAlchemy.co.uk
http://www.technologytrish.co.uk
email john AT technologytrish.co.uk
 
AddShape is returning you the reference to the shape right? Can't you use
that one?

With ActivePresentation
Dim s As Shape
With .Slides(1)
With .Shapes
Set s = .AddShape(msoShapeRectangle, 10, 0, 100, 58)
End With
With s
.Name = "Title"
End With
End With
End With
 
The other repsonses have answered your question, so I thought I would
jump in with a word of explanation. Some procedures/functions/methods in
VBA can return a value or not. AddShape is one of those. If you put the
parameters in parentheses, it tells VBA that you want to return a value.
If you don't, it tells VBA that you don't want to return a value. (For
the procedures that can't return a value, the parentheses are
extraneous; for the ones that must return a value, the parentheses are
required.) In your example, you don't use parentheses, and you don't
expect a value to be returned. In all the answers, parentheses are used,
and the value that is returned is the shape that you are adding. Because
this is an object (not just a number or text or something), you assign
that value to a variable by using Set (instead of just using the equal
sign). That is why

....AddShape paremeter, paremeter, ...

works just fine but leaves you with a shape that you can't access while

Set oShp = ....AddShape(parameter, parameter, ...)

also works and puts the new shape into oShp.

This is one of the confusing things for a VBA newbie, and I wish I
understood it better so I could have explained it better when I wrote my
book.

--David
 
Back
Top