Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I tried these two ways to send mailmerge document but didnt work for me.

C#
object oFilename = @"D:\Title.doc"; //  word template
          myWordApp.Visible = false;
          string path = txtBrowse.Text;//datasource path which is excel
          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] + "]";


1st approach:
C#
 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();


2nd approach:
C#
mydoc.MailMerge.Destination = Word.WdMailMergeDestination.wdSendToEmail;//gives the error as Requested object is not avaialble.
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);
myWordApp.Visible = true;


1)I would like to know why application hangs at OpenDataSource() line ,is something wrong with syntax or setting connection or sqlquery or path(excelfile path)object is wrong.

2) In second approach am not closing the mydoc,still it say Requested object is unavailable then i did save first then tried this line mydoc.MailMerge.Destination = Word.WdMailMergeDestination.wdSendToEmail; still i get the same error.

3) I want to know correct way of sendin g email using wordmail merge.Could you please any one guide me how to achieve this by using word mailmerge.

Kindly let me know where i went wrong and what is correct way of doing it.Please provide me example too ,so that i can understand in a better way .

Thanks in advance.
Posted

C#
//Load Document  
Document document = new Document();  
document.LoadFromFile(@"D:\template\Fax.doc");  
   
//Data information  
string[] filedNames = new string[]{"Contact Name","Fax","Date"};  
  
string[] filedValues = new string[]{"John Smith","+1 (69) 123456",System.DateTime.Now.Date.ToString()};  
  
//Mail Merge  
document.MailMerge.Execute(filedNames, filedValues);  
  
//Save and Launch  
document.SaveToFile("FaxMailMerge.docx", FileFormat.Docx);  
System.Diagnostics.Process.Start("FaxMailMerge.docx");


References
Sending text to MailMerge Fields in MS Word 2010[^]

How to Use Mail Merge Function in Word Document with C#/VB.NET[^]
 
Share this answer
 
v4
Comments
keerth516 2-Aug-13 5:51am    
Thanks for the reply..parveen.I want to send mailmerge document to particular emailid in excel (as DataSource) as explained in above post.I am stuck at OpenDataSource method itself..Can u please suggest me any idea to achieve this
This is the function code to solve your problem in c#

C#
mailMerge = doc.MailMerge;
    
foreach (Word.MailMergeField f in mailMerge.Fields)
{
    // Extract the name of the MergeField starting from the 11 character
    // and looking for the first space after the name
    // (this means that you avoid MergeFields names with embedded spaces)
    string fieldName = f.Code.Text.Substring(11).Trim();
    int  pos = fieldName.IndexOf(' ');

    if (pos >= 0) fieldName = fieldName.Substring(0, pos).Trim();
    
    if (fieldName == pMergeField)
    {
       f.Select();
       app.Selection.TypeText(pValue);
    }
}

And this is the final code.
C#
public static void TextToWord(string pWordDoc, string pMergeField, string pValue)
{
    Object oMissing = System.Reflection.Missing.Value;
    Object oTrue = true;
    Object oFalse = false;
    Word.Application oWord = new Word.Application();
    Word.Document oWordDoc = new Word.Document();
    oWord.Visible = true;
    Object oTemplatePath = pWordDoc;
    oWordDoc = oWord.Documents.Add(ref oTemplatePath, ref oMissing, ref oMissing, ref oMissing);

    foreach (Word.Field myMergeField in oWordDoc.Fields)
    {
        Word.Range rngFieldCode = myMergeField.Code;
        String fieldText = rngFieldCode.Text;

        if (fieldText.StartsWith(" MERGEFIELD"))
        {
            Int32 endMerge = fieldText.IndexOf("\\");
            Int32 fieldNameLength = fieldText.Length - endMerge;
            String fieldName = fieldText.Substring(11, endMerge - 11);
            fieldName = fieldName.Trim();
            if (fieldName == pMergeField)
            {
                myMergeField.Select();
                oWord.Selection.TypeText(pValue);
            }
        }
    }
}
 
Share this answer
 
v2
Comments
keerth516 2-Aug-13 6:06am    
Hi Benz1 Thanks for the reply, how can i send email mailmerge word document to particular parson.Let us say i have merge document as NewDocumnet.doc, then how can i send a email this NewDocument.doc as a attachment or Html body..?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900