/// <summary> /// Save currently loaded document of given frame. /// </summary> /// <param name="xDocument">document for saving changes</param> public static void SaveDocument(XComponent xDocument) { try { // Check for supported model functionality. // Normally the application documents (text, spreadsheet ...) do so // but some other ones (e.g. db components) doesn't do that. // They can't be save then. var xModel = (XModel)xDocument; if (xModel != null) { // Check for modifications => break save process if there is nothing to do. XModifiable xModified = (XModifiable)xModel; if (xModified.isModified() == true) { XStorable xStore = (XStorable)xModel; xStore.store(); } } } catch (unoidl.com.sun.star.io.IOException exIo) { // Can be thrown by "store()" call. // But there is nothing we can do then. System.Diagnostics.Debug.WriteLine(exIo); } catch (RuntimeException exUno) { // Any UNO method of this scope can throw this exception. // But there is nothing we can do then. System.Diagnostics.Debug.WriteLine(exUno); } }
public void terminate() { XModifiable xMod = (XModifiable)mxDocument; if (xMod != null) { xMod.setModified(false); } XDesktop aDesktop = (XDesktop) mxMSFactory.createInstance("com.sun.star.frame.Desktop"); if (aDesktop != null) { try { aDesktop.terminate(); } catch (DisposedException d) { //This exception may be thrown because shutting down OOo using //XDesktop terminate does not really work. In the case of the //Exception OOo will still terminate. } } }
/// <summary> /// Try to close the document without any saving of modifications. /// We can try it only! Controller and/or model of this document /// can disagree with that. But mostly they doesn't do so. /// </summary> /// <param name="xDocument">document which should be clcosed</param> public static void CloseDocument(XComponent xDocument) { try { // Check supported functionality of the document (model or controller). XModel xModel = (XModel)xDocument; if (xModel != null) { // It's a full featured office document. // Reset the modify state of it and close it. // Note: Model can disagree by throwing a veto exception. XModifiable xModify = (XModifiable)xModel; xModify.setModified(false); xDocument.dispose(); } else { // It's a document which supports a controller .. or may by a pure // window only. If it's at least a controller - we can try to // suspend him. But - he can disagree with that! XController xController = (XController)xDocument; if (xController != null) { if (xController.suspend(true)) { // Note: Don't dispose the controller - destroy the frame // to make it right! XFrame xFrame = xController.getFrame(); xFrame.dispose(); } } } } catch (PropertyVetoException exVeto) { // Can be thrown by "setModified()" call on model. // He disagree with our request. // But there is nothing to do then. Following "dispose()" call wasn't // never called (because we catch it before). Closing failed -that's it. System.Diagnostics.Debug.WriteLine(exVeto); } catch (DisposedException exDisposed) { // If an UNO object was already disposed before - he throw this special // runtime exception. Of course every UNO call must be look for that - // but it's a question of error handling. // For demonstration this exception is handled here. System.Diagnostics.Debug.WriteLine(exDisposed); } catch (RuntimeException exRuntime) { // Every uno call can throw that. // Do nothing - closing failed - that's it. System.Diagnostics.Debug.WriteLine(exRuntime); } }