简体   繁体   English

MailMerge:从Excel到Word C#

[英]MailMerge: From Excel to Word C#

I'm currently facing a problem regarding the MailMerge functionality of MS Word. 我目前遇到有关MS Word的MailMerge功能的问题。

I had to rewrite an old VBA Application into C#. 我不得不将旧的VBA应用程序重写为C#。 I'm practically done with that. 我已经完成了。 New Application works fine. 新的应用程序工作正常。 Except for one PopUp that I cannot get rid of. 除了一个我无法摆脱的PopUp。 在此处输入图片说明

So I have been looking around on the web for the past 2 days because our clients don't want that pop up as it hasn't been there in the old application. 因此,过去两天我一直在网上浏览,因为我们的客户不希望弹出该窗口,因为旧应用程序中没有该弹出窗口。 However I couldn't find a proper solution for this. 但是,我找不到合适的解决方案。 Except a few people mentioning that probably the Connection string is incorrect. 除了少数人提到Connection字符串可能不正确之外。 But I found no resources telling me how it should look in the C# code 但是我发现没有资源告诉我它在C#代码中的外观

This it how it looks in the old application: 它在旧应用程序中的外观如下:

Word.ActiveDocument.MailMerge.OpenDataSource Name:=strSourceDoc, ConfirmConversions:=False, _
        ReadOnly:=False, LinkToSource:=True, AddToRecentFiles:=False, PasswordDocument:="", _
        PasswordTemplate:="", WritePasswordDocument:="", WritePasswordTemplate:="", _
        Revert:=False, Format:=wdOpenFormatAuto, Connection:= _
        "Provider=Microsoft.ACE.OLEDB.12.0;User ID=Admin;Data Source=" & strSourceDoc & ";Mode=Read;Extended Properties=""HDR=YES;IMEX=1;"";Jet OLEDB:System database="""";" _
        , SQLStatement:="SELECT * FROM `Tabelle1$`", SQLStatement1:="", SubType:= _
        wdMergeSubTypeAccess

I obviously tried already to take that connection key and use it in my code. 我显然已经尝试过使用该连接密钥并在我的代码中使用它。 But it does not prevent that pop up. 但这并不能阻止该弹出窗口。 I also tried playing around with the subtype. 我也尝试过使用子类型。 But it either doesn't change anything or throws a format exception. 但是它要么什么都没有改变,要么抛出格式异常。

This is whats working in C#: 这是在C#中工作的东西:

mailApp.ActiveDocument.MailMerge.OpenDataSource(processedPath + file, true, false, true, 
    true, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, 
   "Provider=Microsoft.ACE.OLEDB.12.0;User ID=Admin;Data Source=" + 
    processedPath + file + ";Mode=Read;", 
   "SELECT * FROM 'Tabelle1$'", 
   ref oMissing, ref oMissing, ref oMissing,
   Word.WdMergeSubType.wdMergeSubTypeAccess);

How can I change the connection string to prevent that popup from showing? 如何更改连接字符串以防止显示该弹出窗口?

So I found a solution, that is somehow working. 所以我找到了一个解决方案,以某种方式起作用。

The actual problem (at least from what I tested) is the file extension not the connection string. 实际问题(至少从我的测试来看)是文件扩展名而不是连接字符串。 I was using .xlsx files, as my source documents. 我正在使用.xlsx文件作为源文档。 But as soon as I tested with some xls Files the popup disapeared. 但是,一旦我对某些xls文件进行了测试,弹出窗口便消失了。

I just took a "google session" to find out the differences between xls and xlsx. 我只是参加了一个“ Google会话”以了解xls和xlsx之间的区别。

So I could change my code to work with xls Files only. 因此,我可以更改代码以仅使用xls文件。 Issue solved. 问题已解决。 But still an unpleasing solution for me tbh. 但是对我来说仍然是一个不愉快的解决方案。

If you'd like to test around a little to maybe get it working with xlsx. 如果您想进行一些测试,也许可以使其与xlsx一起使用。 Here is some code to test with (just bind it on a button click in winforms or something) 这是一些要测试的代码(只需将其绑定在Winforms或其他按钮上)

 class PerformMailMerge
    {
        Word.Application mailApp;
        Object oMissing = System.Reflection.Missing.Value;
        Object oFalse = false;
        string _path = @"Your Path to Excel File";
        string savePath = @"Your Path to Word Document";

        public void mailMerge2()
        {
            mailApp = new Word.Application();
            mailApp.Visible = false;

            mailApp.Documents.Add();

            mailApp.ActiveDocument.MailMerge.MainDocumentType = Word.WdMailMergeMainDocType.wdMailingLabels;

            //OpenDataSource: 
            mailApp.ActiveDocument.MailMerge.OpenDataSource(_path,
                Word.WdOpenFormat.wdOpenFormatAllWordTemplates, true, true, true,
                ref oMissing, ref oMissing, ref oMissing,
                ref oMissing, ref oMissing, ref oMissing, "TABLE Tabelle1$", "SELECT * FROM `Tabelle1$",
                ref oMissing, ref oMissing,
                Word.WdMergeSubType.wdMergeSubTypeWord2000);

            mailApp.ActiveDocument.SaveAs2(savePath);
            mailApp.ActiveDocument.Close();
            mailApp.Quit();
        }    
    }

EDIT: So in case anyone will stumble upon this again. 编辑:因此,以防万一任何人会再次迷失于此。 I found a solution to the problem. 我找到了解决问题的办法。 The solution is NOT specifying a WdMergeSubType. 解决方案不是指定WdMergeSubType。 This allows reading from xlsx files and still doesn't show the popup! 这样可以读取xlsx文件,但仍不显示弹出窗口!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 来自C#的word邮件合并打开多个实例 - mailmerge in word from c# opens multiple instances 在 C# 中保存来自 Word mailmerge 的新文档结果 - save the new document result from Word mailmerge in C# C#-MS Word-MailMerge如何在MailMerge.DataSource.LastRecord返回-16时获取最新记录 - C# - MS Word - MailMerge how to get Last Record when MailMerge.DataSource.LastRecord returns -16 C#4.0控制台Word MailMerge打印错误 - C# 4.0 Console Word MailMerge print error 当Access mdb具有密码时,来自C#的MailMerge - MailMerge from C# when Access mdb has password MailMerge - Excel(数据)到 Word(模板) - 使用 OpenXML - MailMerge - Excel (data) into Word (template) - With OpenXML 如何使用 C# 进入 Microsoft.Office.Interlop.Word Mailmerge 上的 NextRecord? - How do I advance to the NextRecord on Microsoft.Office.Interlop.Word Mailmerge with C#? 如何使用C#Interop.Word创建嵌套字段(MailMerge字段的IF选项) - How to create nested fields using C# Interop.Word (IF option with MailMerge Fields) C#将多个单元格从Excel复制到同一列中的Word - C# Copy Mutiple Cells from Excel to Word in same column 尝试从FirebirdDB邮件合并到Word中,但无法打开数据源 - Trying to mailmerge from FirebirdDB into Word but unable to open the data source
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM