Compartilhar via


How to: Programmatically Create New Visio Documents

When you create a new Microsoft Office Visio drawing document, you add it to the Microsoft.Office.Interop.Visio.Documents collection of open Visio documents. Consequently, the Microsoft.Office.Interop.Visio.Documents.Add method creates a new Visio drawing document. For more information, see the VBA reference documentation for the Microsoft.Office.Interop.Visio.Documents.Add method.

Creating New Blank Documents

To create a new document

  • Use the Microsoft.Office.Interop.Visio.Documents.Add method to create a new blank document that is not based on a template.

    Me.Application.Documents.Add("")
    
    this.Application.Documents.Add("");
    

Creating Documents Copied From Existing Documents

The Microsoft.Office.Interop.Visio.Documents.Add method can create a new document that is a copy of an existing Visio document. You must supply the file name and fully qualified path of the diagram.

To create a new document that is copied from an existing document

  • Call the Microsoft.Office.Interop.Visio.Documents.Add method and specify the path of the Visio diagram.

    Dim docPath As String = System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments) + "\test\MyDrawing.vsd" 
    Me.Application.Documents.Add(docPath)
    
    string docPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments) + @"\test\MyDrawing.vsd";
    this.Application.Documents.Add(docPath);
    

Creating Stencils Copied From Existing Stencils

The Microsoft.Office.Interop.Visio.Documents.Add method can create a new stencil that is a copy of an existing Visio stencil. You must supply the file name and fully qualified path of the stencil.

To create a new stencil that is copied from an existing stencil

  • Call the Microsoft.Office.Interop.Visio.Documents.Add method and specify the path of the stencil.

    Dim docPath As String = System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments) + "\test\MyStencil.vss" 
    Me.Application.Documents.Add(docPath)
    
    string docPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments) + @"\test\MyStencil.vss";
    this.Application.Documents.Add(docPath);
    

Creating Documents Based on Existing Templates

The Microsoft.Office.Interop.Visio.Documents.Add method can create a new document (a .vsd file) that is based on an existing Visio template (a .vst file). This method copies the stencils, styles, and settings that are part of the template workspace. You must supply the file name and fully qualified path of the template.

To create a new document that is based on an existing template

  • Call the Microsoft.Office.Interop.Visio.Documents.Add method and specify the path of the template.

    Dim docPath As String = System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments) + "\test\MyTemplate.vst" 
    Me.Application.Documents.Add(docPath)
    
    string docPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments) + @"\test\MyTemplate.vst";
    this.Application.Documents.Add(docPath);
    

Compiling the Code

This code example requires the following:

  • A Visio document named myDrawing.vsd must be located in a directory named Test in the My Documents folder (for Windows XP and earlier) or the Documents folder (for Windows Vista).

  • A Visio document named myStencil.vss must be located in a directory named Test in the My Documents folder (for Windows XP and earlier) or the Documents folder (for Windows Vista).

  • A Visio document named myTemplate.vst must be located in a directory named Test in the My Documents folder (for Windows XP and earlier) or the Documents folder (for Windows Vista).

See Also

Tasks

How to: Programmatically Open Visio Documents

How to: Programmatically Close Visio Documents

How to: Programmatically Save Visio Documents

How to: Programmatically Print Visio Documents

Concepts

Visio Object Model Overview

Other Resources

Visio Solutions