次の方法で共有


How to: Programmatically open 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

There are two methods for opening existing Microsoft Office Visio documents: Open and OpenEx. The OpenEx method is identical to the Open method, except that it provides arguments in which the caller can specify how the document opens.

For details about the object model, see the VBA reference documentation for the Microsoft.Office.Interop.Visio.Documents.Open method and Microsoft.Office.Interop.Visio.Documents.OpenEx method.

Open a Visio document

To open a Visio document

  • Call the Microsoft.Office.Interop.Visio.Documents.Open method and supply the fully qualified path of the Visio document.

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

Open a Visio document with specified arguments

To open a Visio document as read-only and docked

  • Call the Microsoft.Office.Interop.Visio.Documents.OpenEx method, supply the fully qualified path of the Visio document, and include the arguments you want to use—in this case, Docked and Read-only.

    string docPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments) + @"\test\MyDrawing.vsd";
    this.Application.Documents.OpenEx(docPath,
        ((short)Microsoft.Office.Interop.Visio.VisOpenSaveArgs.visOpenDocked +
         (short)Microsoft.Office.Interop.Visio.VisOpenSaveArgs.visOpenRO));
    
    Dim docPath As String = System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments) + "\test\MyDrawing.vsd"
    Me.Application.Documents.OpenEx(docPath, CShort(Microsoft.Office.Interop.Visio.VisOpenSaveArgs.visOpenDocked) + CShort(Microsoft.Office.Interop.Visio.VisOpenSaveArgs.visOpenRO))
    

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

See also