我能够根据 DataSource(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);
}
}
Title.doc 是我的模板,我使用 excel 作为数据源。我正在尝试将电子邮件发送到 mailmerge doc 到 excelsheet 中提到的 emailid。