locked
mailmerge.opendatasource in c# RRS feed

  • Question

  • User1776267435 posted
     Microsoft.Office.Interop.Word.Application oWord = new Microsoft.Office.Interop.Word.Application();
                    Microsoft.Office.Interop.Word.Document oWordDoc = new Microsoft.Office.Interop.Word.Document();
                    //oWord.Visible = true;
                    Object oTemplatePath = @"D:hh.doc";
                    oWordDoc = oWord.Documents.Open(oTemplatePath);
                    oWord.Visible = true;
                    object nullobject = Missing.Value;
                    object oConnection = "Data Source=.\\SQLEXPRESS;Initial catalog=RoadWarrior;Integrated Security=true"; //The DSN connection name
                    object sqlStmt = "select ContactName ,Email from CustomerContacts where Email != ''";
    
                    oWordDoc.MailMerge.OpenDataSource("", ref nullobject, ref nullobject, ref nullobject, ref nullobject, ref nullobject,
                        ref nullobject, ref nullobject, ref nullobject, ref nullobject, ref nullobject, ref oConnection, ref sqlStmt, ref nullobject,
                        ref nullobject, ref nullobject);
                    oWordDoc.MailMerge.Execute(ref nullobject);
                    oWordDoc.SaveAs(@"D:\\mydoc.docx");   
                    

    i got the error like : Command Failed

    Can anyone pls help me to move further from this error.

    Thanks in Advance

    Tuesday, January 22, 2013 2:27 AM

Answers

  • User1776267435 posted

    After long search i got the code.

    Microsoft.Office.Interop.Word.Application oWord = new Microsoft.Office.Interop.Word.Application();
    Microsoft.Office.Interop.Word.Document oWordDoc = new Microsoft.Office.Interop.Word.Document();
                  
    Object oTemplatePath = @"D:MyWay.doc";
    oWordDoc = oWord.Documents.Open(oTemplatePath);
    object nullobject = Missing.Value;       
    object qry = "select *from [Demo$]"; oWordDoc.MailMerge.OpenDataSource("D:\\dd.xls", ref nullobject, ref nullobject, ref nullobject, ref nullobject, ref nullobject, ref nullobject, ref nullobject, ref nullobject, ref nullobject, ref nullobject, ref nullobject, ref qry , ref nullobject, ref nullobject, ref nullobject); oWordDoc.MailMerge.Execute(ref nullobject);

    MyWay.doc -> Template document which i designed in Word

    dd.xls -> Excel sheet as a database

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, January 23, 2013 2:38 AM

All replies

  • User1776267435 posted

    After long search i got the code.

    Microsoft.Office.Interop.Word.Application oWord = new Microsoft.Office.Interop.Word.Application();
    Microsoft.Office.Interop.Word.Document oWordDoc = new Microsoft.Office.Interop.Word.Document();
                  
    Object oTemplatePath = @"D:MyWay.doc";
    oWordDoc = oWord.Documents.Open(oTemplatePath);
    object nullobject = Missing.Value;       
    object qry = "select *from [Demo$]"; oWordDoc.MailMerge.OpenDataSource("D:\\dd.xls", ref nullobject, ref nullobject, ref nullobject, ref nullobject, ref nullobject, ref nullobject, ref nullobject, ref nullobject, ref nullobject, ref nullobject, ref nullobject, ref qry , ref nullobject, ref nullobject, ref nullobject); oWordDoc.MailMerge.Execute(ref nullobject);

    MyWay.doc -> Template document which i designed in Word

    dd.xls -> Excel sheet as a database

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, January 23, 2013 2:38 AM
  • User-489377026 posted

    thank you very much for sharing your solution, if you have any problem in future programming, you're welcome to post here.

    Best Regards

    Wednesday, January 23, 2013 3:30 AM
  • User1776267435 posted

    Yes anytime...

    Wednesday, January 23, 2013 3:59 AM
  • User626389161 posted

    In your Code,Data Source Name (D:\\dd.xls) is mentioned. But qry returns selected rows.Please make your xls as Meaning ful data. And if i move this from one server to another server how can i open it without touch odc. I need configurable ODC

    Thursday, April 25, 2013 6:46 AM