Close the active document via API

By Joe Ye

Document.Close() method cannot close active document. This is documented in RevitAPI.chm file. Is there any workaround to close the active document alternatively?

Yes, you can use the way of send

Closing active document via Close() method is not possible. API wish was logged  for this request.

Alternatively we can send message to mimic the user operation in third-party plug-in. Close active document’s shortcut is Ctrl+F4. We can use SendKey.SendWait() method to send this shortcut string to Revit to close active document. SendWait method needs to be called in another thread.

The simplest code below shows the solution.

public Result Execute(          ExternalCommandData commandData,          ref string message,          ElementSet elements)        {          Document pDoc = commandData.Application            .ActiveUIDocument.Document;          ThreadPool.QueueUserWorkItem(            new WaitCallback(CloseDocProc));          return Result.Succeeded;        }        static void CloseDocProc(object stateInfo)        {          try          {             //Ctrl+F4 is the shortcut to close activate document.            SendKeys.SendWait("^{F4}");          }          catch (Exception ex)          {            System.Windows.Forms.MessageBox.Show(ex.Message);          }        }

Launching this command closes the current document. Though this works well to close active document by sending Ctrl+F4, it doesn’t mean this idea can work for all commands’ shortcuts.  Note this workaround is not officially supported.


Comments

2 responses to “Close the active document via API”

  1. Hi This workaround does not work in Revit2014.
    Calling SendKeys.SendWait(“^{F4}”); does not close down the active document. Has anyone tested this or know why it does not work in 2014?

  2. i found it works in 2014 and not in 2015.

Leave a Reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading