none
Error : Requested object is not available in Mail merge word while trying to send email RRS feed

  • Question

  • I am able to create no.of Documents according to DataSource (excel data) and would like to send same document to concerened person emailid (mentioned in excel) but I get this above error. Here is the below code i am using :

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

    Title.doc is my template and am using excel as datasource.I am trying to send email to mailmerge doc to emailid mentioned in excelsheet.

    I am novice in this topic .could you please let me what went wrong and its correction to get the required result.

    Thanks in Advance

    Thursday, August 1, 2013 8:08 AM

Answers

  • Some observations:

    You're creating two documents, one using Word's defaults, one from your "template". Both originally assigned to the object mydoc. I recommend you comment out or remove the first such line of code.

    You then save AND CLOSE mydoc. If you want to do something with it, which apparently you do - merge it to email - you should not CLOSE it. That's why you're getting the error: It's no longer there if you close it.

    Also, do not use ActiveDocument. You would have an object for the document : mydoc. Use that and do NOT use ActiveDocument.

    Finally, mail merge was not designed to be used in the manner you're using it: typing over the mail merge field's result. It's quite possible, when you execute the merge to email, that the values of the fields will revert...


    Cindy Meister, VSTO/Word MVP, my blog

    Thursday, August 1, 2013 3:51 PM
    Moderator
  • Hi hari

    #2 would be the basic correct approach.

    I recommend you test first as an end-user in the Word environment - you need to get it working there, then you can record the steps in a macro to get the basic syntax Word requires.

    If you run into problems with the steps as an end-user, I recommend you ask in the Communities about how to get things to work. Mail merge to Email is not my specialty and it can be quite tricky. You'll find people in the forum on the Communities who have experience with getting it to work:

    http://answers.microsoft.com/en-us/office/forum/word


    Cindy Meister, VSTO/Word MVP, my blog

    Friday, August 2, 2013 12:39 PM
    Moderator

All replies

  • Some observations:

    You're creating two documents, one using Word's defaults, one from your "template". Both originally assigned to the object mydoc. I recommend you comment out or remove the first such line of code.

    You then save AND CLOSE mydoc. If you want to do something with it, which apparently you do - merge it to email - you should not CLOSE it. That's why you're getting the error: It's no longer there if you close it.

    Also, do not use ActiveDocument. You would have an object for the document : mydoc. Use that and do NOT use ActiveDocument.

    Finally, mail merge was not designed to be used in the manner you're using it: typing over the mail merge field's result. It's quite possible, when you execute the merge to email, that the values of the fields will revert...


    Cindy Meister, VSTO/Word MVP, my blog

    Thursday, August 1, 2013 3:51 PM
    Moderator
  • Cindy,thanks a lot for valuable suggestion.How can i send NewDocument to particular email id in excel sheet.please correct  me what i am doing wrong and how can i achieve this?

    Friday, August 2, 2013 5:16 AM
  • I tried two ways

    One:                          mydoc.MailMerge.Destination = Word.WdMailMergeDestination.wdSendToEmail;//gives the same error
                                mydoc.MailMerge.MailFormat = Word.WdMailMergeMailFormat.wdMailFormatHTML;
                                // mydoc.MailMerge.MailAsAttachment = false;
                                mydoc.MailMerge.MailSubject = "Hi welcome to Test1";
                                mydoc.MailMerge.MailAddressFieldName = "EmailAddress";
                                mydoc.MailMerge.Execute(ref oNotTrue); 

    two:

    object oFilename = @"D:\Title.doc"; // our word template
                myWordApp.Visible = false; // tell word not to show itself
                string path = txtBrowse.Text;
                object oPath = path;
                object oConnection = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" + txtBrowse.Text + "; Extended Properties=Excel 12.0 Xml";
                object oSqlStmt = "Select * from [" + excelsheets[jCount] + "]";

     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);
                                Microsoft.Office.Interop.Word.Document mailMergedoc = myWordApp.Documents.Open(ref oFilename, 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);
                                mailMergedoc.MailMerge.OpenDataSource(path, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oConnection, ref oSqlStmt, ref oMissing, ref oMissing, ref oMissing);
                               //System hangs on execution of  mailMergedoc.MailMerge.OpenDataSource();
                                mailMergedoc.MailMerge.Destination = Word.WdMailMergeDestination.wdSendToEmail;
                                mailMergedoc.MailMerge.Execute();

    can you suggest me any idea to achieve this

     
    Friday, August 2, 2013 7:05 AM
  • Hi hari

    #2 would be the basic correct approach.

    I recommend you test first as an end-user in the Word environment - you need to get it working there, then you can record the steps in a macro to get the basic syntax Word requires.

    If you run into problems with the steps as an end-user, I recommend you ask in the Communities about how to get things to work. Mail merge to Email is not my specialty and it can be quite tricky. You'll find people in the forum on the Communities who have experience with getting it to work:

    http://answers.microsoft.com/en-us/office/forum/word


    Cindy Meister, VSTO/Word MVP, my blog

    Friday, August 2, 2013 12:39 PM
    Moderator