How to change parent of a shape which is inside a group to a page?

Started by nashwaan, May 29, 2012, 10:30:30 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

nashwaan

The Parent property of a shape is Read/Write. A shape can be placed inside another shape provided that the other shape is of type Group.
According to Visio help on Parent property, you can also change the parent of a shape from a group to a page. I assume this means you can place a shape, which is inside another shape, directly on a page.

I tried to test the latter scenario, but an error generates!

Here is the sample code:

Sub testChangeParent1()
    Dim shp1 As Shape, shp2 As Shape, shpGroup As Shape
    Set shp1 = ActivePage.DrawRectangle(1, 1, 3, 3)
    Set shp2 = ActivePage.DrawOval(2, 2, 4, 4)
    Call ActiveWindow.Select(shp1, visSelect + visDeselectAll)
    Call ActiveWindow.Select(shp2, visSelect)
    Set shpGroup = ActiveWindow.Selection.Group
    shp1.Parent = ActivePage.PageSheet   ' <----- generates error: "An exception occured"
End Sub


Does anyone know why?

Thanks,
Yousuf.
Give me six hours to chop down a tree and I will spend the first four sharpening the axe — Abraham Lincoln

Jumpy

The Parent is not the PageSheet, but the page itself. Maybe that is wrong?

nashwaan

I tried that and it generated same error!

I believe this should be ActivePage.PageSheet and not ActivePage.

From Visio Help (Shape.Parent):

In general, an object's parent is the object that contains it. If a Shape object is a member of a group, the parent is that group. Otherwise, its parent is a Page or a Master object.
When assigning a new parent shape, you must assign a Shape object. If you want to assign a page or master to be the parent of a shape, you must assign the Shape object returned by the Page or Master object's PageSheet property.


Any Idea why the code in my first post fails?

Thanks,
Yousuf
Give me six hours to chop down a tree and I will spend the first four sharpening the axe — Abraham Lincoln

Jumpy

I can only test it this evening, but
maybe it works the complicated way?:

Sub PagePageSheet_Example()
Dim vsoShape As Visio.Shape
Dim vsoShapes As Visio.Shapes
Dim vsoPage As Visio.Page
Set vsoPage = ActivePage
Set vsoShapes = vsoPage.Shapes
Set vsoShape = vsoShapes("ThePage")
'...
shp1.Parent = vsoShape
End Sub



or (although I can't think why) it has something to do with the last sentence of the help, that you didn't quote:

Quote
A shape and its parent shape must be in the same containing page or containing master. If the new parent is not a Shape object, or if the ContainingPage or ContainingMaster property of the parent shape is different from that of the shape, Microsoft Visio raises an exception.


nashwaan

Sorry for late reply...

I tried your suggestion and it didn't work either.  :-\

Any more suggestion?   :'(

Thanks,
Yousuf.
Give me six hours to chop down a tree and I will spend the first four sharpening the axe — Abraham Lincoln

AndyW

This will work, by selecting your sub-shape and then removing that from the group.

    Visio.ActiveWindow.DeselectAll
    Visio.ActiveWindow.Select Application.ActiveWindow.Page.Shapes.ItemFromID(2), visSubSelect
    Visio.ActiveWindow.Selection.RemoveFromGroup
Live life with an open mind

nashwaan

Thanks AndyW for your reply. Your workaround suggestion works without a problem.

But still, why .Parent property doesn't work when changing a shape's parent from a group to a page. This should work as this is advertised in the Visio Help for Shape.Parent (unless i am missing something here). ???

Anyone has tried this with success?  :-\

Thanks,
Yousuf.
Give me six hours to chop down a tree and I will spend the first four sharpening the axe — Abraham Lincoln