Open Office Sucks and So Does Your Face (UnoRuntime + C#)

If you are stubborn, or an idiot, or you just like using junky apps Open Office is for you. This is probably one of the only situations I'd suggest paying for and installing Office 2007 / 2003. It's just worth it.

I've been doing some automation in C# / Open Office [which I would love to rename Open Orface] and have been having issues porting Java code into C# - especially with this god darn UnoRuntime B.S.

So, what the fark is UnoRuntime and how the hell do I use it in Open Office. Well, I'm not going to get into the nitty gritty about it here - rather I'm going to explain how you don't use this somewhat core seeming object when building automation in C#.

As I'm sure you are aware - UnoRuntime is not available from C# - BUT - Most of the Java examples use this object heavily to query the UI layer and for object creation (or brokering I'm no expert under the Open Orface hood).

At first I took a look into Marshal.QueryInterface method just to see if I couldn't find the UnoRuntime somewhere in the COM layer - I turned around and decided to use an elaborate try catch + reflection algorithm to gain some concrete understanding of what was in the uno objects.

As you work more with Open Office you will realize the C# library uno, and unoidl namespaces merely contain gigantic unorganized transparent object proxy's that point into completely nonintuitive open office objects. Moreover, these open office objects are heavily based on the use of Interfaces - well I looked a bit deeper into the types I was calling and low and behold - they are pretty much huge junky objects that expose a countless number of interfaces - so we don't really even need the UnoRuntime (which makes me wonder why Java uses it).

The bottom line in this article is that UnoRuntime doesn't exist in C#. If you are porting Open Orface Java code into C# then forget about UnoRuntime. Do everything through casting and shut the hell up.

Here is an example of the Java to C# difference in creating a text property replacer:

xRepDesc is of interface type: XReplaceDescriptor AND XPropertyReplace so you can just cast the xRepDesc into XPropertyReplace and boom you're running.

XPropertyReplace xPropRepl = (XPropertyReplace) UnoRuntime.queryInterface(XPropertyReplace.class, xRepDesc);

XPropertyReplace xPropRepl = ((XPropertyReplace)xRepDesc);

Just in case you don't know, you create the xRepDesc object by, guess what - casting xComponent (of type - you guessed it XComponet - seriously wtf is an XComponent):

XReplaceDescriptor xRepDesc = ((unoidl.com.sun.star.util.XReplaceable)xComponent).createReplaceDescriptor();

I'm going to go ahead and call this automarshalling and be done with it. It sucks and if I had a server app I'd be tempted to write all my Open Orface automation in Java and write a high level COM abstraction to port a simple oAssWrapper.RunJunk(params) method and not have to deal with the hassle - OR - just go the Open Document route and use MS Word automation and then merely use open office to convert the document.

Over And Out

No comments: