none
Visio Group Shapes not working in VB.net RRS feed

  • Question

  • I'm using Visual Studio, VB.Net, to develop a VSTO addin for Visio.  I'm trying to make a legend, which comprises 10 boxes on top of a border shape. .  I'm trying to group the shapes programmatically.

    Boxes() is an array of Visio.Shape

    borderBox is a Visio.Shape

    To group shapes, I'm trying this, but it doesn't group the shapes when I test it:

    Dim groupShape as Visio.Shape

    Dim thisSelection as Visio.Selection

    thisSelection = Globals.ThisAddIn.Application.ActiveWindow.Selection

    thisSelection.Select(borderBox, Visio.VisSelectArgs.VisDeselectAll + Visio.VisSelectArgs.visSelect)

    For each box in Boxes

    thisSelection.Select(box, Visio.VisSelectArgs.visSelect)

    Next

    groupShape = thisSelection.Group()

    Any suggestions?


    Gina


    • Edited by Gina1111 Friday, February 7, 2014 4:43 PM left off last line of code accidentally
    Friday, February 7, 2014 4:33 PM

Answers

  • Hi Gina,

    It looks like you are interested in the AddToGroup method of the Selection class from the Visio object model. It adds the selected shapes to the selected group. Here is what MSDN states for the method:

    The current selection must contain both the shapes to add and the group to which you want to add them. The group must be the primary selection or the only group in the selection.

    Here is a sample VBA macro I found for illustrating the required functionality:

    Public Sub AddToGroup_Example() 
     
     Application.ActiveWindow.Page.Drop Application.Documents.Item("BASIC_U.VSS").Masters.ItemU("Square"), 3, 8 
     
     Application.ActiveWindow.Page.Drop Application.Documents.Item("BASIC_U.VSS").Masters.ItemU("Pentagon"), 4, 8 
     
     Application.ActiveWindow.SelectAll 
     
     ActiveWindow.DeselectAll 
     ActiveWindow.Select Application.ActiveWindow.Page.Shapes.ItemU("Pentagon"), visSelect 
     ActiveWindow.Select Application.ActiveWindow.Page.Shapes.ItemU("Square"), visSelect 
     ActiveWindow.Selection.Group 
     
     Application.ActiveWindow.Page.Drop Application.Documents.Item("BASIC_U.VSS").Masters.ItemU("Ellipse"), 5, 6 
     
     ActiveWindow.DeselectAll 
     ActiveWindow.Select Application.ActiveWindow.Page.Shapes.ItemU("Ellipse"), visSelect 
     ActiveWindow.Select Application.ActiveWindow.Page.Shapes.ItemU("Sheet.3"), visSelect 
     ActiveWindow.Selection.AddToGroup 
     
    End Sub 
    

    Finally, I've noticed that you don't release underlying COM objects instantly. For example:

    thisSelection = Globals.ThisAddIn.Application.ActiveWindow.Selection

    The ActiveWindow property of the Application class from the Visio object model returns an instance of the Window class which should be released after. Then you call the Selection property of the Window class which returns an instance of the corresponding class. It should be released too.

    Use System.Runtime.InteropServices.Marshal.ReleaseComObject to release an Visio object when you have finished using it. Then set a variable to Nothing in Visual Basic (null in C#) to release the reference to the object.

     
    • Marked as answer by Gina1111 Friday, February 7, 2014 6:29 PM
    Friday, February 7, 2014 6:10 PM

All replies

  • One problem is that the code is not adding my shapes to "thisSelection."

    Gina

    Friday, February 7, 2014 5:33 PM
  • Hi Gina,

    It looks like you are interested in the AddToGroup method of the Selection class from the Visio object model. It adds the selected shapes to the selected group. Here is what MSDN states for the method:

    The current selection must contain both the shapes to add and the group to which you want to add them. The group must be the primary selection or the only group in the selection.

    Here is a sample VBA macro I found for illustrating the required functionality:

    Public Sub AddToGroup_Example() 
     
     Application.ActiveWindow.Page.Drop Application.Documents.Item("BASIC_U.VSS").Masters.ItemU("Square"), 3, 8 
     
     Application.ActiveWindow.Page.Drop Application.Documents.Item("BASIC_U.VSS").Masters.ItemU("Pentagon"), 4, 8 
     
     Application.ActiveWindow.SelectAll 
     
     ActiveWindow.DeselectAll 
     ActiveWindow.Select Application.ActiveWindow.Page.Shapes.ItemU("Pentagon"), visSelect 
     ActiveWindow.Select Application.ActiveWindow.Page.Shapes.ItemU("Square"), visSelect 
     ActiveWindow.Selection.Group 
     
     Application.ActiveWindow.Page.Drop Application.Documents.Item("BASIC_U.VSS").Masters.ItemU("Ellipse"), 5, 6 
     
     ActiveWindow.DeselectAll 
     ActiveWindow.Select Application.ActiveWindow.Page.Shapes.ItemU("Ellipse"), visSelect 
     ActiveWindow.Select Application.ActiveWindow.Page.Shapes.ItemU("Sheet.3"), visSelect 
     ActiveWindow.Selection.AddToGroup 
     
    End Sub 
    

    Finally, I've noticed that you don't release underlying COM objects instantly. For example:

    thisSelection = Globals.ThisAddIn.Application.ActiveWindow.Selection

    The ActiveWindow property of the Application class from the Visio object model returns an instance of the Window class which should be released after. Then you call the Selection property of the Window class which returns an instance of the corresponding class. It should be released too.

    Use System.Runtime.InteropServices.Marshal.ReleaseComObject to release an Visio object when you have finished using it. Then set a variable to Nothing in Visual Basic (null in C#) to release the reference to the object.

     
    • Marked as answer by Gina1111 Friday, February 7, 2014 6:29 PM
    Friday, February 7, 2014 6:10 PM
  • Eugene,

    Thank you.  The above code (adapted for vb.Net) worked!  It did not work when I included the line,

    ActiveWindow.DeselectAll

    between each addition of a shape.  With this it only grouped the first two shapes, but not the last 9.

    But when I deleted that line, it worked and grouped everything fine.

    I don't understand the part about releasing the ActiveWindow property yet, but I'll study it.


    Gina

    Friday, February 7, 2014 6:29 PM
  • Good news!

    Please see the Office application does not quit after automation from Visual Studio .NET client article for more information on releasing COM objects.

    Friday, February 7, 2014 6:34 PM
  • Great.  Thank you!

    Gina

    Friday, February 7, 2014 8:16 PM