Click here to Skip to main content
15,888,521 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am working on .net MVC platform. I have created a word template(.dotx) and added mergefields in it. I am replacing merge field from database column values. Now i want to loop on merge field. for example :- i have merge fields named < DepartmentID > and < DepartmentName > . And i am getting data from database's table named "tblDepartment" which have two columns "DepID" and "DepName". this table have 5 rows. i want to loop on these mergefields.

i want to show file like this. before execute the docx file is :- < DepartmentID > < DepartmentName >

after code execute:-
C#
DepID             DepName
  1                 DotNet

  2                 Java

  3                 PHP


What I have tried:

here ids my Code:
C#
public void Mail_Merge()
       {
           try
           {
               Application wordApp = new Application();

               Document doc = new Document();
               Object oMissing = System.Reflection.Missing.Value;

               Object oTemplatePath = "D:\\Letter_Template.dotx";
               for (int i = 0; i < 3; i++)
               {
                   Document wordDoc = new Document();

                   wordDoc = wordApp.Documents.Add(ref oTemplatePath, ref oMissing, ref oMissing, ref oMissing);

                   foreach (Field myMergeField in wordDoc.Fields)
                   {

                       Range rngFieldCode = myMergeField.Code;
                       String fieldText = rngFieldCode.Text;


                       // ONLY GETTING THE MAILMERGE FIELDS

                       if (fieldText.StartsWith(" MERGEFIELD"))
                       {

                           // THE TEXT COMES IN THE FORMAT OF

                           // MERGEFIELD  MyFieldName  \\* MERGEFORMAT

                           // THIS HAS TO BE EDITED TO GET ONLY THE FIELDNAME "MyFieldName"

                           Int32 endMerge = fieldText.IndexOf("\\");

                           Int32 fieldNameLength = fieldText.Length - endMerge;

                           String fieldName = fieldText.Substring(11, endMerge - 11);

                           // GIVES THE FIELDNAMES AS THE USER HAD ENTERED IN .dot FILE

                           fieldName = fieldName.Trim();

                           // **** FIELD REPLACEMENT IMPLEMENTATION GOES HERE ****//

                           // THE PROGRAMMER CAN HAVE HIS OWN IMPLEMENTATIONS HERE
                           if (fieldName == "Address")
                           {
                               if (i == 0)
                               {
                                   myMergeField.Select();
                                   wordApp.Selection.TypeText("Ram Debey");
                               }
                               else if (i == 1)
                               {
                                   myMergeField.Select();
                                   wordApp.Selection.TypeText("Mukesh Mahapatra");
                               }
                               else
                               {
                                   myMergeField.Select();
                                   wordApp.Selection.TypeText("Inamul Afridi");
                               }

                           }
                       }
                   }
               }
               wordApp.ActiveDocument.SaveAs("D:\\MergeLetter.doc", ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
               ref oMissing, ref oMissing, ref oMissing, ref oMissing);
               wordApp.ActiveDocument.Close(ref oMissing, ref oMissing, ref oMissing);
               // wordApp.Documents.Open("myFile.doc");
               wordApp.Application.Quit();
           }
           catch(Exception ex)
           {

           }
       }
Posted
Updated 29-Jun-16 1:16am
v4
Comments
Richard Deeming 29-Jun-16 10:01am    
You should read the following Microsoft knowledgebase article:

Considerations for server-side Automation of Office[^]
Microsoft does not currently recommend, and does not support, Automation of Microsoft Office applications from any unattended, non-interactive client application or component (including ASP, ASP.NET, DCOM, and NT Services), because Office may exhibit unstable behavior and/or deadlock when Office is run in this environment.
Jewel-Davis 1-Jul-16 7:10am    
I would recommend you to check out these mail merge examples done in .NET. The examples use a .NET library for Word documents that is safe to be use on server side as well.

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