Storing .NET DataSets inside Microsoft Word Document

Since the release of Office 2007, Microsoft has been storing their MS Word and Excel documents in OpenXML format.  OpenXML is basically a compressed, ZIP file format with multiple XML files inside.

If you have never seen this format, an interesting exercise is to rename a Word (docx) or Excel (xlsx) file with the .zip extension and then open it in Windows Explorer or some other file compression tool and browse the contents of the file.

Well, among other things, this format also allows developers some innovative options for working with Office documents.  Traditionally (before OpenXML format), working with Office documents  required going through the COM layer API which required an instance of winword.exe to run in order to access and modify the contents of a Microsoft Word file.

This code block shows how to save an XML document (as a string) inside a word document using COM interop.  This technique is useful to store large datasets inside a word document from code that is running as a Microsoft Office addin.

public static void SetExtendedPropertyValue(string sPropertyName, string sXML)
{
    //store data as CustomXML Part

    //Connect to document properties
    Word.Application pApplication = Globals.ReviewerPlusAddin.Application;
    Word.Document pDoc = pApplication.ActiveDocument;

    //store the XML dataset and the ID into the proprty
    Office.DocumentProperties pProperties = pDoc.CustomDocumentProperties;

    //find the property if it exists and remove the XMLPart - there is no "replace" option
    foreach (Office.DocumentProperty pProp in pProperties)
    {
        if (pProp.Name.IndexOf(sPropertyName) != -1)
        {
            //add the property and value to our list
            string sID = pProp.Value;
            Office.CustomXMLPart pXMLPartOld = pDoc.CustomXMLParts.SelectByID(sID);
            if (pXMLPartOld != null)
            {
                pXMLPartOld.Delete();
                System.Runtime.InteropServices.Marshal.ReleaseComObject(pXMLPartOld);
            }
            pProp.Delete(); //also remove the old ID
            break;
        }
    }

    //re-add the xml here - the original XML was removed, now add what we want into the document
    Office.CustomXMLPart pXMLPart = pDoc.CustomXMLParts.Add(sXML);
    pProperties.Add(sPropertyName, false, Microsoft.Office.Core.MsoDocProperties.msoPropertyTypeString, pXMLPart.Id, Type.Missing);

    System.Runtime.InteropServices.Marshal.ReleaseComObject(pXMLPart);

}

 

 

Posted in Uncategorized | Leave a comment