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 या पाथ (एक्सेलफाइल पाथ) ऑब्जेक्ट में कुछ गड़बड़ है।

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

संदर्भ
MS Word 2010 में MailMerge फ़ील्ड्स पर टेक्स्ट भेजना[^]

C#/VB.NET के साथ Word दस्तावेज़ में मेल मर्ज फ़ंक्शन का उपयोग कैसे करें[^]

समाधान 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をコピーしました