wordpress hit counter
Mail Merge in WordProcessingML - OpenXML Developer - Blog - OpenXML Developer

Mail Merge in WordProcessingML

Blog

Samples, Demos, and Reference Articles

Mail Merge in WordProcessingML

  • Comments 6

 

Article by Sheela E.N, Sonata Software Limited

 

This article briefly explains the concept of Mail Merge and how this information is stored in WordProcessingML.

 

A mail merge is a word processing feature that allows in creating common letters, mailing labels, envelopes, or cataloging documents to a group of people as stored in a database.

 

We use mail merge when we want to create a set of documents that are essentially in a template format but only in a single place contains unique information. For example, in a letter that announces a new product to all its customers, the company logo and the text about the product is common and will appear in each letter, while the customer address and greeting line will be different in each letter.

 

The two key parts of the mail merge process are

1.    Connecting to an external data source

2.    Populating mail merge fields with external data

 

The connection details of the external data source are mentioned in the settings.xml file as

 

<w:settings xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:sl=http://schemas.openxmlformats.org/schemaLibrary/2006/main …..   ….. … >

    <w:mainDocumentType w:val="envelopes"/>

    <w:linkToQuery/>

    <w:dataType w:val="native"/> 

    <w:query w:val="SELECT * FROM `test` "/>

    <w:dataSource r:id="rId1"/>

    <w:viewMergedData/>

    <w:activeRecord w:val="4"/>

    <w:odso>

      <w:udl w:val="Provider=Microsoft.ACE.OLEDB.12.0;User ID=Admin;Data Source=D:\....\....\....\MailMerge\MailMerge.mdb;Mode=Read;Extended Properties=&quot;&quot;;Jet OLEDB:System database=&quot;&quot;;Jet OLEDB:Registry Path=&quot;&quot;;Jet OLEDB:Engine Type=5;Jet OLEDB:Database Locking Mode=0;Jet OLEDB:Global Partial Bulk Ops=2;Jet OLEDB:Global Bulk Transactions=1;Jet OLEDB:New Database Password=&quot;&quot;;Jet OLEDB:Create System Database=False;Jet OLEDB:Encrypt Database=False;Jet OLEDB:Don't Copy Locale on Compact=False;Jet OLEDB:Compact Without Replica Repair=False;Jet OLEDB:SFP=False;Jet OLEDB:Support Complex Data=False"/>

      <w:table w:val="test"/>

      <w:src r:id="rId1"/>

      <w:colDelim w:val="9"/>

      <w:type w:val="database"/>

      <w:fHdr/>

      <w:recipientData r:id="rId3"/>

    </w:odso>

  </w:mailMerge>

    

……

….  

</w:settings>

 

The   <w:mailMerge> element specifies the following information of the hosting application to connect to an external data source –

1.    w:mainDocumentType specifies the type of the mail merge. The possible types are

a.    formLetters.

b.    email

c.    envelopes

d.    fax

e.    mailingLabels

f.     catalog

2.    w:queryspecifies the table to be queried

3.    w:dataSource — specifies the link to the external data source. The details are stored in document setting part relationship.

4.    w:viewMergedData — specifies that the merged document shall display the data from the specified external data source where merge fields have been inserted.

5.    w:activeRecord — specifies which record within the data source should be displayed.

6.    w:odso — The Office Data Source Object (odso) settings is optional details which specifies a group of additional settings for the mail merge information which comprises an extension to the standard settings stored with a mail merge.

 

The Office Data Source Object Settings also contains a link to another part (mailMergeRecipientData) which contains details about all of the inclusion/exclusion data for the contents of the specified mail merge data source and is represented as

 

  <w:recipientData r:id="rId3"/>

 

The details of the record inclusion/exclusion in mail merge recipient data part are stored as

 

<w:recipients  xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" … …. …. >

 

  <w:recipientData>

    <w:active w:val="0"/>

    <w:column w:val="2"/>

    <w:uniqueTag w:val="MQ="/>

  </w:recipientData>

 

  <w:recipientData>

    <w:active w:val="0"/>

    <w:column w:val="2"/>

    <w:uniqueTag w:val="Mq="/>

  </w:recipientData>

….

….

</w:recipients>

 

The <w:recipientData> element specifies the following details for the inclusion/exclusion of records within mail merged document.

 

1.     w:column specifies the column on which the filter has applied based on the value of the column (like where clause on that column)

 

2.     w:uniqueTag — specifies the contents of a given record within the specified external data source, in the column containing unique data for every record within the external data source for inclusion/exclusion of records for mail merge.  The value specifies that this should be the base64-encoded value of the unique tag value as specified by the data source. This element is used along with <w:column> element.

 

3.     w:active — specifies whether the record to be shown on not. The default value is 1 which indicates the record is included for the mail merge

 

If this part is not present, it indicates that all the records within the data source are included for the mail merge.

 

In the main document part (document.xml by default), the merge fields are populated by extracting data from external data source and the same is represented as

 

<w:body>

….

….

<w:fldSimple w:instr="MERGEFIELD FirstName">

<w:r>

<w:rPr>

        <w:noProof />

  </w:rPr>

       <w:t>«FirstName»</w:t>

  </w:r>

  </w:fldSimple>

   </w:body>

 

 

In the above XML snippet, the merge fieldFirstName’ is inserted in a WordprocessingML document which connects to the external data source. When the mail merge takes place, the data from the FirstName column will be populated into the field FirstName (<w:fldSimple w:instr="MERGEFIELD FirstName">) within the merged WordprocessingML document.

 

The word document containing mail merge field place holders is illustrated as

 

 

 

  

When the mail merge takes place, the data will be generated and displayed as

 

 

 

 

 

 

In my next article, I will explain how to apply mail merge to WordProcessingML programmatically using System.IO.Packaging API.

  • FYI, I'm not seeing any pictures (just placeholders) in your "Mail Merge in WordProcessingML" article, only [red-x] placeholders (IE6, IE7 and Firefox).
  • I can't see the pictures either.
    Please check where's the problem.
    Thanks~
  • Is it possible to Fill 2 or more paragraph data into a MergeField. I tried putting a <w:p/> tag into the text data in the <w:t> of the MergeField. In this case there was a paragraph but it was in a different font from other the content in the MergeField.
  • Is it possible to Fill  <w:t> of the MergeField with Tabular data. Like we want to insert a pricelist into a  MergeField
  • In the code Parse the text for "\n".
    For Each newline text insert the followed by <w:t> the exits text data </w:t><w:p/>

    The Preceding font tags must be placed before Each <w:t> so that a similar font appear for he newly created para text as well.
  • Is there any way that this article could be updated to show how to do this programatically (C#) using the new Open XML SDK with the addresses coming from an XML file that is contained in the package?
Page 1 of 1 (6 items)