DocumentOpened Event


Subscribe to the DocumentOpened event to be notified immediately after Revit has finished opening a document.

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<DocumentOpenedEventArgs> DocumentOpened
Visual Basic
Public Event DocumentOpened As EventHandler(Of DocumentOpenedEventArgs)
Visual C++
public:
 event EventHandler<DocumentOpenedEventArgs^>^ DocumentOpened {
	void add (EventHandler<DocumentOpenedEventArgs^>^ value);
	void remove (EventHandler<DocumentOpenedEventArgs^>^ value);
}

Remarks

This event is raised immediately after Revit has finished opening a document. It is raised even when document opening failed or was cancelled (during DocumentOpening 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.

Check the 'Status' field in event's argument to see whether the action itself was successful or not.

This event is not cancellable, for the process of opening document has already been finished.

If the action was not successful, the document may not be modified and new transactions may not be started.

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.

Examples

Copy C#
public class Application_DocumentOpened : IExternalApplication
{
    /// <ExampleMethod>
    /// <summary>
    /// Implement this method to subscribe to event.
    /// </summary>
    public Result OnStartup(UIControlledApplication application)
    {
        try
        {
            // Register event. 
           application.ControlledApplication.DocumentOpened += new EventHandler
                <Autodesk.Revit.DB.Events.DocumentOpenedEventArgs>(application_DocumentOpened);
        }
        catch (Exception)
        {
            return Result.Failed;
        }

        return Result.Succeeded;
    }

    /// <summary>
    /// Implement OnShutdown method of IExternalApplication interface to unregister subscribed event
    /// </summary>
    public Result OnShutdown(UIControlledApplication application)
    {
        // remove the event.
       application.ControlledApplication.DocumentOpened -= application_DocumentOpened;
        return Result.Succeeded;
    }

    /// <TrivialCode>
    /// Code ID: 501
    /// For DocumentOpened class description
    /// </TrivialCode>

    /// <description>
    /// This sample demonstrates how to subscribe to the DocumentOpened event and modify the model in the event handler method. 
    /// </description>
    /// <summary>
    /// This is the event handler. We modify the model.
    /// </summary>
    /// <param name="sender">Event sender.</param>
    /// <param name="args"></param>
    public void application_DocumentOpened(object sender, DocumentOpenedEventArgs args)
    {
        // get document from event args.
        Document doc = args.Document;

        // Following code snippet demonstrates support of DocumentOpened event to modify the model.
        // Because DocumentOpened supports model changes, it allows user to update document data.
        // Here, this sample assigns a specified value to ProjectInformation.Address property. 
        // User can change other properties of document or create(delete) something as he likes.
        // 
        // Please note that ProjectInformation property is empty for family document.
        // So please don't run this sample on family document.
        using (Transaction transaction = new Transaction(doc, "Edit Address"))
        {
           if (transaction.Start() == TransactionStatus.Started)
           {
              doc.ProjectInformation.Address =
                  "United States - Massachusetts - Waltham - 1560 Trapelo Road";
              transaction.Commit();
           }
        }
    }

    /// </ExampleMethod>
}
Copy VB.NET
Public Class Application_DocumentOpened
    Implements IExternalApplication
    ' <ExampleMethod>
    ' <summary>
    ' Implement this method to subscribe to event.
    ' </summary>
    Public Function OnStartup(application As UIControlledApplication) As Autodesk.Revit.UI.Result Implements IExternalApplication.OnStartup
        Try
            ' Register event. 
            AddHandler application.ControlledApplication.DocumentOpened, AddressOf application_DocumentOpened
        Catch generatedExceptionName As Exception
            Return Result.Failed
        End Try

        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
        ' remove the event.
        RemoveHandler application.ControlledApplication.DocumentOpened, AddressOf application_DocumentOpened
        Return Result.Succeeded
    End Function

    ' <TrivialCode>
    ' Code ID: 501
    ' For DocumentOpened class description
    ' </TrivialCode>


    ' <description>
    ' This sample demonstrates how to subscribe to the DocumentOpened event and modify the model in the event handler method. 
    ' </description>
    ' <summary>
    ' This is the event handler. We modify the model.
    ' </summary>
    ' <param name="sender">Event sender.</param>
    ' <param name="args"></param>
    Public Sub application_DocumentOpened(sender As Object, args As DocumentOpenedEventArgs)
        ' get document from event args.
        Dim doc As Document = args.Document

        ' Following code snippet demonstrates support of DocumentOpened event to modify the model.
        ' Because DocumentOpened supports model changes, it allows user to update document data.
        ' Here, this sample assigns a specified value to ProjectInformation.Address property. 
        ' User can change other properties of document or create(delete) something as he likes.
        '
        ' Please note that ProjectInformation property is empty for family document.
        ' So please don't run this sample on family document.
        Using transaction As New Transaction(doc, "Edit Address")
            If transaction.Start() = TransactionStatus.Started Then
                doc.ProjectInformation.Address = "United States - Massachusetts - Waltham - 1560 Trapelo Road"
                transaction.Commit()
            End If
        End Using
    End Sub

    ' </ExampleMethod>
End Class

See Also