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.

Leave a Reply