Microsoft & .NETVisual C#"Mail Merge" System with XML and Microsoft Word

“Mail Merge” System with XML and Microsoft Word

Developer.com content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

This article was contributed by Franky Braem.

XML2Word

This project is a sample of how you can merge XML-files with a MS Word template. This makes it easy to create a mailmerge-system. I’ve created a ActiveX DLL to perform the mailmerge. This makes it very easy to reuse this code in other applications.

An example

When you have a customer-database and you want to write a letter to each of your customers, the only thing you have to do is to create a Word-template and a XML-file with the data of the customers.

This is an example of a XML-file with customer-data:

<?xml version = "1.0" ?>
<!DOCTYPE DOCUMENT [
  <!ELEMENT DOCUMENT (CUSTOMER)*>
  <!ELEMENT CUSTOMER (NAME, ADDRESS, ORDERS)>
  <!ELEMENT NAME (LASTNAME, FIRSTNAME)>
  <!ELEMENT LASTNAME (#PCDATA)>
  <!ELEMENT FIRSTNAME (#PCDATA)>
  <!ELEMENT ADDRESS (STREET, POSTAL, LOCATION)>
  <!ELEMENT STREET (#PCDATA)>
  <!ELEMENT POSTAL (#PCDATA)>
  <!ELEMENT LOCATION (#PCDATA)>
  <!ELEMENT ORDERS (ITEM)*>
  <!ELEMENT ITEM (PRODUCT, PRICE)>
  <!ELEMENT PRODUCT (#PCDATA)>
  <!ELEMENT PRICE (#PCDATA)>
]>

<DOCUMENT>
  <CUSTOMER>
    <NAME>
      <LASTNAME>Braem</LASTNAME>
      <FIRSTNAME>Franky</FIRSTNAME>
    </NAME>
    <ADDRESS>
      <STREET>Jan Van Rijswijcklaan</STREET>
      <POSTAL>2000</POSTAL>
      <LOCATION>Antwerp</LOCATION>
    </ADDRESS>
    <ORDERS>
      <ITEM>
        <PRODUCT>Computer</PRODUCT>
        <PRICE>54.995</PRICE>
      </ITEM>
      <ITEM>
        <PRODUCT>Printer</PRODUCT>
        <PRICE>12.990</PRICE>
      </ITEM>
    </ORDERS>
  </CUSTOMER>

  <CUSTOMER>
      <NAME>
      <LASTNAME>De Smet</LASTNAME>
      <FIRSTNAME>Nathalie</FIRSTNAME>
    </NAME>
    <ADDRESS>
      <STREET>Kruidtuinlaan</STREET>
      <POSTAL>1000</POSTAL>
      <LOCATION>Brussel</LOCATION>
    </ADDRESS>
    <ORDERS>
      <ITEM>
        <PRODUCT>GSM</PRODUCT>
        <PRICE>9.895</PRICE>
      </ITEM>
    </ORDERS>
  </CUSTOMER>
</DOCUMENT>


screen shot

This is an example of a Word-template:

Properties

Property Type Description
DocumentPrefix String The name of each new document is a number. With this property you can specify a prefix to use in the name of document.
Path String The path where the Word documents should be stored
WordTemplate String The name of the Word-template.
XMLFile String The name of the XMLFile.
XMLString String A string with the XML-data.

Methodes

Methode Returns Parameters Description
Execute Boolean / Performs the mailmerge. Returns False when an error occurred.

Downloads

Download demo project – 10 Kb

Download source – 14Kb

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Latest Posts

Related Stories