次の方法で共有


How to: Programmatically save 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 several ways to save Microsoft Office Visio documents:

Save an existing document

To save a document

  • Call the Microsoft.Office.Interop.Visio.Document.Save method of the Microsoft.Office.Tools.Visio.Document class of a document that has been previously saved.

    To use this code example, run it from the ThisAddIn class in your project.

    Note

    The Microsoft.Office.Interop.Visio.Document.Save method throws an exception if a new Visio document has not yet been saved.

    this.Application.ActiveDocument.Save();
    
    Me.Application.ActiveDocument.Save()
    

Save a document with a new name

Use the Microsoft.Office.Interop.Visio.Document.SaveAs method to save a new document, or a document that has a new name. This method requires that you specify the new file name.

To save the active Visio document with a new name

  • Call the Microsoft.Office.Interop.Visio.Document.SaveAs method of the Microsoft.Office.Tools.Visio.Document that you want to save, by using a fully qualified path including a file name. If a file by that name already exists in that folder, it is silently overwritten.

    To use this code example, run it from the ThisAddIn class in your project.

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

Save a document with a new name and specified arguments

Use the Microsoft.Office.Interop.Visio.Document.SaveAsEx method to save a document with a new name, and specify any applicable arguments to apply to the document.

To save document with a new name and specified arguments

  • Call the Microsoft.Office.Interop.Visio.Document.SaveAsEx method of the Microsoft.Office.Tools.Visio.Document that you want to save, by using a fully qualified path including a file name. If a file by that name already exists in that folder, an exception is thrown.

    The following code example saves the active document with a new name, marks the document as read-only, and shows the document in the Most Recently Used list of documents. To use this code example, run it from the ThisAddIn class in your project.

    string newDocPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments) + @"\test\MyOtherDrawing.vsd";
    this.Application.ActiveDocument.SaveAsEx(newDocPath,
        ((short)Microsoft.Office.Interop.Visio.VisOpenSaveArgs.visSaveAsRO +
         (short)Microsoft.Office.Interop.Visio.VisOpenSaveArgs.visSaveAsListInMRU));
    
    Dim newDocPath As String = System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments) + "\test\MyOtherDrawing.vsd"
    Me.Application.ActiveDocument.SaveAsEx(newDocPath, CShort(Microsoft.Office.Interop.Visio.VisOpenSaveArgs.visSaveAsRO) + CShort(Microsoft.Office.Interop.Visio.VisOpenSaveArgs.visSaveAsListInMRU))
    

Compile the code

This code example requires the following:

  • To save a document that has a new name, a directory named Test must be located in the My Documents folder (for Windows XP and earlier) or the Documents folder (for Windows Vista).

See also