So, this being the first proper post, I'm going to kick things off with a look at some of the basics of how to get at both the ShapeSheet and the VBA programming environment so that I can refer people back to this in future posts if required.
This is by no means a full course; it's intended to be a quick run through of the key elements for anyone who's not tried any ShapeSheet or VBA development before.
The ShapeSheet
All shapes in Visio have an accompanying set of properties that describe things like page position, geometry and fill, line and text formatting. By default, access to these 'ShapeSheets' is only available via the Window menu. However, by checking the "Run in developer mode" under Tools / Options / Advanced, you can right-click on any shape to reveal its ShapeSheet.
The ShapeSheet is structured like a spreadsheet that is broken down into a number of Sections. A Section can be further broken down into Cells that are contained in rows and columns.
Each Cell can contain formulae (somewhat akin to Excel) or straight values. So, for example, the height cell of a shape could be set to a value, such as 90mm, (as above) or to a formula, which sets the shape's height to be relative to its width as follows:
Height =Width*3
Within formulae you can add a range of ShapeSheet functions that enable you to calculate a particular result based on some input variables. These can range from logic functions such as the IF function, (which gives a true or false value dependent on an initial expression) to information-based functions such as PAGENAME, (which returns the name of the page on which the shape is sitting). An example for each of these would be:
=IF(Width>3mm,Width*2,Width*3)
=PAGENAME()
Translated into English, the IF function above would read as follows:
"If the result of the Width cell value is more than 3mm then make this formula Width*2, otherwise make it Width*3"
There's really no end of combinations that you can put together to build 'smart shapes' and if you've not done so already it's really worth while taking a look at how existing shapes are constructed. If this is the first time you've seen the ShapeSheet then don't worry about understanding each and every formula; just get a feel for the structure and have a go at changing a few values and see the effect this has on the shape.
An extra point that may not be immediately apparent is that Visio places shape-like properties against pages and documents and they all follow the same format. So to view a page's ShapeSheet right-click on the page itself and for a document, select Window / Show ShapeSheet with the Shift key held down.
Further details on the ShapeSheet can be found here under the Developing Microsoft Visio Solutions and a very good introduction to the ShapeSheet can also been seen here from the Visio Insights blog. Additionally there's some good tips at Visio.MVPS.org and Chris Roth's 'Visio Guy' blog.
Macros and VBA (Visual Basic for Applications)
While the ShapeSheet is extremely powerful in its own right, as can be seen in Chris's blog above, with a little bit of code you can achieve things that are either impossible or impractical through ShapeSheet work alone.
The first place to start is the Visual Basic Editor (VBE) from where you can create and run your code. To access this click Tools / Macro / Visual Basic Editor (or alt + F11) and the VBE window should be revealed.
The basic setup here is a list of open documents on the left in the Project Explorer, a Properties window below that and the code pane on the right. Code is usually broken up into procedures and these are stored in Modules (and Classes, although you won't need to worry about these just yet), which in turn are stored in documents.
To start adding code, whether you're pasting it in or writing from scratch, select the Module you want to add to and start typing. (If you can't see a Module under your chosen document then you can add one by clicking Insert / Module.)
Before you get going with any code however, you may have to alter the security settings in order for your code to run. For Visio 2007 you can check the 'Disable all macros with notification' option under Tools / Trust Center / Macro Settings, which will give you the option of allowing code to be enabled each time a document is opened. (A similar option for Visio 2003 can be found under Tools / Macros / Security... / Security Level / Medium.)
So, now you know how things are basically structured but how do you produce something useful? Well there's several things that can help here. First there is the Macro Recorder (introduced in 2003) that creates code for you based on actions that you take within the normal Visio user interface. I'm not going to go into detail here because there's already a great post by John Marshall on the subject here. Enough to say that this is an extremely useful tool whenever you get stuck and you should definitely play around with it and see what it produces.
Secondly, there's IntelliSense. This is a feature within the VBE that offers you a range of relevant options as you type. For example, if you type in 'Application' and then a full stop you'll be offered all of the objects that relate to Application:
Thirdly, there's the F8 key. To run a procedure under normal conditions you click on Run in the menu bar (or F5 or the green triangle on the toolbar). However, pressing F8 (or Debug / Step Into) enables you to execute one line at a time. This is especially useful for understanding what your code is up to as, at any point, you can hover your mouse over a variable and see its current value in a pop-up tool tip.
Objects
Before I finish my whirlwind introduction to the Visio programming environment I'd like to add a few words about Objects. Although VBA is not a fully "object-orientated" language, a little object thinking will help in trying to understand what's going on.
Objects are essentially 'things' that can represent tangible objects in the real world through to quite abstract concepts. For example, you might think about a house as being an object that contains other objects such and walls, windows and doors. Similarly, in programming developers create a structure of objects to mirror the system they are trying to model. These are structured together, often in a hierarchical manner, to create an 'object model'. In Visio's case a basic top level view would be as follows:
An Application contains a collection of Document objects. Each Document contains a collection of Page objects and each Page contains a collection of Shape objects. The full Visio object-model is of course a good deal more extensive but a clear understanding of the above four objects will make life a lot easier when it comes to writing and understanding code.
Finally, objects contain sets of Properties and Methods. Properties are bits of information about the respective object. So, for example, the Page object has a 'Name' property that, unsurprisingly, returns its assigned name, while its 'Shapes' property returns a reference to all of the shapes contained in that page. Methods, on the other hand 'do' things. Looking again at the Page object, this has a method called 'Drop', which is used to add a new shape to the respective page. Other page methods include actions such as Delete, DrawRectangle, Export and Paste.
OK, so that's enough theory for now. As I said, this isn't meant to be a full course but an easier introduction. There are many useful online resources for using VBA within Visio. Here are a few of the best ones:
- Developing Microsoft Visio Solutions (Chapter 15 and onwards)
- Visio Developer (VBA) Newsgroup
- Visio.MVPS.org (VBA Section)
- Chris Roth's VisGuy.com (Code category)
In terms of books, the Developing Microsoft Visio Solutions is also available in hardcopy. Graham Wideman's Visio 2003 Developers Survival Pack is still considered the serious developer's key resource and to bring you right up to date David Parker's Visualizing Information with Microsoft Visio 2007 is a great reference, in particular for all of the latest Data Graphics, Pivot Diagrams and External Data functionality that's part of Visio 2007 Professional.
What's Next?
So, hopefully this post provides a basic entry point into the Visio development environment. In future posts I'll be taking a more practical approach and try to solve some real problems....(check out the 'Code' category in the sidebar above).