object (accessed through the application name Visio). It returns the Documents collection.
Set oDoc = oDocs.Item(1)
Item is a property of the Documents collection (and of most other collections, too). It happens to be the default property, so you can omit Item but not (1). The rest of the code establishes a reference to each collection and collection member.
Set oPages = oDoc.Pages
Set oPage = oPages.Item(1)
Set oShapes = oPage.Shapes
Set oShape = oShapes.Item(1)
sShapeName = oShape.Name
The Name property of a shape returns a string, which can be assigned to the Text property of our text box.
txtShapeName.Text = sShapeName
Getting and Setting Properties
Once you get the object you want, you’ll probably become interested in its other properties. For example, in addition to the Name property, a Shape object has a Text property that returns its text, and a Type property that identifies the type of shape—whether it’s a group, a guide, or other kind of shape. Most properties that don’t return objects return strings or integers.
The PageSheet property of the page returns a reference to the page’s ShapeSheet. This is a Shape object that has Type=visTypePage.
You can set properties too, as long as they’re read-write or write-only.
Here are some examples of property types:
-
Read-only: A shape’s Sheet.#
-
Read-write: ShapeSheet cells
-
Write-only: Custom menu or toolbar sets
The Developer Reference help file contains a list of objects with their properties and methods. This reference also has an alphabetical list of properties and methods, whether they’re read-only, read-write, or write-only, and their return type.
Note:
|
When making an assignment to a variable of type Object, the Set keyword must be used. When making an assignment to a variable that is not an object (such as a string variable), the Set keyword is not used.
|
Invoking Methods
There were no methods used in the demonstration program of getting the shape’s name, but their syntax is similar to properties.
Methods often correspond to Visio commands. For example, a Shape object has a Copy method that performs the same action as selecting the shape and choosing Copy from the Edit menu in Visio.
Other methods correspond to actions. For example, a Window object has an Activate method that makes that window the active window.
If a method creates an object, it typically returns a reference to that object. Methods that don’t create objects typically don’t return anything.
Both properties and methods sometimes take arguments. For example:
-
A Shape object’s Cells property takes the name of the ShapeSheet cell to return.
-
The Documents collection’s Add method takes the name of a template on which to base a new document. Passing an empty string is equivalent to choosing File > New Drawing from the menu or New from the Toolbar.
Compound References
The examples in this course tend to declare an object variable and set it for every object in the model that we need a reference to. You don’t have to do this as you can concatenate Visio object references, properties, and methods as you can with Visual Basic objects.
However, there are some tradeoffs and sometimes, simple references are better. If you use any of the intermediate objects in your program—for example, if you’re going to be working with more than one shape from the Shapes collection—it makes sense to assign the intermediate objects to variables so you have them available for other uses. This requires less overhead than repeatedly calling a compound statement, and enhances performance.
A compound statement can also be harder to debug. Each expression is evaluated and its return value plugged into the statement before the next expression can be evaluated. If one fails, you’ll get an error at the statement but it may not be obvious where the failure occurred.