Thursday, 22 August 2013

Word Mail merge in asp.net c# to add several pages and show

Word Mail merge in asp.net c# to add several pages and show

I have created a word mail merge in asp.net C#. I need to replace
MergeField with data from a datatable. The problem is, only the first page
shows correctly, the other pages get added, but with no data as i would
want to it. I want to iterate to the end of datatable rows, generate a
word doc for each row and have all these docs in one word file which will
then be opened. I have attached the entire code. Any help will be greatly
appreciated.
using Word = Microsoft.Office.Interop.Word;
public void GenerateDoc(DataTable dt)
{
string schoolName = "Name 1";
string schoolAddress = "Address 1";
//OBJECT OF MISSING "NULL VALUE"
Object oMissing = System.Reflection.Missing.Value;
//OBJECTS OF FALSE AND TRUE
Object oTrue = true;
Object oFalse = false;
//CREATING OBJECTS OF WORD AND DOCUMENT
Word.Application oWord = new Word.Application();
Word.Document oWordDoc = new Word.Document();
//THE LOCATION OF THE TEMPLATE FILE ON THE MACHINE
string filePath =
Path.Combine(HostingEnvironment.ApplicationPhysicalPath,
@"App_Data\envelope3.dotx");
Object oTemplatePath = filePath;
//ADDING A NEW DOCUMENT FROM A TEMPLATE
oWordDoc = oWord.Documents.Add(ref oTemplatePath, ref oMissing, ref
oMissing, ref oMissing);
for (int i = 0; i < dt.Rows.Count; i++)
{
string studentName = dt.Rows[i]["StudentName"].ToString();
string studentID = dt.Rows[i]["StudentID"].ToString();
string guard = dt.Rows[i]["GuardName"].ToString();
string phone = dt.Rows[i]["Phone"].ToString();
string address = dt.Rows[i]["Address"].ToString();
string className = dt.Rows[i]["ClassName"].ToString();
foreach (Word.Field myMergeField in oWordDoc.Fields)
{
int iTotalFields = 0;
iTotalFields++;
Word.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 .dotx FILE
fieldName = fieldName.Trim();
// **** FIELD REPLACEMENT IMPLEMENTATION GOES HERE ****//
// THE PROGRAMMER CAN HAVE HIS OWN IMPLEMENTATIONS HERE
if (fieldName.Contains("SchoolName"))
{
myMergeField.Select();
oWord.Selection.TypeText(schoolName);
}
else if (fieldName.Contains("SchoolAddr"))
{
myMergeField.Select();
oWord.Selection.TypeText(schoolAddress);
}
else if (fieldName.Contains("StudentName"))
{
myMergeField.Select();
oWord.Selection.TypeText(studentName);
}
else if (fieldName.Contains("StudentID"))
{
myMergeField.Select();
oWord.Selection.TypeText(studentID);
}
else if (fieldName.Contains("GuardName"))
{
myMergeField.Select();
oWord.Selection.TypeText(guard);
}
else if (fieldName.Contains("Phone"))
{
myMergeField.Select();
oWord.Selection.TypeText(phone);
}
else if (fieldName.Contains("Address"))
{
myMergeField.Select();
oWord.Selection.TypeText(address);
}
else if (fieldName.Contains("ClassName"))
{
myMergeField.Select();
oWord.Selection.TypeText(className);
}
}
}
//Add to oWord to the final Word Document here
Word.Selection sel = oWord.Selection;
sel.InsertNewPage();
}
//SETTING THE VISIBILITY TO TRUE
oWord.Visible = true;
}
What i did initially was, i was calling oWord.Visible = true on every
iteration. It was opening a lot of word documents, though correct, it is
not pleasant at all for me. I wish to put all those together into a single
document by adding new pages to oWord. These new pages however do not
contain the mergefields i need to replace. Have searched around for it,
but no results so far. I wish to continue use the Microsoft.Office.Interop

No comments:

Post a Comment