执行邮件合并并生成许多文档.类型不匹配错误

本文关键字:文档 许多 类型 不匹配 错误 合并 执行 | 更新日期: 2023-09-27 18:03:02

            //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;

我尝试了我能想到的所有方法来产生多个字母(为了更容易打印,留在同一个文档中),但我一直得到一个类型不匹配的错误。有人能帮忙吗?欢迎提出任何意见。

执行邮件合并并生成许多文档.类型不匹配错误

您描述的特定错误发生是因为. execute的参数不是您想要使用的数据集(ds),而是一个布尔值,指定当合并中出现错误时Word会做什么。

很难看出这一点,因为它部分取决于您的CreateDataDoc方法的确切功能