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.

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.

    System_CAPS_noteNote

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

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

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

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

Show: