Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 错误:尝试发送电子邮件时,请求的对象在邮件合并word中不可用_C#_Wpf_Ms Word_Mailmerge - Fatal编程技术网

C# 错误:尝试发送电子邮件时,请求的对象在邮件合并word中不可用

C# 错误:尝试发送电子邮件时,请求的对象在邮件合并word中不可用,c#,wpf,ms-word,mailmerge,C#,Wpf,Ms Word,Mailmerge,我能够根据数据源(excel数据)创建数量的文档,并希望将相同的文档发送给相关人员emailid(excel中提到),但我收到上述错误。 以下是我正在使用的代码: private void GenerateMultipleDocumentAndSendEmail() { Microsoft.Office.Interop.Word.Application myWordApp = new Microsoft.Office.Interop.Word.Applica

我能够根据数据源(excel数据)创建数量的文档,并希望将相同的文档发送给相关人员emailid(excel中提到),但我收到上述错误。 以下是我正在使用的代码:

      private void GenerateMultipleDocumentAndSendEmail()
    {

        Microsoft.Office.Interop.Word.Application myWordApp = new Microsoft.Office.Interop.Word.Application(); 
        object oMissing = System.Reflection.Missing.Value; 
        object oNotTrue = false; 
        object oFilename = @"D:\Title.doc"; // word template
        myWordApp.Visible = false; 
        //Create connection object
        string constring = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" + txtBrowse.Text + "; Extended Properties=Excel 12.0 Xml";
        OleDbConnection con = new OleDbConnection(constring);
        try
        {
            int iCount = 0;
            con.Open();
            dt = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
            string[] excelsheets = new string[dt.Rows.Count];
            foreach (System.Data.DataRow dr in dt.Rows)
            {
                excelsheets[iCount] = dr["TABLE_NAME"].ToString();
                iCount++;
            }
            for (int jCount = 0; jCount < excelsheets.Length; jCount++)
            {
                OleDbCommand cmd = new OleDbCommand("Select * from [" + excelsheets[jCount] + "]");
                cmd.Connection = con;
                //cmd.CommandText=CommandBindings
                try
                {
                    OleDbDataReader reader = cmd.ExecuteReader();
                    int i = 1;
                    while (reader.Read())
                    {
                        Microsoft.Office.Interop.Word.Document mydoc = myWordApp.Documents.Add();//new Microsoft.Office.Interop.Word.Document();//
                        object destination = @"D:\NewDocument" + i.ToString() + ".doc";
                        System.Collections.Generic.Dictionary<string, string> row = new System.Collections.Generic.Dictionary<string, string>();
                        row.Add("Title", reader.GetString(0).ToString());
                        row.Add("FirstName", reader.GetString(1).ToString());
                        row.Add("MiddleName", reader.GetString(2).ToString());
                        row.Add("LastName", reader.GetString(3).ToString());
                        row.Add("Suffix", reader.GetString(4).ToString());
                        row.Add("Company", reader.GetString(5).ToString());
                        row.Add("EmailAddress", reader.GetString(6).ToString());
                        mydoc = myWordApp.Documents.Add(ref oFilename, ref oMissing, ref oMissing, ref oMissing);

                        foreach (Word.MailMergeField myField in mydoc.MailMerge.Fields)
                        {
                            myField.Select();
                            string key = myWordApp.Selection.Text;
                            key = key.Replace("»", "").Replace("«", "");

                            if (row.ContainsKey(key))
                            {
                                string text = row[key];
                                myWordApp.Selection.TypeText(text);
                            }
                        }

                        mydoc.SaveAs(ref destination, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
                        mydoc.Close(ref oNotTrue, ref oMissing, ref oMissing);
                       //mydoc = myWordApp.Documents.Open(ref destination, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
                        myWordApp.ActiveDocument.MailMerge.Destination = Word.WdMailMergeDestination.wdSendToEmail; //am getting error at this line as Requested object is not available.
                      //  mydoc.MailMerge.MailFormat = Word.WdMailMergeMailFormat.wdMailFormatPlainText;
                        myWordApp.ActiveDocument.MailMerge.MailAsAttachment = false;
                        myWordApp.ActiveDocument.MailMerge.MailSubject = "Hi welcome to Test1";
                        myWordApp.ActiveDocument.MailMerge.MailAddressFieldName = "EmailAddress";
                        myWordApp.ActiveDocument.MailMerge.Execute(ref oNotTrue);



                        i++;
                    }//end while
                    //reader.Close();
                }//try end
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                finally
                {
                    cmd.Dispose();
                    con.Close();
                    con.Dispose();
                    myWordApp.Application.Quit(ref oNotTrue, ref oMissing, ref oMissing);
                }
            }
        }



        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }

    }
private void GenerateMultipleDocumentAndSendEmail()
{
Microsoft.Office.Interop.Word.Application myWordApp=新的Microsoft.Office.Interop.Word.Application();
对象omising=System.Reflection.Missing.Value;
对象oNotTrue=false;
文件名的对象=@“D:\Title.doc”;//word模板
myWordApp.Visible=false;
//创建连接对象
string constring=“Provider=Microsoft.ACE.OLEDB.12.0;数据源=“+txtBrowse.Text+”;扩展属性=excel12.0xml”;
OLEDB连接con=新OLEDB连接(构造);
尝试
{
int-iCount=0;
con.Open();
dt=con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables,null);
string[]excelsheets=新字符串[dt.Rows.Count];
foreach(System.Data.DataRow dr in dt.Rows)
{
excelsheets[iCount]=dr[“TABLE_NAME”].ToString();
iCount++;
}
for(int jCount=0;jCount

Title.doc是我的模板,我使用excel作为数据源。我正在尝试将电子邮件发送到mailmerge doc,发送到excel工作表中提到的emailid。

我假设您正在打开一个未定义任何mailmerge字段的文档。我想将保存的NewDocument1.doc发送到excel中相应的电子邮件地址。无法获取它。我如何定义mailmerge字段。你能建议我如何定义吗?通常的步骤是:用word插入文本创建文档模板,用常用工具(插入->字段->合并字段->命名->确定)合并字段。完成后,将此模板保存到应用程序的已知文件夹中。在代码中打开此文件并用最终名称另存为它,应用代码替换合并字段和