wordpress hit counter
Mail merge in WordProcessingML using System.IO.Packaging API - OpenXML Developer - Blog - OpenXML Developer
Goodbye and Hello

OpenXmlDeveloper.org is Shutting Down

There is a time for all good things to come to an end, and the time has come to shut down OpenXmlDeveloper.org.

Screen-casts and blog posts: Content on OpenXmlDeveloper.org will be moving to EricWhite.com.

Forums: We are moving the forums to EricWhite.com and StackOverflow.com. Please do not post in the forums on OpenXmlDeveloper.org. Instead, please post in the forums at EricWhite.com or at StackOverflow.com.

Please see this blog post for more information about my plans moving forward.  Cheers, Eric

Mail merge in WordProcessingML using System.IO.Packaging API

Mail merge in WordProcessingML using System.IO.Packaging API

Rate This
  • Comments 2

Article by Sheela E.N, Sonata Software Limited


This article explains the process of adding mail merge in WordProcessingML using System.IO.Packaging API in C# project.


1.    Create a C# project and a reference to WindowsBase.dll located at C:\Program Files\Reference Assemblies\Microsoft\Framework\v3.0 (This is part of .Net 3.0)


2.    Import the namespace to refer to the dll

using System.IO.Packaging


3.    Create main document part of the WordProcessingML document using the packaging API


pkgOutputDoc = Package.Open(Filename, FileMode.Create, FileAccess.ReadWrite);


4.    Create relationship to the main document part


Uri uri = new Uri("/word/document.xml", UriKind.Relative);

PackagePart partDocumentXML = pkgOutputDoc.CreatePart(uri, "application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml");


5.    Either construct the xml file for the main document part or load the existing file. Here, I am loading the existing file


StreamWriter streamStartPart = new StreamWriter(partDocumentXML.GetStream(FileMode.Create, FileAccess.Write));

              XmlDocument xdoc = new XmlDocument();






6.      Create the relationship to the main document part


pkgOutputDoc.CreateRelationship(uri, TargetMode.Internal, "http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument", "rId1");



7.    Create document setting part


Uri uriSetting = new Uri("/word/settings.xml", UriKind.Relative);


string settingContentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.settings+xml";

string relationshipType = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/settings";


PackagePart settingPart = pkgOutputDoc.CreatePart(uriSetting,  settingContentType); 


8.      Either construct the xml file for the document setting  part or load the existing file. Here, I am loading the existing file





9.    Create relationship to this part from the main document part


 Uri relativeSettingUri =

            PackUriHelper.GetRelativeUri(uri, uriSetting);


partDocumentXML.CreateRelationship(relativeSettingUri, TargetMode.Internal, "http://schemas.openxmlformats.org/officeDocument/2006/relationships/settings", "rId2");


10. Create recipientData part  (this part is optional)


  Uri receipt = new Uri("/word/recipientData.xml", UriKind.Relative);

  PackagePart receipData = pkgOutputDoc.CreatePart(receipt, "application/vnd.ms-word.mailMergeRecipientData+xml");



11. Either construct the xml file for the recipientData part or load the existing file. Here, I am loading the existing file






This part holds the information about which are all the records to be included/excluded for the mail merge. If this part is not present, all the selected records from the data source will be included for the mail merge.


12. Create a URI which links to the external data source


Uri uri1 = new Uri("file:///.../MailMerge/MailMerge.mdb");


13. Create a relationship for internal recipientData part and the external data source to document setting part


settingPart.CreateRelationship(uri1, TargetMode.External, "http://schemas.openxmlformats.org/officeDocument/2006/relationships/mailMergeSource", "rId1");


       Uri uri2 = new Uri("/word/recipientData.xml", UriKind.Relative);


                 Uri relativeDataUri =PackUriHelper.GetRelativeUri(uri, uri2);




settingPart.CreateRelationship(relativeDataUri, TargetMode.Internal, "http://schemas.openxmlformats.org/officeDocument/2006/relationships/recipientData", "rId3");


14. Flush the package content and close the handler






This is a simple demo which demonstrates use of system.IO.Packaging API for adding mail merge to word document. The example demo is attached with the article as a zip file


PS: After extracting attached file, change the URI (step 12) pointing to the external database.






Attachment: MailMerge.zip
  • I open my docx file to the Mail Merge, but my field is not merge. What use
    parameters to be automatically ***  document as an example MailMerge.zip? To see merge filed's I must clik http://img35.imageshack.us/img35/7109/29078302.jpg

  • I ran your program. but when I want to open the file which was created with the merge data. word says the file cannot be opened. something with the word/settings.xml file.

    What the problem?

Page 1 of 1 (2 items)