Using Properties and Methods

Once you have a reference to an object, you can get and set the values of its properties or use methods that cause the object to perform actions. For properties and methods that have return values or arguments, you must declare variables in your program. These variables can be object references, or they can be values such as strings or numbers. For example, the value of a Shape object's Text property is a string—the text of the shape.

In this section…

Declaring Variables for Return Values and Arguments

Getting and Setting Properties

Using an Object's Default Property

Using Methods

Declaring Variables for Return Values and Arguments

When declaring a variable for a return value from a property or method, declare the variable with either an explicit data type or the Visual Basic Variant data type, and use a simple assignment statement to assign the value to the variable.

When declaring a variable for an argument to a property or method, the same rules apply: Use an object variable for an object (using a specific type of object is preferable), and use either the Variant data type or the appropriate explicit data type for other kinds of values.

For details about declaring object variables, see Declaring Object Variables in Getting and Releasing Visio Objects earlier in this chapter. In addition, all property and method arguments are described in the Automation Reference in the Microsoft Visio Developer Reference (on the Help menu, click Developer Reference).

TOP

Getting and Setting Properties

Properties often determine an object's appearance. For example, the following statement sets the Text property of a Shape object:

shpObj.Text = "Hello World!"

The following statement gets the text of this shape:

shpText = shpObj.Text

Some properties take arguments. For example, the Cells property of a Shape object takes a string expression that specifies a particular cell in the corresponding shape. When a property takes arguments, enclose them in parentheses. For example, the following statement sets the formula of the PinX cell.

shpObj.Cells("PinX").Formula = "4.25 in"

The following statement gets the formula of the PinX cell and stores it in strPinX:

strPinX = shpObj.Cells("PinX").Formula

Most properties of Visio objects are read/write, which means you can both get and set the property's value. Certain properties are read-only—you can get them, but you cannot set them. For example, you can get the Application property of an object to determine the Visio instance that contains the object, but you cannot set the Application property to transfer the object to a different instance.

A few properties are write-only—you can only set their values. Such properties usually handle a special case for a corresponding read/write property. For example, you change the formula in a cell by setting its Formula property, unless the formula is protected with the guard function. In that case, you must use the FormulaForce property to set the formula. However, you cannot get a cell's formula by using FormulaForce; you must use Formula, whether the cell's formula is protected.

For details about properties and methods, including whether a property is read/write, read-only, or write-only, see the Microsoft Visio Developer Reference (on the Help menu, click Developer Reference).

TOP

Using an Object's Default Property

Most objects have a default property that is used if you don't specify a property when referring to that object. For example, the default property of a Document object is Name, so the following two statements return the same value:

'Long format docName = Documents.Item(5).Name 'Short format docName = Documents.Item(5)

The default property for most collections is Item, so you can use a statement such as the following to specify an object from a collection:

'Long format Set shpObj = shpsObj.Item(1) 'Short format Set shpObj = shpsObj(1)

To determine the default property for any Visio object, search for that object in the Microsoft Visio Developer Reference (on the Help menu, click Developer Reference). The default property for a Visio object is also marked with a blue dot in the Object Browser. For details about the Object Browser, see Using the Object Browser in Chapter 15, Programming Visio with VBA.

TOP

Using Methods

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 clicking the Copy command on the Edit menu in Visio. Other methods correspond to other actions. For example, a Window object has an Activate method that you can use to make the corresponding window active, which is the same as clicking that window with the mouse.

The syntax for using a method is similar to that for setting a property. If a method creates an object, like a Page object, the method returns a reference to the newly created object, as in the following example. (Methods that don't create objects typically don't return values.)

Dim pagObj As Visio.Page Set pagObj = pagsObj.Add