Design-Drawing Home  
Drawing Program
ISSN 1441-5585

Search...

Home
Articles
Software Catalog
Book Store
About
Advertising
Newsletter

 

 

Learnin' Tae Automate

David A Edson

Greetin’s an’ welcome bak tae ye a’!!! Weel ‘tis spring and I thought that I would take this opportunity to give a wee bit o’ assistance to those o’ ye that might be new to Visio development. Consider it a wee bit o’ joy at the better, langer deys, but ma sincere desire is that I can assist ye in takin’ Visio beyond the box an’ begin developin’ yer own solutions.

You are aware that every shape in Visio is controlled by it’s own ShapeSheet. This Excel-like spreadsheet contains numerous cells. Each of these cells control some aspect of the shape’s appearance or behavior. I encourage each of ye to open up the ShapeSheet for shapes in drawings and analyse what each cell does and how changing the formulas or results (values) relates to the shape that is referenced.

To make this learning process easier I shall concentrate on the Height and Width cells of a given shape. Ye are aware that when you open up a ShapeSheet and directly enter a new formula or value into the Height or Width cell, the physical size of the shape is altered. Conversely, when ye "pull on the green things", that is, click-and-drag-and release on one of the shape’s sizing handles, the values in the Height and/or Width cells change as weel. This is because the shape and the ShapeSheet cells are actually two views of exactly the same data.

How then do we manipulate these cells under automation? Weel before ye can begin tae manipulate the Height or Width cells of a given shape you must understand how tae traverse the hierarchy of the Visio object model. I have attached a copy of a Visio Object Model drawing to make it easier for you to visually see the structure I am discussing here.

If you are creating an application in C, C++, or Visual Basic or any other external Automation Controller, you must first obtain a reference to the Visio Application object. There are two Automation calls to do this: GetObject() and CreateObject().

GetObject() assumes that Visio is already running. If Visio is not running, the call will fail and your application crashes. If Visio is running you get a reference to the application object which you store in a previously declared object variable. Here is an example of getting the Visio application object for a running instance of Visio:

Public Sub Foo(
Dim appVisio As Object ‘declaration of the variable appVisio as type object
Set appVisio = GetObject("Visio.Application")
End Sub

CreateObject() makes no assumption about whether Visio is running or not. If Visio is running, CreateObject() will launch an entirely new instance of Visio. If Visio is not running, CreateObject() will launch an instance of Visio. Here is an example of getting a reference to the new instance of Visio using CreateObject():

Public Sub Foo(
Dim appVisio As Object ‘declaration of the variable appVisio as type object
Set appVisio = CreateObject("Visio.Application")
End Sub

If ye are utilizing VBA from within Visio, ye dinnae need to get a reference to the application object since obviously Visio is already running and you are running your code from within that running instance.

Look at the hierarchy in the Visio Object Model. Note that once you have a reference to the application, you then can get a reference to the Documents Collection. Keep in mind that if you use CreateObject() to create a new instance of Visio, the Documents collection in a new Visio instance will contain zero members. You will then need to use the Add method of the Documents Collection Object to create the Visio drawing. If Visio is running, the Count property of the Documents Collection Object will return the number of open documents in the running instance of Visio. Here is an example of getting a reference to the first (and presuming only) open document in the running instance of Visio:

Public Sub Foo()
Dim appVisio As Object
Dim docsObj As Object
Dim inCount As Integer
Dim docObj As Object
Set appVisio = GetObject("Visio.Application")
Set docsObj = appVisio.Documents
inCount = docsObj.Count
Set docObj = docsObj.Item(inCount)
End Sub

If you are writing your application using Visio’s VBA, you have a much shorter method available to you. Remember that you are by nature currently in a running instance of Visio, and a document is currently open. Therefore the reference is:

Public Sub Foo()
Dim docObj As Visio.Document
Set docObj = Visio.ActiveDocument
End Sub

Now that you have a reference to the currently open document you will need to get a reference to the page of the document that you need to reference. This can be accomplished by either getting a reference to the ActivePage or to an explicit page by Name or Index in the pages collection. The first example below is the "long method" and the second is the VBA "direct" method:

Public Sub Foo()
Dim appVisio As Object
Dim docsObj As Object
Dim inCount1 As Integer
Dim docObj As Object
Dim pagsObj As Object
Dim pagObj As Object
Set appVisio = GetObject("Visio.Application")
Set docsObj = appVisio.Documents
inCount1 = docsObj.Count
Set docObj = docsObj.Item(inCount1)
Set pagsObj = docObj.Pages
PagObj = pagsObj.Item(1)
End Sub

Public Sub Foo()
Dim pagObj As Visio.Page
Set pagObj = Visio.ActivePage
End sub

With a page reference at hand, and let us presume that you have only the shape that you want to reference on the page, you need a reference to that shape. Shapes are part of the Shapes Collection. You can assume that the Shape in question is the first item in the Shapes collection. Again, both the "long" and "short / VBA" methodologies are shown below:

Public Sub Foo()
Dim appVisio As Object
Dim docsObj As Object
Dim inCount1 As Integer
Dim docObj As Object
Dim pagsObj As Object
Dim pagObj As Object
Dim shpsObj As Object
Dim shpObj As Object
Set appVisio = GetObject("Visio.Application")
Set docsObj = appVisio.Documents
inCount1 = docsObj.Count
Set docObj = docsObj.Item(inCount1)
Set pagsObj = docObj.Pages
Set pagObj = pagsObj.Item(1)
Set shpsObj = pagObj.Shapes
Set shpObj = shpsObj.Item(1)
End Sub

Public Sub Foo()
Dim pagObj As Visio.Page
Dim shpObj As Visio.Shape
Set pagObj = Visio.ActivePage
Set shpObj = pagObj.Shapes.Item(1)
End sub

Weel folk, were a’maist thar!!! We have a reference to the shape in question and now wee need to get a reference to both the Height and Width cells in the Shape. Mind, these are exactly the same thing as the cells in the ShapeSheet of the Shape from the User Interface. The Cells Object is a Property of the Shape Object and is referenced by name. At this point the methodology is the same for either VB or VBA so I’ll stick to the short method to illustrate:

Public Sub Foo()
Dim pagObj As Visio.Page
Dim shpObj As Visio.Shape
Dim celObjHeight As Visio.Cell
Dim celObjWidth As Visio.Cell
Set pagObj = Visio.ActivePage
Set shpObj = pagObj.Shapes.Item(1)
Set celObjHeight = shpObj.Cells("Height")
Set celObjWidth = shpObj.Cells("Width")
End sub

Finally, we detearmine whether we simply want to find out what the formula or value is that is in the cell or whether we want to actually change the formula or value in the cell. In a moment I shall discuss the difference between a result and a formula, but I want tae show you the examples first. In the first example, I’ll get both the formulas and values in the Height and Width cells of the shape. In the second, I’ll set a new formula in the Height cell and Width cell:

Public Sub Foo()
Dim pagObj As Visio.Page
Dim shpObj As Visio.Shape
Dim celObjHeight As Visio.Cell
Dim celObjWidth As Visio.Cell
Dim stDisplayString As String
Set pagObj = Visio.ActivePage
Set shpObj = pagObj.Shapes.Item(1)
Set celObjHeight = shpObj.Cells("Height")
Set celObjWidth = shpObj.Cells("Width")
stDisplayString = "The Formula in the Width Cell is: "
stDisplayString = stDisplayString & celObjWidth.Formula & VBCrLf
stDisplayString = stDisplayString & "The Formula in the Height Cell is: "
stDisplayString = stDisplayString & celObjHeight.Formula & VBCrLf
stDisplayString = stDisplayString & "The Value in the Width Cell is: "
stDisplayString = stDisplayString & celObjWidth.Result("In.") & VBCrLf
stDisplayString = stDisplayString & "The Value in the Height Cell is: "
stDisplayString = stDisplayString & celObjHeight.Result("In.")
MsgBox stDisplayString
End sub

Public Sub Foo()
Dim shpObjShape As Visio.Shape
Dim ceObjHeight As Visio.Cell
Dim ceObjWidth As Visio.Cell
Dim stReturnString As String
Set shpObjShape = Visio.ActivePage.Shapes.Item(1)
Set ceObjHeight = shpObjShape.Cells("Height")
Set ceObjWidth = shpObjShape.Cells("Width")
stReturnString = InputBox("New Height Value", "Parametric Shape Example")
ceObjHeight.Formula = stReturnString
stReturnString = InputBox("New Width Value", "Parametric Shape Example")
ceObjWidth.Formula = stReturnString
End Sub

A’richt then… We can see that we can programmatically manipulate any cell in a Shape by settin’ its Formula property. Ye are aware that in a ShapeSheet that you can view either the Formulas or the Values. Values are the evaluation of the Formula. For example, if the Width cell has a Formula [= 2 in.] and the Height cell has a Formula [= Width * 0.5], then the Width cell’s Value is [2 in.] and the Height cell’s Value is [1 in.] since 2 in. times 0.5 equals 1 in. When, under automation, you use the Formula property, you retrieve or set the Formula just as if you were to do it in the ShapeSheet. If you use the Result property, you retrieve the result of the evaluation of the Formula just as if you had viewed the Value in the ShapeSheet.

Weel, I de sae hope that you can use the wee bit o’ a primer on travearsing the Object Hierarchy and that this will assist ye in beginning to develop yer ain solutions for Visio. Until next month…. Enjoy the langer, warmer deys an’ …..

Haste ye bak!!!

Dave "The Auld Scotsman" Edson

Dave's Hot Download(s)
A Visio drawing with the shape defined, programmed and ready to rock and roll... or highland fling... as the case may be...
ObjectBasics.zip
Everything above already keyed in to a Visio drawing - 10Kb

ObjectModel.zip
Diagram of the Visio Object Model- 101Kb

 

 
Rate this article...
Hmmm  OK  Good  Yes! Brilliant
Your a friend about this article.

Copyright © 1998-2007 DBM & others | Disclaimer | Privacy | Re-publication | Trademarks | Webmaster | Home