Volume 3: Programming with the Visio Object Model disclaimer



Download 0.78 Mb.
Page8/36
Date29.01.2017
Size0.78 Mb.
#11974
1   ...   4   5   6   7   8   9   10   11   ...   36

A Sample Visio Program



Code sample conventions

Code samples in the demonstration files and code displayed on slides generally follow a simple Hungarian notation. Variable names start with a lower case letter or letters that designate the data type of the variable. Following the data type designator will be a name describing the variable that starts in upper case.

Below are some examples of variable names:



  • oDocs – a data type Object referencing a Documents collection

  • oDoc – a data type Object referencing a single Document

  • sShapeName – a data type String containing a shape’s name

  • txtShapeName – a text box control containing a shape’s name

  • dCellResult – a double precision variable containing the contents of a ShapeSheet cell
Try it! A Sample Visio Program

  1. Run an instance of Visio, and from your student CD open Demos\Vol3\ Automation and Visio\Sample Code.vsd.

  2. Right-click on the page to run the add-on GetShape (or from View > Macros > ThisDocument.GetShape). We will look at the VBA code for this add-on in step 5.

  3. Close the GetShape add-on.

  4. Press Alt+F11 to open Visual Basic for Applications.

  5. In the Project Window, open the Forms folder and right-click on frmGetShape. Select View Code and look at the following line:

Set oDocs = Visio.Documents

Documents is a property of the Application object and also of the 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.




Download 0.78 Mb.

Share with your friends:
1   ...   4   5   6   7   8   9   10   11   ...   36




The database is protected by copyright ©ininet.org 2024
send message

    Main page