To create a new drawing, add a Document object to the Documents collection of the Application object.
As you may recall, the Application object has a Documents property that returns its Documents collection. Because this is an object, you need to assign it to an object variable using Set. The first three code statements are equivalent in VBA because the global Visio is predefined and contains a reference to the Application object and the Documents object. If working outside of VBA it will be necessary to get a reference to the active instance of Visio’s Application object in order to access the Documents collection.
If no documents are open in the instance, its Documents collection is empty. This can be important to remember if, for example, you run Visio and attempt to get a document without opening one first. Attempting to get an object from an empty collection causes an error.
To create a new document, use the Add method. For a Documents collection, Add takes the filename of a template as an argument. Visio searches for the template in the template location defined under the menu File > Options > Advanced > File Locations. Alternatively, you may specify the file path explicitly.
If you don’t want to base the new document on a template, use a null string. This creates a new blank document.
The Open and OpenEx methods can be used to open existing document files. OpenEX takes the additional parameter that flags how to open the file. See the Microsoft Visio Developer Reference for more information.
Getting a Reference to the Master
If you base a new drawing on a template, it probably opens one or more stencils for you.
This means the Documents collection now has more than one object in it. It has the new document you created plus each stencil opened by the template.
When you use automation to drop a master in a drawing, you’ll be working with two Document objects—one that represents the new drawing, and the other that represents the stencil containing the master.
Note:
|
Every Visio document has a stencil which holds the master shapes that have been added to the drawing, so every Document object has a Masters collection. But the Masters collection of a new drawing is generally empty. Just make sure that you set your object reference to the Masters collection of the stencil, not the drawing.
|
Dropping the Master on the Page
Having obtained a reference to the master, you now need a page on which to drop it. The next step is to get a Page object.
A new document has at least one page, so its Pages collection will contain at least one Page object.
To drop a master, use the Drop method of the Page. Drop takes three arguments: a Master object and a pair of x,y coordinates that indicate where to place the pin of the new shape (the instance of the master).
The coordinate arguments to Drop must be expressed in inches, which are the units that Visio uses internally. Unfortunately, this means you may have to convert the arguments before dropping the master, if for example, you’re working in a scaled drawing or an unscaled drawing in metric units. The ConvertResult method of the Application is a handy tool for solving these unit conversion problems.
Tip:
|
Because you are going to do more to the new instance of the master, assign the object returned by Drop to an object variable, such as oShape. You’ll usually want to do this, but it’s not required.
|
Adding Text to Shapes
When you are creating a drawing from a program, you’ll usually set the text in shapes at run time. You might get the text from another file, or have the user type it in a form, for example.
You assign text to a shape by setting its Text property to a string. You can, of course, concatenate strings and variables with the Visual Basic concatenation operator, and insert special characters (such as the linefeed shown here) using the VB Chr$() function or vbCrLf. The Text property of a shape can contain up to 64K of character data, which will be ample for most purposes.
Use the Characters object to control field codes within the stream of text, just as you can do through the user interface.
Getting Formulas of Shapes and Pages
Compared to everything you can do to a shape in Visio, Shape objects have relatively few properties and methods (although they have many compared to some other objects in the model). So it might seem that your options are limited.
They’re not, however, because you have full access to the ShapeSheet through Cell objects. A Cell object represents a particular formula, such as a shape’s Width formula. Once you have a Cell object, you can get the formula or the value it represents, just as you can choose Formulas or Values when viewing the ShapeSheet. Change the contents of a cell by setting its Formula property.
Most commonly, you’ll get the values of certain formulas so you can use them to construct your drawing, such as the width and height of the page, or the drawing scale.
To get a Cell object, get the Cells property of a shape object and specify the name of the cell. Use the same names for cells as are used in the ShapeSheet. The ShapeSheet reference under help lists all of the ShapeSheet cells alphabetically by name.
To get page cells, first get the PageSheet property of the page, which returns a Shape object. Then use the Shape object’s Cells property to get the formula of a particular cell.
Tip:
|
When should you use Cell vs CellU and Cells vs CellsU. If you are faced the possibility of having to localize your solution, using CellU instead of Cell and CellsU instead of Cells will save you some work. CellU and CellsU take the universal name (not localized) name of a cell as their argument. The universal names never change in localized versions of Visio.
|
Share with your friends: |