locked
Populate data into word templates ??? RRS feed

  • Question

  • User391606836 posted

    Hi

    I have created a word template.I get the data from database and store it into an xml file. How can i map the word templates feilds with the data from XML.I mean how to populate data into the word template from XML or from dataset. Please respond asap.

     Thanks and Regards

    Friday, October 10, 2008 12:08 AM

All replies

  • User-611445092 posted

    Hi,

        If you are using Office 2007, then you can look into the WORD XML Formats. Following article contains more inof on the same:

    http://msdn.microsoft.com/en-us/library/bb266220.aspx#office2007wordfileformat_separatingcontentfromthedocument

    Or you have to use Word APIs in .Net to place the data into the word application from each element of the xml.

    Friday, October 10, 2008 3:21 AM
  • User391606836 posted
    Iam using office 2003....can u pls present a sample code ????
    Friday, October 10, 2008 4:47 AM
  • User-611445092 posted

    Hi,

        While creating the word template where ever you want to place the value mark it as a bookmarks.

    Using .Net code we can read the bookmarks and replace these bookmarks with the actual values from the xml elements.

    Following article examples on how read bookmarks and replacing with the actual values:

    http://support.microsoft.com/kb/316384

    Friday, October 10, 2008 7:16 AM
  • User391606836 posted
    can u pls say how to insert bookmarks????
    Monday, October 13, 2008 7:51 AM
  • User-611445092 posted

    Hi,

        You can use following article for adding Bookmarks in MS Word 2003:

    http://www.ehow.com/how_4531007_set-bookmarks-be-viewed-microsoft.html

    Monday, October 13, 2008 9:31 AM
  • User391606836 posted

    What i have done is in my word template i have marked the location where data needs to be inserted from XML as merge feilds.

    Below is the code i have written.

    private void button1_Click(object sender, EventArgs e)

    {

    //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();

     

     

    //SETTING THE VISIBILITY TO TRUE

    oWord.Visible = true;

     

     

    //THE LOCATION OF THE TEMPLATE FILE ON THE MACHINE

    Object oTemplatePath = "C:\\Name2.dot";

     

     

    //ADDING A NEW DOCUMENT FROM A TEMPLATE

    oWordDoc = oWord.Documents.Add(ref oTemplatePath, ref oMissing, ref oMissing, ref oMissing);int iTotalFields;

    iTotalFields = oWordDoc.Fields.Count;

    foreach (Word.Field myMergeField in oWordDoc.Fields)

    {

    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 .dot FILE

    fieldName = fieldName.Trim();

     

     

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

    // THE PROGRAMMER CAN HAVE HIS OWN IMPLEMENTATIONS HERE

    if (fieldName == "CIFLNAME")

    {

    myMergeField.Select();

    oWord.Selection.TypeText(
    "This Text Replaces the Field in the Template");

    }

    }

    }

    In The above sample(The last line) just types the text at runtime.

    Instead of this "This text replace....." i need to map the particular xml node.How can i do that??????

     

     

    Monday, October 13, 2008 9:39 AM
  • User391606836 posted

    What i have done is in my word template i have marked the location where data needs to be inserted from XML as merge feilds.

    Below is the code i have written.

    private void button1_Click(object sender, EventArgs e)

    {

    //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();

     

     

    //SETTING THE VISIBILITY TO TRUE

    oWord.Visible = true;

     

     

    //THE LOCATION OF THE TEMPLATE FILE ON THE MACHINE

    Object oTemplatePath = "C:\\Name2.dot";

     

     

    //ADDING A NEW DOCUMENT FROM A TEMPLATE

    oWordDoc = oWord.Documents.Add(ref oTemplatePath, ref oMissing, ref oMissing, ref oMissing);int iTotalFields;

    iTotalFields = oWordDoc.Fields.Count;

    foreach (Word.Field myMergeField in oWordDoc.Fields)

    {

    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 .dot FILE

    fieldName = fieldName.Trim();

     

     

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

    // THE PROGRAMMER CAN HAVE HIS OWN IMPLEMENTATIONS HERE

    if (fieldName == "CIFLNAME")

    {

    myMergeField.Select();

    oWord.Selection.TypeText(
    "This Text Replaces the Field in the Template");

    }

    }

    }

    In The above sample(The last line) just types the text at runtime.

    Instead of this "This text replace....." i need to map the particular xml node.How can i do that??????

     

     

    Monday, October 13, 2008 9:39 AM
  • User391606836 posted

    What i have done is in my word template i have marked the location where data needs to be inserted from XML as merge feilds.

    Below is the code i have written.

    private void button1_Click(object sender, EventArgs e)

    {

    //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();

     

     

    //SETTING THE VISIBILITY TO TRUE

    oWord.Visible = true;

     

     

    //THE LOCATION OF THE TEMPLATE FILE ON THE MACHINE

    Object oTemplatePath = "C:\\Name2.dot";

     

     

    //ADDING A NEW DOCUMENT FROM A TEMPLATE

    oWordDoc = oWord.Documents.Add(ref oTemplatePath, ref oMissing, ref oMissing, ref oMissing);int iTotalFields;

    iTotalFields = oWordDoc.Fields.Count;

    foreach (Word.Field myMergeField in oWordDoc.Fields)

    {

    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 .dot FILE

    fieldName = fieldName.Trim();

     

     

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

    // THE PROGRAMMER CAN HAVE HIS OWN IMPLEMENTATIONS HERE

    if (fieldName == "CIFLNAME")

    {

    myMergeField.Select();

    oWord.Selection.TypeText(
    "This Text Replaces the Field in the Template");

    }

    }

    }

    In The above sample(The last line) just types the text at runtime.

    Instead of this "This text replace....." i need to map the particular xml node.How can i do that??????

     

     

    Monday, October 13, 2008 9:39 AM
  • User-611445092 posted

    Hi,

        You can use XmlDocument class to read the data from xml. Use SelectNodes or Select SingleNode to extract the data which will be printed in word.

    Following url explains on how to read data using XmlDocument Class:

    http://www.c-sharpcorner.com/xml/ReadingXmlUsingXmlDocument.asp 

    Monday, October 13, 2008 9:54 AM
  • User-1038969477 posted

    Hi bklight ,

    So you have got one template doc file. You can fill that template file with some dummy data. Then Save As dialog--right under "Word Document (*.doc)"--is "XML Document (*.xml)". Save it as xml file. So you get the template xml file.

    And you can get the datasource , then , things get easy, all what you need to do is to replace the xml node's inner text or attribute to the real value it should be from datasource.

    ksridharbabuus has said you can use XmlDocument class to read the data from xml , create one xmlnode , change values.

     

    Tuesday, October 14, 2008 4:47 AM
  • User391606836 posted

    What i had done is i saved the word as "document template" ie .dot extension.

    Can it be done like that or not????which is the correct way????

    Tuesday, October 14, 2008 5:21 AM
  • User-611445092 posted

     HI,

       You can go either of the approaches. If you want to use xml approach follow the thread give by Samu Zhang
     

    Friday, October 17, 2008 4:03 AM