Naming shapes in VBA

R

Roderick O'Regan

I've added a picture using the following statement:
ActiveDocument.Shapes.AddPicture(MyFileName, msoFalse, msoTrue, 52, 14, 527,
388.3).Select

What I want to do now is give that graphic a name (e.g. MyPicture) and then
go back to it by name at a later time to delete it.

First of all, can I give a shape, such as a picture, a name? I've tried
using the Selection.ShapeRange.Name="MyPicture" command but don't know
whether that is correct. If it is, then how con I select it by name, please?

Regards

Roderick
 
P

Peter Hewett

Hi Roderick O'Regan

You can do it this way:
Public Sub ShapeTest()
Dim MyFileName As String
Dim shpNew As Word.Shape

MyFileName = "c:\bath.jpg"
Set shpNew = ActiveDocument.Shapes.AddPicture(MyFileName, msoFalse, msoTrue, 52, 14,
527, 388.3)
shpNew.Name = "MyShapeName"
End Sub

Just a couple of things to be aware of. When you create a new shape it's always assigned
a name. If you change the shapes name it must be unique. To select a previously named
shape you can use something like:

Dim shpNew As Word.Shape
Set shpNew = ActiveDocument.Shapes("MyShapeName")

HTH + Cheers - Peter
 
H

Helmut Weber

Hi Roderick,
I guess, there is nothing wrong with your approach.
ActiveDocument.Shapes.AddPicture("c:\test\test.jpg", _
msoFalse, msoTrue, 20, 20, 20, 20).Select
With Selection
.ShapeRange.Name = "MyPic 2"
.WholeStory
.Collapse
End With
ActiveDocument.Shapes("MyPic 2").Select
Greetings from Bavaria, Germany
Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word XP, Windows 2000
 
R

Roderick O'Regan

Thanks to both of you. Your replies were of great value to me and worked
perfectly.

Regards

Roderick
 

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