Click here to Skip to main content
15,891,136 members
Articles / Productivity Apps and Services / Microsoft Office

Hiding and Storing/caching of Application Specific data in MS Word 2003 documents

Rate me:
Please Sign up or sign in to vote.
3.54/5 (8 votes)
9 Oct 2008CPOL8 min read 29.1K   512   18  
Proof of concept on how the application specific (small/large amount of) data can be stored in ms word document as well as how it can be made hidden from end user’s eye.
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Office.Core;
using System.Reflection;

namespace WordAddIn1
{
    public class CustomDocumentPropertyManager
    {
        public string GetPropertyValue(string propertyName,ref Microsoft.Office.Interop.Word.Document Doc)
        {
            string propertyValue = String.Empty;
            Type validatedType = null;
            object oDocCustomProps = null;
            Type typeDocCustomProps = null;
            string prop = String.Empty;
            object property = null;
            try
            {
                oDocCustomProps = Doc.CustomDocumentProperties;
                typeDocCustomProps = oDocCustomProps.GetType();
                prop = propertyName; //property to extract

                property = typeDocCustomProps.InvokeMember("Item",
                                                    System.Reflection.BindingFlags.Default |
                                                    System.Reflection.BindingFlags.GetProperty,
                                                    null, oDocCustomProps, new object[] { prop });

                validatedType = property.GetType();
                //to check the value of custom document property
                propertyValue = validatedType.InvokeMember("Value",
                    System.Reflection.BindingFlags.Default |
                    System.Reflection.BindingFlags.GetProperty,
                    null,
                    property,
                    new object[] { }).ToString();

            }
            catch (System.Reflection.TargetInvocationException ex)
            {
                propertyValue = String.Empty;
            }
            catch (Exception ex)
            {
                propertyValue = String.Empty;
            }
            return propertyValue;

        }

        public bool AddCustomDocumentProperty(string propertyName, string propertyValue,ref Microsoft.Office.Interop.Word.Document Doc)
        {
            bool success = false;
            Type validatedType = null;
            object oDocCustomProps = null;
            Type typeDocCustomProps = null;
            string prop = String.Empty;
            object property = null;
            try
            {
                oDocCustomProps = Doc.CustomDocumentProperties;
                typeDocCustomProps = oDocCustomProps.GetType();
                prop = propertyName;

                property = typeDocCustomProps.InvokeMember("Item",
                                                    System.Reflection.BindingFlags.Default |
                                                    System.Reflection.BindingFlags.GetProperty,
                                                    null, oDocCustomProps, new object[] { prop });


            }
            catch (System.Reflection.TargetInvocationException ex)
            {
                oDocCustomProps = Doc.CustomDocumentProperties;
                typeDocCustomProps = oDocCustomProps.GetType();

                string strIndex = propertyName;
                string strValue = propertyValue;
                object[] oArgs = {strIndex,false,
                                                 MsoDocProperties.msoPropertyTypeString,
                                                 strValue};

                typeDocCustomProps.InvokeMember("Add", BindingFlags.Default |
                                           BindingFlags.InvokeMethod, null,
                                           oDocCustomProps, oArgs);

                success = true;


            }
            catch (Exception ex)
            {
                success = false;
            }
            return success;
        }

        public bool ChangePropertyValue(string propertyName, string propertyValue,ref  Microsoft.Office.Interop.Word.Document Doc)
        {
            bool success = false;
            object oDocCustomProps = null;
            Type typeDocCustomProps = null;
            string prop = String.Empty;
            object property = null;

            try
            {
                oDocCustomProps = Doc.CustomDocumentProperties;
                typeDocCustomProps = oDocCustomProps.GetType();
                prop = propertyName;

                property = typeDocCustomProps.InvokeMember("Item",
                                                    System.Reflection.BindingFlags.Default |
                                                    System.Reflection.BindingFlags.GetProperty,
                                                    null, oDocCustomProps, new object[] { prop });

                typeDocCustomProps.InvokeMember("Value",
                                                  System.Reflection.BindingFlags.Default |
                                                  System.Reflection.BindingFlags.SetProperty,
                                                  null,
                                                  property,
                                                  new object[] { propertyValue });

                success = true;
            }
            catch (Exception ex)
            {
                success = false;
            }

            return success;
        }
    }


}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions