Creating a Simple Drawing: an Example

3 4

Here's a program that demonstrates how you can traverse the object model using Document, Page, and Shape objects to create a drawing. This example creates Microsoft Visio objects as opposed to referring to Visio objects that already exist. This program follows these steps:

  1. Gets the first page in the Pages collection of the document associated with the Microsoft Visual Basic for Applications (VBA) project.
  2. Adds the stencil Basic Shapes.vssto the Documents collection.
  3. Drops an instance of the Rectangle master from the stencil onto the drawing page.
  4. Sets the text of the rectangle shape on the drawing page to "Hello World!"
  5. Saves the document.
 Sub HelloWorld ()       'Stencil document that contains master       Dim stnObj As Visio.Document       'Master to drop       Dim mastObj As Visio.Master       'Pages collection of document       Dim pagsObj As Visio.Pages       'Page to work in       Dim pagObj As Visio.Page       'Instance of master on page       Dim shpObj As Visio.Shape              'Get the first page in the document associated with the VBA        'program.       'A new document always has one page, whose index in the Pages                    'collection is 1.       Set pagsObj = ThisDocument.Pages       Set pagObj = pagsObj.Item(1)       'Get the stencil from the Documents collection and set the                 'master.       Set stnObj = Documents.Add("Basic Shapes.vss")       Set mastObj = stnObj.Masters("Rectangle")       'Drop the rectangle in the middle of a US letter-size page.       Set shpObj = pagObj.Drop(mastObj, 4.25, 5.5)       'Set the text of the rectangle.       shpObj.Text = "Hello World!"       'Save the drawing. The message pauses the program so you know the                'drawing is finished.       ThisDocument.SaveAs "Hello.vsd"       MsgBox "Drawing finished!", , "Hello World!" End Sub 

The drawing created by this program looks something like this.

figure 16-8. the drawing created by the hello world program.

Figure 16-8 The drawing created by the Hello World program.

This program uses the Visio object types, such as Visio.Document, Visio.Master, Visio.Pages, Visio.Page, and Visio.Shape, defined in the Visio type library. Using Visio object types instead of the general Object variable type increases the speed of your program.

This program uses object variables to hold references to the Visio objects. Each Set statement assigns an object reference to an object variable, starting with the ThisDocument object. Notice the progression from ThisDocument object to Pages collection to Page object.

Set pagsObj = ThisDocument.Pages uses the ThisDocument object, which is equivalent to the specific Document object associated with a VBA project. If you want to reference the Document object of the file associated with your VBA project, you don ' t need to get it from the Documents collection; just begin by referencing the ThisDocument object.

Set stnObj = Documents.Add("Basic Shapes.vss") doesn't reference an object higher in the Visio object model preceding Documents. Documents is a property of the Visio global object. The Visio global object has properties and methods you can reference with no qualifying object. For details about the Visio global object, see Chapter 15, Programming Visio with VBA.

Set shpObj = pagObj.Drop(mastObj, 4.25, 5.5) uses the Drop method to drop a master on the page represented by the pagObj variable. (Although you can draw shapes from scratch in a program, dropping a master is a far easier and more common technique.) The mastObj argument specifies the master to drop; 4.25, 5.5 are the page coordinates of the location to drop the pin (its center of rotation) of the new shape. These coordinates are measured from the lower-left corner of the drawing page in drawing units expressed as inches. The Drop method returns a reference to a Shape object—the new rectangle—that is assigned to the variable shpObj.

shpObj.Text = "Hello World!" assigns the string "Hello World!" to the Text property of the Shape object, which causes Visio to display that string in the rectangle. This is similar to selecting a shape with the pointer in a Visio drawing window and typing Hello World!

ThisDocument.SaveAs "Hello.vsd" uses the SaveAs method to save the ThisDocument object under the file name Hello.vsd. Because no folder path is specified, the document is saved in the working folder (probably the folder that contains the program). The MsgBox statement simply lets you know the drawing is finished. When you click OK in the message box, the program finishes.

To get details about any Visio object, property, method, or event, see the Microsoft Visio Developer Reference (on the Help menu, click Developer Reference). To find information about VBA programming, see the Microsoft Visual Basic Help, which is accessible from the Help menu in the Visual Basic Editor.



Developing Microsoft Visio Solutions 2001
Developing Microsoft Visio Solutions (Pro-Documentation)
ISBN: 0735613532
EAN: 2147483647
Year: 2004
Pages: 180

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net