How to: Programmatically create new Visio documents

Applies to: yesVisual Studio noVisual Studio for Mac

Note

This article applies to Visual Studio 2017. If you're looking for the latest Visual Studio documentation, see Visual Studio documentation. We recommend upgrading to the latest version of Visual Studio. Download it here

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.

Create 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.

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

Create 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.
string docPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments) + @"\test\MyDrawing.vsd";
this.Application.Documents.Add(docPath);
Dim docPath As String = System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments) + "\test\MyDrawing.vsd"
Me.Application.Documents.Add(docPath)

Create 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.

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

Create 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.

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

Compile 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