DocumentSavingAs Event


Subscribe to the DocumentSavingAs event to be notified when Revit is just about to save the document with a new file name.

Namespace: Autodesk.Revit.ApplicationServices
Assembly: RevitAPI (in RevitAPI.dll) Version: 17.0.0.0 (17.0.1090.0)
Since: 2010

Syntax

C#
public event EventHandler<DocumentSavingAsEventArgs> DocumentSavingAs
Visual Basic
Public Event DocumentSavingAs As EventHandler(Of DocumentSavingAsEventArgs)
Visual C++
public:
 event EventHandler<DocumentSavingAsEventArgs^>^ DocumentSavingAs {
	void add (EventHandler<DocumentSavingAsEventArgs^>^ value);
	void remove (EventHandler<DocumentSavingAsEventArgs^>^ value);
}

Remarks

This event is raised when Revit is just about to save the document with a new file name. Note that the first save of a newly created document will raise DocumentSavingAs rather than DocumentSaving event.

Handlers of this event are permitted to make modifications to any document (including the active document), except for documents that are currently in read-only mode.

This event is cancellable, except when it is raised during close of the application. Check the 'Cancellable' property of event's argument to see whether it is cancellable or not. When it is cancellable, call the 'Cancel()' method of event's argument to cancel it. Your application is responsible for providing feedback to the user about the reason for the cancellation.

The following API functions are not available for the current document during this event:

Exception InvalidOperationException will be thrown if any of the above methods is called during this event.

Another event DocumentSavedAs will be raised immediately after the document has been saved with a new file name.

Examples

Copy C#
public class DocumentSavingAs : IExternalApplication
{
    /// <ExampleMethod>
    /// <summary>
    /// This method subscribes to DocumentSavingAs event.
    /// </summary>
    public Result OnStartup(UIControlledApplication application)
    {
       application.ControlledApplication.DocumentSavingAs += new EventHandler<DocumentSavingAsEventArgs>(CheckProjectStatusInitial);
        return Result.Succeeded;
    }

    /// <summary>
    /// Implement OnShutdown method of IExternalApplication interface to unregister subscribed event
    /// </summary>
    public Result OnShutdown(UIControlledApplication application)
    {
        // unregister subscribed event
       application.ControlledApplication.DocumentSavingAs -= new EventHandler<DocumentSavingAsEventArgs>(CheckProjectStatusInitial);
        return Result.Succeeded;
    }

    /// <summary>
    /// Event handler method for DocumentSavingAs event. This method will check the "Project Status" value, 
    /// if it is null or empty string cancel the save as process.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="args">Event arguments that contains the event data.</param>
    private void CheckProjectStatusInitial(Object sender, DocumentSavingAsEventArgs args)
    {
        Document doc = args.Document;
        ProjectInfo proInfo = doc.ProjectInformation;

        // Project information is only available for project document.
        if (null != proInfo)
        {
            if (string.IsNullOrEmpty(proInfo.Status))
            {
                // cancel the save as process.
                args.Cancel();
                TaskDialog.Show("Revit","Status project parameter is not set.  Save is aborted.");
            }
        }
    }
    /// </ExampleMethod>
}
Copy VB.NET
Public Class DocumentSavingAs
    Implements IExternalApplication
    ' <ExampleMethod>
    ' <summary>
    ' This method subscribes to DocumentSavingAs event.
    ' </summary>
    Public Function OnStartup(application As UIControlledApplication) As Autodesk.Revit.UI.Result Implements IExternalApplication.OnStartup
        AddHandler application.ControlledApplication.DocumentSavingAs, AddressOf CheckProjectStatusInitial
        Return Result.Succeeded
    End Function

    ' <summary>
    ' Implement OnShutdown method of IExternalApplication interface to unregister subscribed event
    ' </summary>
    Public Function OnShutdown(application As UIControlledApplication) As Autodesk.Revit.UI.Result Implements IExternalApplication.OnShutdown
        ' unregister subscribed event
        RemoveHandler application.ControlledApplication.DocumentSavingAs, AddressOf CheckProjectStatusInitial
        Return Result.Succeeded
    End Function

    ' <summary>
    ' Event handler method for DocumentSavingAs event. This method will check the "Project Status" value, 
    ' if it is null or empty string cancel the save as process.
    ' </summary>
    ' <param name="sender">The source of the event.</param>
    ' <param name="args">Event arguments that contains the event data.</param>
    Private Sub CheckProjectStatusInitial(sender As [Object], args As DocumentSavingAsEventArgs)
        Dim doc As Document = args.Document
        Dim proInfo As ProjectInfo = doc.ProjectInformation

        ' Project information is only available for project document.
        If proInfo IsNot Nothing Then
            If String.IsNullOrEmpty(proInfo.Status) Then
                ' cancel the save as process.
                args.Cancel()
                TaskDialog.Show("Revit", "Status project parameter is not set.  Save is aborted.")
            End If
        End If
    End Sub
    ' </ExampleMethod>
End Class

See Also