0
Answer

how to send email mailmerge word document&Datasource Excel

kida atlantis

kida atlantis

11y
1.6k
1
I tried these two ways to send mailmerge document but didnt work for me.

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:
 Collapse | Copy Code
 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:
 Collapse | Copy Code
 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.