如何从C#向com.sun.star.frame.DispatchHelper发送命令

时间:2019-07-02 12:01:29

标签: c# com libreoffice late-binding openoffice-writer

我正在使用.NET Framework 3.5,并尝试打开磁盘上的文档,并使用DispatchHelper将文本写入其中:

internal static void open_office_test(string fn)
{
   object open_office_app = Activator.CreateInstance(Type.GetTypeFromProgID("com.sun.star.ServiceManager"));
   object obj_desktop = open_office_app.GetType().InvokeMember("createInstance",
                                     BindingFlags.Public | BindingFlags.Instance | BindingFlags.InvokeMethod,
                                     null, open_office_app, new object[] { "com.sun.star.frame.Desktop" });

   //open file by fn
   object w_doc = obj_desktop.GetType().InvokeMember("loadComponentFromURL",
                               BindingFlags.Public | BindingFlags.Instance | BindingFlags.InvokeMethod,
                               null, obj_desktop, new object[] { new Uri(fn, UriKind.Absolute).AbsoluteUri, "_blank", 0, new object[]{ create_property(obj_desktop, "Hidden", false) } });

   /*get CurrentController.Frame*/
   object w_doc_cc = w_doc.GetType().InvokeMember("CurrentController",
                                  BindingFlags.Public | BindingFlags.Instance | BindingFlags.GetProperty,
                                  null, w_doc, null);
   object w_doc_frame = w_doc_cc.GetType().InvokeMember("Frame",
                                     BindingFlags.Public | BindingFlags.Instance | BindingFlags.GetProperty,
                                     null, w_doc_cc, null);
   /*****************************/

   /*create dispatcher and send InsertText command*/
   object dispatcher = open_office_app.GetType().InvokeMember("createInstance",
                                    BindingFlags.Public | BindingFlags.Instance | BindingFlags.InvokeMethod,
                                    null, open_office_app, new object[] { "com.sun.star.frame.DispatchHelper" });

   dispatcher.GetType().InvokeMember("executeDispatch",
                BindingFlags.Public | BindingFlags.Instance | BindingFlags.InvokeMethod,
                null, w_doc_frame, new object[] { ".uno:InsertText", "", 0, create_property(obj_desktop, "Text", "aaa") });
   /***********************************************/

}

private static object create_property<T>(object obj_desktop, string name, T value)
{
   object w_prop = obj_desktop.GetType().InvokeMember("Bridge_GetStruct",
                                BindingFlags.Public | BindingFlags.Instance | BindingFlags.InvokeMethod,
                                null, obj_desktop, new object[] { "com.sun.star.beans.PropertyValue" });
   Type w_prop_type = w_prop.GetType();
            w_prop_type.InvokeMember("Name",
                BindingFlags.Public | BindingFlags.Instance | BindingFlags.SetProperty,
                null, w_prop, new object[] { name });
   w_prop_type.InvokeMember("Value",
                BindingFlags.Public | BindingFlags.Instance | BindingFlags.SetProperty,
                null, w_prop, new object[] { value });
   return w_prop;
}

但是执行executeDispatch会导致

  

System.Runtime.InteropServices.COMException HRESULT:0x80020006   (DISP_E_UNKNOWNNAME))

P.S。我只能使用反射


解决方案是代码行

dispatcher.GetType().InvokeMember("executeDispatch",
                    BindingFlags.Public | BindingFlags.Instance | BindingFlags.InvokeMethod,
                    null, w_doc_frame, new object[] { ".uno:InsertText", "", 0, create_property(obj_desktop, "Text", "aaa") });

应该像

dispatcher.GetType().InvokeMember("executeDispatch",
                    BindingFlags.Public | BindingFlags.Instance | BindingFlags.InvokeMethod,
                    null, dispatcher, new object[] { w_doc_frame, ".uno:InsertText", "", 0, new object[] { create_property(obj_desktop, "Text", "aaa") } });

0 个答案:

没有答案