كيفية إرسال البريد الإلكتروني باستخدام Word Mailmerge في 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] + "]";

النهج الأول:

ج #
 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();

النهج الثاني:

ج #
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 أو المسار (مسار ملف Excel) خطأ.

2) في الطريقة الثانية، لا أقوم بإغلاق mydoc، ولا يزال يقول إن الكائن المطلوب غير متوفر، ثم قمت بالحفظ أولاً ثم جربت هذا السطر mydoc.MailMerge.Destination = Word.WdMailMergeDestination.wdSendToEmail; ما زلت أحصل على نفس الخطأ.

3) أريد أن أعرف الطريقة الصحيحة لإرسال البريد الإلكتروني باستخدام دمج بريد الكلمات. هل يمكن أن يرشدني أحد إلى كيفية تحقيق ذلك باستخدام دمج بريد الكلمات.

يرجى إخباري أين أخطأت وما هي الطريقة الصحيحة للقيام بذلك. يرجى تقديم مثال لي أيضًا، حتى أتمكن من الفهم بطريقة أفضل.

شكرا لك مقدما.

الحل 1

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

مراجع
إرسال النص إلى حقول MailMerge في MS Word 2010[^]

كيفية استخدام وظيفة دمج المراسلات في مستند Word باستخدام C#/VB.NET[^]

الحل 2

هذا هو رمز الوظيفة لحل مشكلتك في 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);
    }
}

وهذا هو الكود النهائي.

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

コメント

タイトルとURLをコピーしました