public void CloseDoc(string docUri) { //add code here to close the specific doc/app instance with docUri and notify the group of the closure WordFactory.CloseDocument(docUri); Clients.All.docClosed(); Console.WriteLine($"Doc {docUri} was closed"); }
public static void CloseWord() { string result = "Word already Closed"; if (WordApp != null) { try { foreach (Document doc in WordApp.Documents) { doc.Activate(); WordApp.Activate(); WordFactory.CloseDocument(doc.FullName); } //WordApp.Quit(WdSaveOptions.wdDoNotSaveChanges); result = "Word Closed"; } catch (System.Runtime.InteropServices.COMException ex) { switch (ex.HResult) { case -2147417846: //Dialog box is open and blocking us WordApp.Activate(); context.Clients.All.addMessage("CloseWord", "Can't close Word, please check for open dialog box"); return; case -2147023174: //Word Instance died without us knowing, need to set back to null to recover context.Clients.All.addMessage("CloseWord", "Word Failed, attempting to recover..."); break; default: //this is to catch the unknown and bubble up the details context.Clients.All.addMessage("CloseWord", $"Oops... Something went wrong Code {ex.HResult} Message: {ex.Message}"); return; } } } WordApp = null; context.Clients.All.addMessage("CloseWord", result); Console.WriteLine($"CloseWord: {result}"); }