I'm trying to automate openoffice calc through ole automation from a c# application. I'm opening a document and saving it again. Code is mostly downloaded from Apache web site with few modifications.
private void OpenSave(string FileAddress)//format: "file:///C:/Untitled1.ods"
{
Type t_OOo= Type.GetTypeFromProgID("com.sun.star.ServiceManager");
Object objServiceManager= System.Activator.CreateInstance(t_OOo);
// arguments for IDispatch-call
Object[] parameters = new Object[1];
parameters[0] = "com.sun.star.frame.Desktop";
// arguments for document
Object[] args = new Object[4];
//args[0] = "private:factory/scalc";
args[0] = FileAddress;
args[1] = "_blank";
args[2] = 0;
args[3] = new Object[] { };
Object desktop;
Object doc;
try
{
desktop = (Object)t_OOo.InvokeMember("createInstance",BindingFlags.InvokeMethod, null,objServiceManager, parameters);
doc = desktop.GetType().InvokeMember("loadComponentFromUrl",BindingFlags.InvokeMethod, null, desktop, args);
if (doc == null)
{/*Error*/ }
object[] O = new object[3];
O[0] = FileAddress;
O[1] = new PropertyValue();
((PropertyValue)O[1]).Name = "";
((PropertyValue)O[1]).Value = true;
O[2] = new Object[] { };
desktop.GetType().InvokeMember("storeAsURL", BindingFlags.InvokeMethod, null, desktop,O);
}
catch (Exception e1)
{
Console.WriteLine(e1);
}
Last line of code (saveTOURL) always throws exception of "Unknown name"(exact exception: [System.Runtime.InteropServices.COMException] = {"Unknown name. (Exception from HRESULT: 0x80020006 (DISP_E_UNKNOWNNAME))"}). May Anybody help? Please consider that I'm mostly opening xls files with this program.
In fact documentation about c# & OLE is not rich enough. All documentation is in Java and I had to compare Java to few ole samples available to find a weak clue about object model.
I want to be able to modify a cell's value before saving, I would be grateful if you could guide me about this one also.
I solved my problem using c# SDK instead of OLE, so this is not exactly answer to question about OLE.
I recommend using SDK because you can benefit the predefined object model of official API while in com interop there is no such guide.
You must have Openoffice SDK installed on your system, then in your c# project add reference to all dlls in this directory:
...\OpenOffice 4\sdk\cli\cli_basetypes.dll
These pages have practical samples of using APIs of c#:
http://suite101.com/a/creating-an-openoffice-calc-document-with-c-a124112 (404) http://www.oooforum.org/forum/viewtopic.phtml?t=107055
User contributions licensed under CC BY-SA 3.0