如何在C#中使用word mailmerge发送电子邮件 [英] how to send email using word mailmerge in C#

查看:320
本文介绍了如何在C#中使用word mailmerge发送电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试了这两种方式来发送mailmerge文档,但对我来说没有用。



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:

 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:

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)我想知道为什么应用程序挂起在OpenDataSource()行,语法或设置有问题连接或sqlquery或路径(excelfile路径)对象是错误的。



2)在第二种方法中我没有关闭mydoc,仍然说它请求的对象不可用然后我做了先保存,然后尝试了这一行 mydoc.MailMerge.Destination = Word.WdMailMergeDestination.wdSendToEmail; 我仍然得到同样的错误。



3)我想知道使用wordmail merge发送电子邮件的正确方法。请你指导我如何使用word mailmerge来实现这个目标。



请告诉我在哪里我出错了,这是怎么做的正确方法。请给我一个例子,以便我能以更好的方式理解。



提前致谢。



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.

推荐答案

//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");





参考资料

将文本发送到MS Word 2010中的MailMerge字段 [ ^ ]



如何使用C#/ VB.NET中的Word文档中的邮件合并功能 [ ^ ]


这是在c#中解决问题的功能代码



This is the function code to solve your problem in 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.

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


这篇关于如何在C#中使用word mailmerge发送电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆