Mail Merge c#

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Hi, im doing a program which read data from a xml, that xml contains tags and values, and with that tags i wanna replace them in a .dot file, generating a .doc file... My xml data and my .dot template could change... So i need something dynamic...
I already accomplish that, by doing this:
But i think is not the best solution.. If someone could help me, with a more direct way....
Sorry for the bad English, and if the post is in wrong place..

c# code:
<div style="overflow:auto;background-color:white;line-height:100% ! important;font-family:Courier New;font-size:11px <table style="border-width:0px;margin:2px 0px;width:99%;border-collapse:collapse;background-color:rgb(255, 255, 255)" cellpadding=0 cellspacing=0><col style="font-family:Courier New;font-size:11px;padding-left:10px;white-space:nowrap <tbody><tr><td><font style="font-size:11px  private void btnWord_Click(object sender, EventArgs e) // Cals the CCWord class, wich performs the changes, and all data
        {
            string xml = "dados.xml";
            object template = Environment.CurrentDirectory + @"template.dot";
            CCWordApp test;
            test = new CCWordApp();            
            test.Open(template);
            Hashtable xmlDados = test.GetValuesXML(xml);
            IDictionaryEnumerator enumerator = xmlDados.GetEnumerator();
            while (enumerator.MoveNext())
            {
                test.MergeFiles(enumerator.Key.ToString(),enumerator.Value.ToString());
               
            }
            test.Save();
            test.Quit();
}

CCWordApp class...
public void Open(object template) {...} //Opens Word Aplication and a new document, in agreement with .dot template
public Hashtable GetValuesXML(string nome){...} //Reads data from a XML, and pass it to a HashTable
public void MergeFiles(string tag, string valor )//Receives the key and the value from the hashtable and replaces it in the document .dot
    {
        foreach (Word.Field wf in oWordDoc.Fields)
        {
            Word.Range rngFieldCode = wf.Code;
            String fieldText = rngFieldCode.Text;
            if (fieldText.StartsWith(tag))
            {
                wf.Select();
                oWordApp.Selection.TypeText(valor);               
            }               
        }
    }
 public void Quit() {..}
 public void Save() {..}

</font></td></tr></tbody></table>
an example of my xml (My tags are the elements into Employee, and the text to replace with is the elements text) :

<div style="overflow:auto;background-color:white;line-height:100% ! important;font-family:courier new;font-size:11px
<table style="border-width:0px;margin:2px 0px;border-collapse:collapse;background-color:rgb(255, 255, 255)" cellpadding=0 cellspacing=0 width=864 height=108>
<col style="font-family:courier new;font-size:11px;padding-left:10px;white-space:nowrap
<tbody>
<tr>
<td><font style="font-size:11px <?xml version="1.0" encoding="iso-8859-1"?>
  <rootnode xmlns:od="urn:schemas-microsoft-com:officedata
    <Employee>
      <LastName>Davolio</LastName>
      <FirstName>Nancy</FirstName>
      <Address>507 - 20th Ave. E. Apt. 2A</Address>
      <City>Seattle</City>
      <Region>WA</Region>
      <PostalCode>98122</PostalCode>
    </Employee>
</rootnode>
</font><font style="font-size:11px </font></td>
</tr>
</tbody>
</table>

<hr class=sig>César

View the full article
 
Back
Top