![]() |
![]() ![]() |
|
Learnin' Tae Automate Greetins 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 its own ShapeSheet. This
Excel-like spreadsheet contains numerous cells. Each of these cells control some aspect of
the shapes 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 shapes 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:
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():
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:
If you are writing your application using Visios 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:
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:
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:
Weel folk, were amaist 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 Ill stick to the short method to illustrate:
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, Ill get both the formulas and values in the Height and Width cells of the shape. In the second, Ill set a new formula in the Height cell and Width cell:
Aricht 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 cells Value is [2 in.] and the Height cells 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
|
|
|
Copyright © 1998-2007 DBM & others | Disclaimer | Privacy | Re-publication | Trademarks | Webmaster | Home |