3 4
A Document object represents a drawing file (.vsd or .vdx), stencil file (.vss or .vsx), or template file (.vst or .vtx) that is open in a Microsoft Visio instance. When working with an existing drawing from a program, you'll often simply work with the active page of the active document—that is, the drawing displayed in the active drawing window. However, in some circumstances, your program might open a document for the user or retrieve a document that is open but not active.
To get information about a document, you need to get a reference to a Document object.
Figure 16-1 Document object and related objects higher in the Visio object model.
Depending on the design of your solution, you can get a reference to the Document object in several ways. The following examples all assume that you have defined an object variable docObj (Dim docObj As Visio.Document).
Set docObj = ActiveDocument
As an alternative, if you've retrieved the active window, you can get the Document property of that Window object; it refers to the same Document object as does ActiveDocument.
For details about the global object, see Using the Visio Global Object in Chapter 15, Programming Visio with VBA.
Set docObj = Documents.Item("Hello.vsd")
The previous statement retrieves the document Hello.vsd from the Documents collection. If Hello.vsd is not open, attempting to retrieve it causes an error.
Set docObj = Documents.Open("c:\Visio\Drawings\Hello.vsd")
This statement opens the document Hello.vsd as an original and adds it to the Documents collection.
You can open any Visio document—stencil, template, or drawing file—with the Open method, but this is not recommended for stencils and templates. The Open method opens the document as an original, rather than as a copy or read-only. An original document can be changed, which is undesirable for stencils and templates because nothing prevents the user from editing masters, altering the template's workspace, or making other potentially unwelcome changes.
To open a Visio document as read-only, use the OpenEx method. You can also use OpenEx to open:
For details, see OpenEx in the Microsoft Visio Developer Reference (on the Help menu, click Developer Reference).
You can get information about a document by retrieving properties, such as Description, Keywords, Subject, and Title. These properties correspond to text boxes in the Properties dialog box for the document (on the File menu, click Properties).
A Document object has three properties you can use to get a document's file name:
These properties are read-only. To change the name, drive, or path of a document, use the SaveAs method to save the document under a different name or to a different drive or path.
You can get the status of a document by getting its ReadOnly or Saved property:
You can also get information about a Document object by getting the DocumentSheet property of the Document object. This property returns a Shape object whose Cells property returns Cell objects that contain the document's formulas. These Cell objects correspond to cells shown in a document's ShapeSheet window. For details about working with formulas, see Chapter 17, Automating Formulas.
To determine what styles are available in a document, get the Styles property of a Document object. The Styles property returns a Styles collection, which represents the set of styles defined for a document. The Name property of a Style object returns the style name that appears in style lists and in the Define Styles dialog box (on the Format menu, click Define Styles).
The following example iterates through the document's Styles collection and lists the style names in a listbox on a user form:
Sub ListStyles () Dim stylsObj As Visio.Styles Dim stylObj As Visio.Style Dim styleName As String Set stylsObj = ThisDocument.Styles UserForm1.ListBox1.Clear For Each stylObj In stylsObj styleName = stylObj.Name UserForm1.ListBox1.AddItem styleName Next UserForm1.Show End Sub
Tip
To create a style from a program, use the Add method of a Styles collection and specify the name of the new style. You can optionally specify the name of a style on which to base the new style and whether the style includes text, line, and fill attributes.
For example, to create a new style named Caption based on the Normal style that includes only text attributes:
Set stylObj = stylsObj.Add("Caption", "Normal", 1, 0, 0)
To create a new style that is not based on another style, with text, line, and fill attributes:
Set stylObj = stylsObj.Add("Street Sign","", 1, 1, 1)
You can change the style's name by setting its Name property, or change whether it includes text, line, or fill attributes by setting its IncludesFill, IncludesLine, or IncludesText property. For details about creating styles, see the Microsoft Visio Help (on the Help menu, click Microsoft Visio Help). A Style object has a Cells property you can use to set formulas for ShapeSheet cells that define the style. This property returns a Cell object that corresponds to a cell in a style.
For example, to change the font size of a style:
Set fontsizeCellObj = stylObj.Cells("Char.Size") fontsizeCellObj.Formula = "18 pt"
For details about working with formulas, see Chapter 17, Automating Formulas.
Your program can print or save the drawing it creates. For users who are comfortable with the Visio menu commands, you'll probably create the drawing from your program and leave printing and saving to the user. If not, you can handle these steps from your program.
You can print a document or a page in a document by using the Print method.
To print all of the pages of a document, use Print with a Document object. This is equivalent to clicking All in the Print dialog box (on the File menu, click Print). To print just one page, use the Print method with a Page object. This is similar to displaying that page and clicking Current page in the Print dialog box.
When printing from Microsoft Visual Basic for Applications (VBA) or Visual Basic, you must:
For example, to print a document:
Dim docObj As Visio.Document Dim docObjTemp As Object Dim dummy As String Set docObj = ThisDocument Set docObjTemp = docObj dummy = docObjTemp.Print
To save a document from a program, use the Save or SaveAs method of a Document object.
Use the SaveAs method and supply a file name and path to save and name a new document, to save a copy of an existing document under a different name, or to save an existing document to a different drive or path. For example:
ThisDocument.SaveAs "c:\visio\drawings\myfile.vsd"
Use the Save method only if the document has already been saved and named. For example:
ThisDocument.Save
Unlike the Save menu command in the Visio user interface, which displays the Save As dialog box if a document is unnamed, using the Save method on an unnamed document won't invoke the SaveAs method—it will cause an error.
To find out whether a document has ever been saved, check its Path property, which returns the drive and path of the document's full name or a null string ( "" ) if the document hasn't been saved. To find out whether a document has been saved since changes were made to it, check its Saved property.
In Visio, the pages of a document are displayed in a window. To display a particular page using Automation, you might expect to use a method or property of a Document object. However, to change the page that is being displayed by Visio, you actually use the Page property of the Window object. For example:
ActiveWindow.Page = NameOfPageToDisplay
where NameOfPageToDisplay is the valid name of a page in the document.
Note
The following example displays the last page of the active document in the active window:
Sub TurnToLastPage() Dim szPageName As String Dim iPageCount As Integer 'Get the page count iPageCount = ActiveDocument.Pages.Count 'Get the name of the last page szPageName = ActiveDocument.Pages(iPageCount).Name 'Display the last page in the ActiveWindow 'i.e. "Turn" the page ActiveWindow.Page = szPageName End Sub