Partager via


How to: Programmatically Save Visio Documents

There are several ways to save Microsoft Office Visio documents:

  • Save changes in an existing document.

  • Save a new document, or save a document with a new name.

  • Save a document with specified arguments.

For more information, see the VBA reference documentation for the Microsoft.Office.Interop.Visio.Document.Save method, Microsoft.Office.Interop.Visio.Document.SaveAs method, and Microsoft.Office.Interop.Visio.Document.SaveAsEx method.

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

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

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

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

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

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

Compiling 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

Tasks

How to: Programmatically Create New Visio Documents

How to: Programmatically Open Visio Documents

How to: Programmatically Close Visio Documents

How to: Programmatically Print Visio Documents

Concepts

Visio Object Model Overview

Other Resources

Visio Solutions