MailMerge:从Excel到Word C# [英] MailMerge: From Excel to Word C#

查看:185
本文介绍了MailMerge:从Excel到Word C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前遇到有关MS Word的MailMerge功能的问题.

I'm currently facing a problem regarding the MailMerge functionality of MS Word.

我不得不将旧的VBA应用程序重写为C#.我实际上已经完成了.新的应用程序工作正常. 除了一个我无法摆脱的PopUp.

I had to rewrite an old VBA Application into C#. I'm practically done with that. New Application works fine. Except for one PopUp that I cannot get rid of.

因此,过去两天我一直在网上浏览,因为我们的客户不希望弹出该窗口,因为旧应用程序中没有该弹出窗口. 但是,我找不到合适的解决方案.除了少数人提到Connection字符串可能不正确之外.但是我发现没有资源告诉我它在C#代码中的外观

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. But I found no resources telling me how it should look in the C# code

这是它在旧应用程序中的外观:

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.

这是在C#中的工作方式:

This is whats working in 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.

实际的问题(至少从我的测试来看)是文件扩展名而不是连接字符串.我使用.xlsx文件作为我的源文件.但是,一旦我对某些xls文件进行了测试,弹出窗口便消失了.

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. But as soon as I tested with some xls Files the popup disapeared.

我刚刚参加了一次"Google会话",以了解xls和xlsx之间的区别.

因此,我可以更改代码以仅与xls文件一起使用.问题已解决.但是对我来说仍然是一个不愉快的解决方案.

So I could change my code to work with xls Files only. Issue solved. But still an unpleasing solution for me tbh.

如果您想进行一些测试,也许可以使其与xlsx一起使用.这是一些要测试的代码(只需将其绑定在单击winforms或其他内容的按钮上)

If you'd like to test around a little to maybe get it working with xlsx. Here is some code to test with (just bind it on a button click in winforms or something)

 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();
        }    
    }

因此,以防万一有人再次发现这一点.我找到了解决问题的办法.解决方案不是指定WdMergeSubType.这样可以读取xlsx文件,但仍不显示弹出窗口!

So in case anyone will stumble upon this again. I found a solution to the problem. The solution is NOT specifying a WdMergeSubType. This allows reading from xlsx files and still doesn't show the popup!

这篇关于MailMerge:从Excel到Word C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆