How to add a shape from a stencil to a shape on the document?

M

Mozzy

Hi there,
I am developing a Visio 2003 add-in using the Visio 2003 SDK.
After developing a custom shape that has a QUEUEMARKEREVENT on it (The shape
consist of a group of 4 chair shapes and one table shape and with each chair
on a different layer and the whole group contains user defined cells and
actions) I have made a POC and came to following.

The POC should show how to add/remove chairs to the table in the shape.
Until now I was only successful in adding chairs by duplicating existing
chairs in the destination layer (each layer represents one side of the table)
and deleting chairs by calling Shape.Delete.
My problem now is that if I delete all shapes on one side I am unable to add
again a shape. When trying to get the Chair shape from my stencil (
MyNewChair = Application.Documents("MyStencil.vss").Item("Chair").Shapes(1)
and then calling MyMainShape.Layer(2).Add(MyNewChair, 1) ) it fails with the
error message "Inappropriate target object for this action."

I have been searching the whole day in groups for an answer to that but now
I am out of ideas and need direct help.
 
P

Paul Herber

Hi there,
I am developing a Visio 2003 add-in using the Visio 2003 SDK.
After developing a custom shape that has a QUEUEMARKEREVENT on it (The shape
consist of a group of 4 chair shapes and one table shape and with each chair
on a different layer and the whole group contains user defined cells and
actions) I have made a POC and came to following.

The POC should show how to add/remove chairs to the table in the shape.
Until now I was only successful in adding chairs by duplicating existing
chairs in the destination layer (each layer represents one side of the table)
and deleting chairs by calling Shape.Delete.
My problem now is that if I delete all shapes on one side I am unable to add
again a shape. When trying to get the Chair shape from my stencil (
MyNewChair = Application.Documents("MyStencil.vss").Item("Chair").Shapes(1)
and then calling MyMainShape.Layer(2).Add(MyNewChair, 1) ) it fails with the
error message "Inappropriate target object for this action."

You aren't creating your new shape correctly.
First reference the shape you wish to use
Declare MyNewChair as a master
MyNewChair = Application.Documents("MyStencil.vss").Masters("Chair")
 
M

Mozzy

The problem is that the Layer.Add()
takes type shape as parameter.
So I had to use
Dim MyChair as Shape =
Application.Documents("MyStencil.vss").Masters("Chair").Shape(1)
 
P

Paul Herber

The problem is that the Layer.Add()
takes type shape as parameter.
So I had to use .
Dim MyChair as Shape =
Application.Documents("MyStencil.vss").Masters("Chair").Shape(1)

But you haven't yet got a shape to add.
Once you have a reference to the master, then you can add a shape to
your diagram.
shapeObj = currentPage.Drop(MyNewChair, 0, 0)

You now have a shape on the page, you can set its location and size
etc.
shapeObj is the reference to the shape. Now you can use the
Layer(2).add to add the shape.
 
M

Mozzy

Sorry...am feeling pretty stupid right now cuz it didn't work

Dim Chair =
Table.Application.Documents("SeatingShapes.vss").Masters("Chair").Shapes(1)
Table.Application.ActivePage.Drop(Chair, 0, 0)
Table.Master.Layers(2).Add(Chair, 1)

It still gives me the same exception put now it adds the shape to the page.
 
J

John Marshall, MVP

See Paul's reply

--
John... Visio MVP

Need stencils or ideas? http://www.mvps.org/visio/3rdparty.htm
Need VBA examples? http://www.mvps.org/visio/VBA.htm
Common Visio Questions http://www.mvps.org/visio/common_questions.htm
Visio Wishlist http://www.mvps.org/visio/wish_list.htm

Mozzy said:
Sorry...am feeling pretty stupid right now cuz it didn't work

Dim Chair =
Table.Application.Documents("SeatingShapes.vss").Masters("Chair").Shapes(1)
Table.Application.ActivePage.Drop(Chair, 0, 0)
Table.Master.Layers(2).Add(Chair, 1)

It still gives me the same exception put now it adds the shape to the
page.
 
P

Paul Herber

Sorry...am feeling pretty stupid right now cuz it didn't work

Dim Chair =
Table.Application.Documents("SeatingShapes.vss").Masters("Chair").Shapes(1)
Table.Application.ActivePage.Drop(Chair, 0, 0)
Table.Master.Layers(2).Add(Chair, 1)

It still gives me the same exception put now it adds the shape to the page.

Remove the .Shapes(1)
Chair should be a Master, not a Shape.
 
M

Mozzy

Thanks to both of you for your effort...
Your answers sound to me actually logical and understandable....but it seems
that Visio has a different opinion (or I am actually right now totally koko
;) have been working on this problem since morning)

I tried now following but it still gives me the same error ;S

Dim ChairMaster As Visio.Master =
Table.Application.Documents("SeatingShapes.vss").Masters("Chair")

Chair = Table.Application.ActivePage.Drop(ChairMaster, 0, 0)

Table.Master.Layers(2).Add(Chair, 0)
 
P

Paul Herber

Thanks to both of you for your effort...
Your answers sound to me actually logical and understandable....but it seems
that Visio has a different opinion (or I am actually right now totally koko
;) have been working on this problem since morning)

I tried now following but it still gives me the same error ;S

Dim ChairMaster As Visio.Master =
Table.Application.Documents("SeatingShapes.vss").Masters("Chair")

Chair = Table.Application.ActivePage.Drop(ChairMaster, 0, 0)

Table.Master.Layers(2).Add(Chair, 0)

Table.Master can't be right.
I'm trying to work out what you want to do. Do you want to add shape
Chair to the shape Table and incorporate it within Table i.e. add
Chair to the group Table?
 
M

Mozzy

Yes exactly. There is one issue though that the Table shape does not belong
to the layer that I want the Chair shape to be added to that is why I used
the Master. However, overnight I was thinking of adding the Table shape to
all Side layers so that I can access them directly from the Shape.
I will try it out and let you know :)
 
M

Mozzy

Alright that didn't work either, at least not as I expected it to behave.
It stopped giving me an exception BUT the shape did not become a member of
the shape group :(

However I have a workaround for this. I just added a new layer to the
complete shape group which's visibility is false. There I added a chair shape
so that I always will have a chair that I can duplicate and add to other
layers of the shape.

Now this works just fine.

Again thanks guys for your help :)
 

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