Working with Selected Shapes

You can access a shape's properties and methods from a program whether the shape is selected or not. You can also create a Selection object to work with multiple shapes. A Selection object is similar to a Shapes collection in that it represents a set of Shape objects and has an Item and a Count property. Unlike a Shapes collection, a Selection object represents only shapes that are selected.

Selection object and related objects higher in the Visio object model

Selection object and related objects higher in the Visio object model

You can get a Selection object that represents shapes that are selected in a window, or create a Selection object that represents shapes you specify from any Shapes collection. The order of items in a Selection object follows the order in which the items were selected. The first item returned is the first shape selected.

In this section…

Getting Shapes that are Selected in a Window

Adding and Removing Shapes in Selections

Selecting and Deselecting Shapes in a Window

Performing Operations on Selected Shapes

Determining a Selection's Scope

Getting Shapes that are Selected in a Window

To work with shapes the user has selected in a window, get the Selection property of that Window object.

Window.Selection represents shapes selected in a drawing window

Window.Selection represents shapes selected in a drawing window.

The following example gets the Selection object of the active window:

selectObj = ActiveWindow.Selection

If all of the shapes on a page are selected, the Selection object of the window and the Shapes collection of the page are the same set of shapes. If nothing is selected, the Selection object is empty and its Count property returns zero (0). If your program requires a selected shape, you might check the Selection property of the active window to make sure it contains at least one object, such as in the following example:

Set selectObj = ActiveWindow.Selection If selectObj.Count = 0 Then     MsgBox "You must select a shape first." Else     'Continue processing End If

A Selection object retrieved from a window represents the shapes selected in the window. Subsequent operations that change the selection in the window have no effect on the retrieved Selection object.

If you have more than one window showing the same drawing page, you can have different shapes selected in each window.

By default, a Selection object reports only selected shapes and groups; shapes subselected within a group are not included. To report subselected shapes (a shape that is a child of a group) or a group that is a parent of a subselected shape, you can modify the default settings of a Selection object's IterationMode property. For details about the Selection object and its properties, see the Microsoft Visio Developer Reference (on the Help menu, click Developer Reference).

TOP

Adding and Removing Shapes in Selections

To add an item to a selection, use the Selection object's Select method and specify the Shape object to select. You can add a shape to a selection or remove it from the selection of a shape without affecting the other selected shapes.

The constants visSelect and visDeselect, defined in the Visio type library, control the action that is performed. For example, the following statement adds a shape to those already in the Selection object:

selObj.Select shpObj,visSelect

The following statement removes a shape from a selection:

selObj.Select shpObj,visDeselect

The following statement makes a selection include exactly one shape:

selObj.Select shpObj, visSelect + visDeselectAll

TOP

Selecting and Deselecting Shapes in a Window

To select a shape in a window from a program, use the Select method of a Window object and specify the Shape object to select. You can add a shape to a selection or remove a shape from a selection without affecting the other selected shapes. For example, the following statement adds a shape to those already selected in a drawing window:

winObj.Select shpObj,visSelect

To select all the shapes on a drawing page, use the Window object's SelectAll method; to deselect all selected shapes, use the DeselectAll method. If you get a Selection object after using SelectAll, the new Selection object includes a Shape object for each shape on the drawing page displayed in that window. If you get a Selection object after using DeselectAll, the new Selection object is empty.

TOP

Performing Operations on Selected Shapes

After you have a Selection object, you can perform operations on the selected shapes, similar to the actions you can perform in a drawing window.

For example, you can use the Copy, Cut, Delete, or Duplicate method of a Window or Selection object to copy, cut, delete, or duplicate selected shapes:

selectObj.Delete

Or you can perform Boolean operations such as union, combine, and fragment using the Union, Combine, and Fragment methods. These methods correspond to the Union, Combine, and Fragment commands in Visio, which create one or more new shapes that replace the selected shapes:

selectObj.Union

Before using Union, Combine, or Fragment, make sure that only the shapes you want to affect are selected. These methods delete the original shapes, so any smart formulas in the original shapes are lost and the Selection object that represents the shapes is no longer current.

For details about what you can do with a Selection object as well as any of the objects, properties, or methods discussed here, see the Microsoft Visio Developer Reference (on the Help menu, click Developer Reference).

TOP

Determining a Selection's Scope

To find out whether a Selection object gets its shapes from a Page object, a Master object, or a Shape object (group), check the Selection object's ContainingPage, ContainingMaster, and ContainingShape properties.

  • If the shapes are on a page, the ContainingPage property returns that Page object, and ContainingMaster returns Nothing.
  • If the shapes are in a master, the ContainingMaster property returns that Master object, and ContainingPage returns Nothing.
  • If the shapes are in a group, the ContainingShape property returns a Shape object that represents the group. Otherwise, this property returns a Shape object that represents the page sheet of the master or page that contains the shapes.