/// <summary> /// Try to find an unique frame name, which isn't currently used inside /// remote office instance. Because we create top level frames /// only, it's enough to check the names of existing child frames on the /// desktop only. /// </summary> /// <returns> /// should represent an unique frame name, which currently isn't /// used inside the remote office frame tree /// (Couldn't guaranteed for a real multi threaded environment. /// But we try it ...) /// </returns> public static String GetUniqueFrameName() { String sName = null; XComponentContext xCtx = OO.GetContext(); try { var xSupplier = (XFramesSupplier)xCtx.getServiceManager().createInstanceWithContext("com.sun.star.frame.Desktop", xCtx); var xContainer = (XIndexAccess)xSupplier.getFrames(); int nCount = xContainer.getCount(); for (int i = 0; i < nCount; ++i) { XFrame xFrame = (XFrame)xContainer.getByIndex(i).Value; sName = BASEFRAMENAME + _mnViewCount; while (String.Compare(sName, xFrame.getName(), System.StringComparison.Ordinal) == 0) { ++_mnViewCount; sName = BASEFRAMENAME + _mnViewCount; } } } catch (unoidl.com.sun.star.uno.Exception) { sName = BASEFRAMENAME; } if (sName == null) { System.Diagnostics.Debug.WriteLine("invalid name!"); sName = BASEFRAMENAME; } return(sName); }
/// <summary> /// Load document specified by an URL into given frame synchronously. /// The result of this operation will be the loaded document for success /// or null if loading failed. /// </summary> /// <param name="xFrame">frame wich should be the target of this load call</param> /// <param name="sUrl">unparsed URL for loading</param> /// <param name="lProperties">optional arguments</param> /// <returns> /// the loaded document for success or null if it's failed /// </returns> public static XComponent LoadDocument(XFrame xFrame, String sUrl, PropertyValue[] lProperties) { XComponent xDocument; String sOldName = null; try { XComponentContext xCtx = OO.GetContext(); // First prepare frame for loading // We must address it inside the frame tree without any complications. // So we set an unambiguous (we hope it) name and use it later. // Don't forget to reset original name after that. sOldName = xFrame.getName(); String sTarget = "odk_officedev_desk"; xFrame.setName(sTarget); // Get access to the global component loader of the office // for synchronous loading the document. var xLoader = (XComponentLoader)xCtx.getServiceManager().createInstanceWithContext(OO.Services.FRAME_DESKTOP, xCtx); // Load the document into the target frame by using his name and // special search flags. xDocument = xLoader.loadComponentFromURL( sUrl, sTarget, FrameSearchFlag.CHILDREN, lProperties); // don't forget to restore old frame name ... xFrame.setName(sOldName); } catch (unoidl.com.sun.star.io.IOException exIo) { // Can be thrown by "loadComponentFromURL()" call. // The only thing we should do then is to reset changed frame name! System.Diagnostics.Debug.WriteLine(exIo); xDocument = null; if (sOldName != null) { xFrame.setName(sOldName); } } catch (IllegalArgumentException exIllegal) { // Can be thrown by "loadComponentFromURL()" call. // The only thing we should do then is to reset changed frame name! System.Diagnostics.Debug.WriteLine(exIllegal); xDocument = null; if (sOldName != null) { xFrame.setName(sOldName); } } catch (RuntimeException exRuntime) { // Any UNO method of this scope can throw this exception. // The only thing we can try(!) is to reset changed frame name. System.Diagnostics.Debug.WriteLine(exRuntime); xDocument = null; if (sOldName != null) { xFrame.setName(sOldName); } } catch (unoidl.com.sun.star.uno.Exception exUno) { // "createInstance()" method of used service manager can throw it. // The only thing we should do then is to reset changed frame name! System.Diagnostics.Debug.WriteLine(exUno); xDocument = null; if (sOldName != null) { xFrame.setName(sOldName); } } return(xDocument); }
/// <summary> /// Load document specified by an URL into given frame synchronously. /// The result of this operation will be the loaded document for success /// or null if loading failed. /// </summary> /// <param name="xFrame">frame wich should be the target of this load call</param> /// <param name="sUrl">unparsed URL for loading</param> /// <param name="lProperties">optional arguments</param> /// <returns> /// the loaded document for success or null if it's failed /// </returns> public static XComponent LoadDocument(XFrame xFrame, String sUrl, PropertyValue[] lProperties) { XComponent xDocument; String sOldName = null; try { XComponentContext xCtx = OO.GetContext(); // First prepare frame for loading // We must address it inside the frame tree without any complications. // So we set an unambiguous (we hope it) name and use it later. // Don't forget to reset original name after that. sOldName = xFrame.getName(); String sTarget = "odk_officedev_desk"; xFrame.setName(sTarget); // Get access to the global component loader of the office // for synchronous loading the document. var xLoader = (XComponentLoader)xCtx.getServiceManager().createInstanceWithContext(OO.Services.FRAME_DESKTOP, xCtx); // Load the document into the target frame by using his name and // special search flags. xDocument = xLoader.loadComponentFromURL( sUrl, sTarget, FrameSearchFlag.CHILDREN, lProperties); // don't forget to restore old frame name ... xFrame.setName(sOldName); } catch (unoidl.com.sun.star.io.IOException exIo) { // Can be thrown by "loadComponentFromURL()" call. // The only thing we should do then is to reset changed frame name! System.Diagnostics.Debug.WriteLine(exIo); xDocument = null; if (sOldName != null) xFrame.setName(sOldName); } catch (IllegalArgumentException exIllegal) { // Can be thrown by "loadComponentFromURL()" call. // The only thing we should do then is to reset changed frame name! System.Diagnostics.Debug.WriteLine(exIllegal); xDocument = null; if (sOldName != null) xFrame.setName(sOldName); } catch (RuntimeException exRuntime) { // Any UNO method of this scope can throw this exception. // The only thing we can try(!) is to reset changed frame name. System.Diagnostics.Debug.WriteLine(exRuntime); xDocument = null; if (sOldName != null) xFrame.setName(sOldName); } catch (unoidl.com.sun.star.uno.Exception exUno) { // "createInstance()" method of used service manager can throw it. // The only thing we should do then is to reset changed frame name! System.Diagnostics.Debug.WriteLine(exUno); xDocument = null; if (sOldName != null) xFrame.setName(sOldName); } return xDocument; }