从 C# ASP.NET 中的数据集进行邮件合并

发布于 2024-08-07 12:56:01 字数 2809 浏览 3 评论 0原文

我是 OpenOffice 的新手,并且阅读了很多有关它的文章。我需要用 C# 编写邮件合并功能的代码,以指示 OpenOffice 对提供的文档执行邮件合并。我见过很多使用 VB.NET 的邮件合并示例,但是当我将 VB 转换为 C# 时,它的邮件合并组件总是无法正确创建。我的代码如下。我已经注释掉了邮件合并代码,因为它不与它一起编译。我将如何更正代码以使其执行邮件合并?

public int InitialiseOpenOffice() 
{ 
    // This is the document with the mail merge tags in that I want to be used as the source. 
    string filePathStr = "file:///C:/DotNetDev/MailMerge//MailMergeSample.doc"; 

    Type openOffice; 
    openOffice = Type.GetTypeFromProgID("com.sun.star.ServiceManager"); 
    object objServiceManager = System.Activator.CreateInstance(openOffice); 

    // 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/swriter"; 
    args[1] = "_blank"; 
    args[2] = 0; 
    args[3] = new object[] { }; 

    // arguments for document 
    object[] existingargs = new object[4]; 
    existingargs[0] = filePathStr; 
    existingargs[1] = "_blank"; 
    existingargs[2] = 0; 
    existingargs[3] = new object[] { }; 

    object startdesktop; 
    object doc; 
    try 
    { 
       startdesktop = (object)openOffice.InvokeMember("createInstance", 
       BindingFlags.InvokeMethod, null, 
       objServiceManager, parameters); 
       doc = startdesktop.GetType().InvokeMember("loadComponentFromUrl", 
       BindingFlags.InvokeMethod, null, startdesktop, existingargs); 

       object openOfficeServiceManagerObj = System.Activator.CreateInstance(openOffice); 

       // arguments for MailMerge 
       object[] mailMergeParameters = new object[1]; 
       mailMergeParameters[0] = "com.sun.star.text.MailMerge"; 
       // 
       // 
       // All the code up to this point works, but the code below doesn't.  At this 
       // point OponOffice is open with the document I want to use in the mailmerge. 
       //
       //
       //Type t_OOo; 
       //t_OOo = Type.GetTypeFromProgID("com.sun.star.ServiceManager"); 
       //objServiceManager = System.Activator.CreateInstance(t_OOo); 
       //object oMailMerge; 
       //oMailMerge = t_OOo.InvokeMember("createInstance", BindingFlags.InvokeMethod, null, objServiceManager, new Object[] { "com.sun.star.text.MailMerge" }); 
       //oMailMerge.DocumentURL = "file:///C:/Users/me/Desktop/OpenOffice Investigation/mail merged.odt"; 
       //oMailMerge.DataSourceName = "adds"; 
       //oMailMerge.CommandType = 0; 
       //oMailMerge.Command = "adds"; 
       //oMailMerge.OutputType = 2; 
       //oMailMerge.execute(new Object[] { }); 

       if (doc == null) 
       { 
         return 1; // error!! 
       } 

   } 
   catch (Exception e) 
   { 
       string s = e.Message; 
       return 1; 
   } 

   return 0; 

}

I am new to OpenOffice and have been reading lots of articles about it. I need to code mail merge functionality in C# to instruct OpenOffice to perform a mail merge on a supplied document. I have seen lots of mail merge examples using VB.NET but when I convert the VB to C# the mailmerge component of it always fails to create correctly. My code is as follows. I have commented out the mail merge code as it doesn't compile with it in. How would I correct the code so it performed the mail merge?

public int InitialiseOpenOffice() 
{ 
    // This is the document with the mail merge tags in that I want to be used as the source. 
    string filePathStr = "file:///C:/DotNetDev/MailMerge//MailMergeSample.doc"; 

    Type openOffice; 
    openOffice = Type.GetTypeFromProgID("com.sun.star.ServiceManager"); 
    object objServiceManager = System.Activator.CreateInstance(openOffice); 

    // 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/swriter"; 
    args[1] = "_blank"; 
    args[2] = 0; 
    args[3] = new object[] { }; 

    // arguments for document 
    object[] existingargs = new object[4]; 
    existingargs[0] = filePathStr; 
    existingargs[1] = "_blank"; 
    existingargs[2] = 0; 
    existingargs[3] = new object[] { }; 

    object startdesktop; 
    object doc; 
    try 
    { 
       startdesktop = (object)openOffice.InvokeMember("createInstance", 
       BindingFlags.InvokeMethod, null, 
       objServiceManager, parameters); 
       doc = startdesktop.GetType().InvokeMember("loadComponentFromUrl", 
       BindingFlags.InvokeMethod, null, startdesktop, existingargs); 

       object openOfficeServiceManagerObj = System.Activator.CreateInstance(openOffice); 

       // arguments for MailMerge 
       object[] mailMergeParameters = new object[1]; 
       mailMergeParameters[0] = "com.sun.star.text.MailMerge"; 
       // 
       // 
       // All the code up to this point works, but the code below doesn't.  At this 
       // point OponOffice is open with the document I want to use in the mailmerge. 
       //
       //
       //Type t_OOo; 
       //t_OOo = Type.GetTypeFromProgID("com.sun.star.ServiceManager"); 
       //objServiceManager = System.Activator.CreateInstance(t_OOo); 
       //object oMailMerge; 
       //oMailMerge = t_OOo.InvokeMember("createInstance", BindingFlags.InvokeMethod, null, objServiceManager, new Object[] { "com.sun.star.text.MailMerge" }); 
       //oMailMerge.DocumentURL = "file:///C:/Users/me/Desktop/OpenOffice Investigation/mail merged.odt"; 
       //oMailMerge.DataSourceName = "adds"; 
       //oMailMerge.CommandType = 0; 
       //oMailMerge.Command = "adds"; 
       //oMailMerge.OutputType = 2; 
       //oMailMerge.execute(new Object[] { }); 

       if (doc == null) 
       { 
         return 1; // error!! 
       } 

   } 
   catch (Exception e) 
   { 
       string s = e.Message; 
       return 1; 
   } 

   return 0; 

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

壹場煙雨 2024-08-14 12:56:01

看起来这个问题已在 Mailmerge using OpenOffice 中得到解决...

Looks like this has been addressed at Mailmerge using OpenOffice...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文