Click here to Skip to main content
15,887,485 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
MIDL
wrdMailMerge = wrdDoc.MailMerge;
            CreateMailMergeDataFile();
              wrdMailMerge.Destination = Microsoft.Office.Interop.Word.WdMailMergeDestination.wdSendToNewDocument;
        wrdMailMerge.Execute(ref oFalse);

        object wdFormat = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatRTF;
        Object fileName = "D:\\test.doc";
        wrdDoc.SaveAs(ref fileName, 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);
        
        wrdDoc.Saved = true;
        wrdDoc.Close(ref oFalse, ref oMissing, ref oMissing);

       
        
        wrdMailMerge = null;
      
        wrdDoc = null;
        wrdApp = null;
 private void CreateMailMergeDataFile()
    {
         Microsoft.Office.Interop.Word._Document oDataDoc;
        
        
        Object oName = "D:\\DataDoc.doc";
        Object oHeader = "FirstName, LastName, Address, CityStateZip";
        wrdDoc.MailMerge.CreateDataSource(ref oName, ref oMissing,
            ref oMissing, ref oHeader, ref oMissing, ref oMissing,
            ref oMissing, ref oMissing, ref oMissing);

        // Open the file to insert data.
        
        oDataDoc = wrdApp.Documents.Open(ref oName, 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);
      
      
        con.Open();
        SqlCommand cmd = new SqlCommand("select * from tblMailMerge", con);
        SqlDataReader dr1;
        dr1 = cmd.ExecuteReader();
        int nR = 2;
        while (dr1.Read())
        {
            FillRow(oDataDoc, nR++, dr1.GetString(1), dr1.GetString(2), dr1.GetString(3), dr1.GetString(4));
            oDataDoc.Tables[1].Rows.Add(ref oMissing);
           // FillRow(oDataDoc, nR++, dr[1].ToString(), dr[2].ToString(), dr[3].ToString(), dr[4].ToString());
        }
       // con.Close();
        dr1.Close();
        oDataDoc.Tables[1].Rows.Last.Delete();
        //--------------------
       
        
        oDataDoc.Save();
        oDataDoc.Close(ref oFalse, ref oMissing, ref oMissing);

        //throw new Exception("The method or operation is not implemented.");
    }

    private void FillRow(_Document oDoc, int Row, string p_3, string p_4, string p_5, string p_6)
    {
        // Insert the data into the specific cell.
        oDoc.Tables[1].Cell(Row, 1).Range.InsertAfter(p_3);
        oDoc.Tables[1].Cell(Row, 2).Range.InsertAfter(p_4);
        oDoc.Tables[1].Cell(Row, 3).Range.InsertAfter(p_5);
        oDoc.Tables[1].Cell(Row, 4).Range.InsertAfter(p_6);
         implemented.");
    }
       //throw new Exception("The method or operation is not implemented.");
    
}-----------------------


HOW TO SAVE THESE MERGED FILE.....pls Help......
Posted
Updated 11-Aug-10 21:18pm
v3
Comments
LittleYellowBird 12-Aug-10 3:14am    
Hi, you need to explain your problem better to give people a chance of helping you. :)

1 solution

Your code look somewhat sloppy using untyped and completely unused variables.
VB
object wdFormat = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatRTF;
Object fileName = "D:\\test.doc";


Furthermore you open and close the document in CreateMailMergeDataFile and then open another document and simply use some methods relating mail merge. But how should it know what documents you want to merge? This won't simply be remembered. You have to specify both. Look into msdn for Word interop and how to perform a mailmerge. Sure there is an example of it.

Good luck!
 
Share this answer
 
Comments
NITHIN INDIA 12-Aug-10 5:49am    
pls sent an Example
E.F. Nijboer 12-Aug-10 6:26am    
http://msdn.microsoft.com/en-us/library/aa140183%28office.10%29.aspx
http://msdn.microsoft.com/en-us/library/aa140197%28office.10%29.aspx
http://support.microsoft.com/kb/301659

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900