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();
xdoc.Load(@"..\..\maildocument.xml");
xdoc.Save(streamStartPart);
streamStartPart.Close();
pkgOutputDoc.Flush();
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
xdoc.Load(@"..\..\settings.xml");
xdoc.Save(settingPart.GetStream());
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
xdoc.Load(@"..\..\recipientData.xml");
xdoc.Save(receipData.GetStream());
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
pkgOutputDoc.Close();
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.
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?