The last post on ‘Modifying the Visio Grid shape’ demonstrates how to change the resizing behaviour of a complex shape from a ShapeSheet perspective. I used the word ‘modifying’ but, in fact, it’s actually a new shape and one built using code and it’s this aspect that I thought I’d focus on in this post.
I’d love to encourage anyone who’s not written any code before to keep reading as I think this makes a good challenge for new coders. If you’re in that boat then you might also want to have a quick read through Just for starters and Looping through to get you going, but the code below has lots of comments and is hopefully self explanatory.
I think a reasonable question before you start reaching for the Alt+F12 shortcut is, why go to the bother of writing code to create a shape when there’s a perfectly good UI sitting there wait for your input?
Although you could tackle this problem manually, a code driven approach is a better option in this case, given the work involved to update each one of the hundred cell child shapes contained by the parent group.
Aside from time, you also benefit from a much greater degree of flexibility from making future changes either due to a previous error (of which there are bound to be some) or a change in the design of the shape itself.
Of course it always depends on how much duplication is required and also what level of variation each instance requires. For example if all of your duplicate shapes were just that, an exact copy, then even with a hundred shapes you could probably tap out Ctrl+D faster than opening the code window. But, if you find yourself having to open the ShapeSheet for each one then the reverse is likely to be the case.
The basic flow of the code is essentially to drop the parent shape and then add the children and respective formulae with the correct Sheet.ID references. A sketched outline looks something like this:
I’ll break up the actual code into sections. I’ve started off with a number of string constants, where they’re are used multiple times, to avoid typo bugs and to make it easier to make changes later on:
The main method just checks the window type before kicking off the real work in BuildGrid:
As you can see BuildGrid calls two other methods. Firstly a little helper method that creates a delimited string for the Shape Data format cells and secondly the CreateGridCell method, which is called for each cell in the group:
Points of interest
Order – Unless it’s not apparent, the order in which you add ShapeSheet cells and formulae is important. If you try and add a formula that references an as yet uncreated shape or cell your code will generate an exception.
Cell properties – To retrieve a reference to a particular cell you can use either the Shape.Cells/U or Shape.CellsSRC properties. The first, which takes a simple cell name string, is arguably easier to both read and write, the second though, which takes section, row and column indices, allows you to reference a cell by index when the name isn’t needed. This is particularly handy in this case where you’re just dropping a row onto the end of the existing rows (using the visRowLast enumeration), but also applies to the building of geometry sections.
Double quotes – If you want to add a formula that includes double quotes within the string, you add a consecutive pair of double quotes that Visio interprets as a single set to be included in the final formula. For example, if in ShapeSheet you wanted to see this:
Prop.MyCell.Format = “One;Two”
…and you want to create this in code based on a constant for the delimiter (;) then you’d type the following:
Private Const Format_Cell_Delimiter = ";"
"""One" & Format_Cell_Delimiter & "Two"""
Selections – To add children to group shapes you have to use the AddToGroup or Group methods on the Selection object. Although you get an initial selection object via Window.Selection you can separate this from the ActiveWindow’s own selection. If you watch the shapes being created, you’ll see the ActiveWindow selection changing as each new cell shape is dropped and this has no affect on the other selection object (‘GridSelection’) being used to create the group.
Although the above code is used to generate a grid shape, it hopefully demonstrates how you can generate any grouped shape based on your own logic.