How to determine name of newly added shape

D

dcuthill

I add a shape to a document using the following code. How do I identify this
newly added shape in order to refer to it later? Can I name it - if so how?
Or can I get it's index and later refer to it by an index number or is the
index a dynamic number that change as other shapes are added to the document.

Set oShape = Rng.InlineShapes.AddOLEObject _
(ClassType:="Excel.Chart.8", _
FileName:="", _
LinkToFile:=False, _
DisplayAsIcon:=False)
 
D

Doug Robbins

I assume that you have dimmed oShape as a Shape. That being the case, just
refer to oShape.

--
Please respond to the Newsgroup for the benefit of others who may be
interested. Questions sent directly to me will only be answered on a paid
consulting basis.

Hope this helps,
Doug Robbins - Word MVP
 
D

dcuthill

Let's say I add the shape, save and close the document and then open it again
later. How do I know which shape was tghe one I added in the previous session
- especially if I have a number of such shapes? Can I not give the shape a
specific name when it is inserted?
 
D

Doug Robbins

Try assigning a bookmark to it. But YOU will have to remember the name of
the bookmark

--
Please respond to the Newsgroup for the benefit of others who may be
interested. Questions sent directly to me will only be answered on a paid
consulting basis.

Hope this helps,
Doug Robbins - Word MVP
 
J

Jean-Guy Marcil

dcuthill was telling us:
dcuthill nous racontait que :
Let's say I add the shape, save and close the document and then open
it again later. How do I know which shape was tghe one I added in the
previous session - especially if I have a number of such shapes? Can
I not give the shape a specific name when it is inserted?

As Doug wrote, with inline shapes the best bet is to use a bookmark. Set a
range to the inserted shape and assign a bookmark, use a constant in the
code for the bookmark name.

Otherwise, if you can work with floating shapes, the Name property is handy.
I have done this many times and it works like a charm later, as in:

'_______________________________________
Const ShapeName As String = "MyShapeName"

Dim oShape As Shape
Dim MyRange As Range

Set MyRange = Selection.Paragraphs(1).Range
Set oShape = ActiveDocument.Shapes.AddShape(Type:=msoShapeRectangle, _
Left:=50, Top:=50, Width:=100, Height:=200, Anchor:=MyRange)

oShape.Name = ShapeName

ActiveDocument.Shapes(ShapeName).Select
'_______________________________________

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
D

dcuthill

I have tried to implement your suggestions. This code is giving an error when
I try to assign the name to the shape "method or data member not found". Any
ideas?

Const ShapeName As String = "MyShapeName"
Dim xlApp As Excel.Application
Dim oWordDoc As Word.Document
Dim oShape As Word.InlineShape
Dim oGraphChart As Excel.Chart
Dim oWorkbook As Excel.Workbook
Dim oSheet As Excel.Worksheet
Dim Rng As Word.Range
Dim TabCnt As Long
Dim i As Long

WindowUpdating (False) 'to freeze the screen

Set oWordDoc = ActiveDocument
TabCnt = oWordDoc.Tables.Count

For i = 1 To TabCnt Step 4

Set Rng = ActiveDocument.Tables(i).Range
Rng.Copy
Rng.Collapse wdCollapseEnd
Rng.InsertAfter "" & vbCrLf
Rng.Collapse wdCollapseEnd

Set oShape = Rng.InlineShapes.AddOLEObject _
(ClassType:="Excel.Chart.8", _
FileName:="", _
LinkToFile:=False, _
DisplayAsIcon:=False)

oShape.Name = ShapeName <==== Method or data member not found
 
J

Jean-Guy Marcil

dcuthill was telling us:
dcuthill nous racontait que :
I have tried to implement your suggestions. This code is giving an
error when I try to assign the name to the shape "method or data
member not found". Any ideas?

Having a long day? :)

Here is what I wrote :
<quote>
As Doug wrote, with inline shapes the best bet is to use a bookmark. Set a
range to the inserted shape and assign a bookmark, use a constant in the
code for the bookmark name.

Otherwise, if you can work with floating shapes, the Name property is handy.
I have done this many times and it works like a charm later, as in:
<unquote>

I meant that the Name property is not available to inline shapes, which is
what you are using.
--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
D

dcuthill

Okay, okay, okay ... now I understand.

Jean-Guy Marcil said:
dcuthill was telling us:
dcuthill nous racontait que :


Having a long day? :)

Here is what I wrote :
<quote>
As Doug wrote, with inline shapes the best bet is to use a bookmark. Set a
range to the inserted shape and assign a bookmark, use a constant in the
code for the bookmark name.

Otherwise, if you can work with floating shapes, the Name property is handy.
I have done this many times and it works like a charm later, as in:
<unquote>

I meant that the Name property is not available to inline shapes, which is
what you are using.
--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 

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