Using XML and Word for Mail-Merging

Many thanks to Franky Braem for allowing us to host this article and code from his original article.

This project is a sample of how you can merge XML-files with a MS Word97 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>

This is an example of a Word-template:



Click here for a larger image.

Properties

  • 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

Methods

  • Execute
    • Returns - Boolean
    • Parameters - none
    • Performs the mailmerge. Returns False when an error occurred.

(Ed: Again, many thanks to Franky Braem for allowing us to host this code, be sure to check out his original article for any updates.)

Download Zipped Files (project/wordfiles) 21k



Comments

  • VB with Word Document

    Posted by Legacy on 11/26/2001 12:00am

    Originally posted by: vijayaragavan

    Sir,

    My problem is to link VB to word Document.
    Then Fetch first word(english), it equlates the tamil word, if tamil word found, then replace english word, placed the tamil word with sulogini font.

    It is like English to Tamil Translation.

    Thanking U Sir.

    By
    vijayaragavan

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: May 6, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT While you likely have very good reasons for remaining on WinXP after end of support -- an estimated 20-30% of worldwide devices still are -- the bottom line is your security risk is now significant. In the absence of security patches, attackers will certainly turn their attention to this new opportunity. Join Lumension Vice President Paul Zimski in this one-hour webcast to discuss risk and, more importantly, 5 pragmatic risk mitigation techniques …

  • On-demand Event Event Date: March 27, 2014 Teams need to deliver quality software faster and need integrated agile planning, task tracking, source control, auto deploy with continuous builds and a configurable process to adapt to the way you work. Rational Team Concert and DevOps Services (JazzHub) have everything you need to build great software, integrated seamlessly together right out of the box or available immediately in the cloud. And with the Rational Team Concert Client, you can connect your …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds