执行邮件合并并生成许多文档。键入不匹配错误

时间:2015-07-16 20:50:03

标签: c# c#-4.0 mailmerge

            //instantiate wordDoc and wordApp
        string PathDoc, DateTemp = string.Empty;
        DateTime dDateTemp = new DateTime();
        dDateTemp = Convert.ToDateTime(sDate);

        var ds = new DataSet();
        string textDate = dDateTemp.ToString("MM/dd/yy");
        switch (msDateType)
        {
            case "D":
                sSQL = string.Format(msQuery+"'{0}'",textDate);
                break;
            case "W":
                dDateTemp = dDateTemp.AddDays(4);
                DateTemp = dDateTemp.Month.ToString("MM") + "/" + dDateTemp.Day.ToString("dd")
                    + "/" + dDateTemp.Year.ToString("yy");
                sSQL = msQuery + " @textDate";
                //sSQL = msQuery + " '" + textDate + "' AND '" + DateTemp + "'";
                MessageBox.Show(sSQL);
                break;
            case "N":
                sSQL = msQuery;
                break;
            default:
                break;
        }
        string test = "SELECT * FROM ltrFammedNoShow WHERE ApptDt = '06/01/2015'";

        int same = string.Compare(test, sSQL, true);
        using (cn = new SqlConnection(connectionString))
        {
            cn.Open();
            using (SqlCommand command = cn.CreateCommand())
            {
                command.CommandText = test;
                // "select top 10 * from ltrFammedNoShow"; // 
                //command.Parameters.Add(@textDate, SqlDbType.NVarChar,15);
                //command.Parameters[@textDate].Value = textDate;
                //MessageBox.Show(command.CommandText);
                var adapter = new SqlDataAdapter(command);
                adapter.Fill(ds);
            }
        }
        //Set the path and document of the letter to be used for the merge 
        PathDoc = msPath + "\"" + msDocument;

        //Create a new instance of the word Application
        wordApp = new Microsoft.Office.Interop.Word.Application();

        //Add a new document
        wordDoc = wordApp.Documents.Add(ref oMissing, ref oMissing, ref oMissing, ref oMissing);
        wordDoc.Select();

        //Create MailMerge data document
        CreateDataDoc(ref wordApp, ref ds);         
        wordDoc = wordApp.Documents.Add(Template: @"D:\ClinicLetters\Fammed\NoShow.doc");
        wordApp.Visible = true;
        wordDoc.MailMerge.OpenDataSource(@"D:\\data.doc");

        //wordDoc.MailMerge.Destination = WdMailMergeDestination.wdSendToNewDocument;
        //wordDoc.MailMerge.Execute(ref oFalse);

        wordDoc.MailMerge.Execute(ds);
        //wordDoc.Words.Last.InsertBreak(Microsoft.Office.Interop.Word.WdBreakType.wdPageBreak);

        //Close the original form document
        wordDoc.Saved = false;
        wordDoc.Close(ref oFalse, ref oMissing, ref oMissing);

        //Release reference
        wordDoc = null;
        wordApp = null;

我尝试了所有我能想到的产品多个字母(为了更容易打印而保留在同一个文档中),但我发现了类型不匹配错误。有人可以帮忙吗?所有意见都非常感谢。提前谢谢。

1 个答案:

答案 0 :(得分:1)

您描述的特定错误是因为.Execute的参数不是您要使用的DataSet(ds),而是一个布尔值,用于指定在合并中发生错误时Word所执行的操作。

很难看到,因为它部分取决于你的CreateDataDoc方法的作用。