Click here to Skip to main content
15,893,508 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.Windows.Forms;
using Microsoft.VisualStudio.Tools.Applications.Runtime;
using Word = Microsoft.Office.Interop.Word;
using Office = Microsoft.Office.Core;
using System.Collections.Specialized;
using System.Threading;
using System.Collections;
using System.IO;
using Microsoft.Office.Core;
using System.Reflection;
using System.Diagnostics;
using Common;

namespace WordAddIn1
{
    public partial class ThisAddIn
    {
        public const int STRINGDATA_DOCUMENTID = 100;
        private const int MAX_TRY = 8;
        private static Mutex threadCountMutex = new Mutex(false);
        private static int threadCount = 0;
 
        Word.Application wb;

        private void ThisAddIn_Startup(object sender, System.EventArgs e)
        {
            wb = this.Application;
            wb.DocumentBeforeSave += new Microsoft.Office.Interop.Word.ApplicationEvents4_DocumentBeforeSaveEventHandler(wb_DocumentBeforeSave);
            wb.DocumentBeforeClose += new Microsoft.Office.Interop.Word.ApplicationEvents4_DocumentBeforeCloseEventHandler(wb_DocumentBeforeClose);
            wb.DocumentOpen += new Microsoft.Office.Interop.Word.ApplicationEvents4_DocumentOpenEventHandler(wb_DocumentOpen);
        }

        void wb_DocumentBeforeSave(Microsoft.Office.Interop.Word.Document Doc, ref bool SaveAsUI, ref bool Cancel)
        {
            if (SaveAsUI)
            {
                string documentID = String.Empty;
                FileOperation fileoperation = new FileOperation();
                Boolean documentIDExists = fileoperation.ReadData(Doc.FullName, STRINGDATA_DOCUMENTID, ref documentID, 256);
                if (documentIDExists == false || documentID == String.Empty || documentID == null)
                {
                    string uniqueID = String.Empty;
                    CustomDocumentPropertyManager docProp = new CustomDocumentPropertyManager();
                    uniqueID = docProp.GetPropertyValue(@"Temp", ref Doc);
                    if (uniqueID != null && uniqueID != String.Empty)
                    {
                        DocumentInfoHandler documentInfohandler = new DocumentInfoHandler();
                        Common.Message docInfo = documentInfohandler.ReadDocumentInfo(uniqueID);
                        if (docInfo != null && docInfo.documentID != null && docInfo.fileName != null && docInfo.documentID != String.Empty && docInfo.fileName != String.Empty)
                        {
                            docInfo.uniqueID = String.Empty;
                            Serialization serializeObj = new Serialization();
                            byte[] requestData = null;
                            bool retVal = serializeObj.Serialize(ref docInfo, ref requestData);
                            if (retVal == true)
                            {
                                ClientApplication Client = new ClientApplication();
                                retVal = Client.StartClient();
                                if (retVal == true)
                                {
                                    try
                                    {
                                        Client.connectDone.WaitOne();
                                        Thread.Sleep(50);
                                        Client.Send(ref requestData);
                                        Client.sendDone.WaitOne();
                                        Client.CloseSocket();
                                    }
                                    catch (Exception ex)
                                    {
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }



        void wb_DocumentOpen(Microsoft.Office.Interop.Word.Document Doc)
        {
       
            string documentID = String.Empty;
            FileOperation fileoperation = new FileOperation();
            Boolean documentIDExists = fileoperation.ReadData(Doc.FullName, STRINGDATA_DOCUMENTID, ref documentID, 256);

            if (documentIDExists && documentID != null && documentID != String.Empty)
            {
                string uniqueFileId = DateTime.Now.Ticks.ToString();
                CustomDocumentPropertyManager docProp = new CustomDocumentPropertyManager();
                bool res = docProp.AddCustomDocumentProperty(@"Temp",uniqueFileId, ref Doc);
                res = docProp.ChangePropertyValue(@"Temp", uniqueFileId, ref Doc);
                if (res == true)
                {
                    DocumentInfoHandler documentInfohandler = new DocumentInfoHandler();
                    Common.Message docInfo = new Common.Message();
                    docInfo.documentID = documentID;
                    docInfo.fileName = Doc.FullName;
                    res=documentInfohandler.WriteDocumentInfo(docInfo, uniqueFileId);
                }
            }
        }

        void wb_DocumentBeforeClose(Microsoft.Office.Interop.Word.Document Doc, ref bool Cancel)
        {
            string uniqueID = String.Empty;
           // MessageBox.Show(Doc.FullName);
            CustomDocumentPropertyManager docPropMgr = new CustomDocumentPropertyManager();
            uniqueID = docPropMgr.GetPropertyValue(@"Temp", ref Doc);
            if (uniqueID != null && uniqueID != String.Empty)
            {
                DocumentInfoHandler documentInfoHandler = new DocumentInfoHandler();

                Common.Message docInfo=documentInfoHandler.ReadDocumentInfo(uniqueID);
               
                if (docInfo != null && docInfo.documentID != null && docInfo.fileName != null && docInfo.documentID != String.Empty && docInfo.fileName != String.Empty)
                {
                    docInfo.fileName = Doc.FullName;
                    Serialization serializeObj = new Serialization();
                    byte[] requestData = null;
                    bool retVal = serializeObj.Serialize(ref docInfo, ref requestData);
                    if (retVal == true)
                    {
                        ClientApplication Client = new ClientApplication();
                        retVal = Client.StartClient();
                        if (retVal == true)
                        {
                            try
                            {
                                Client.connectDone.WaitOne();
                                Thread.Sleep(50);
                                Client.Send(ref requestData);
                                Client.sendDone.WaitOne();
                                Client.CloseSocket();
                            }
                            catch (Exception ex)
                            {
                            }
                        }
                    }
                }
            }
  }

        #region VSTO generated code

        /// <summary>

        /// Required method for Designer support - do not modify

        /// the contents of this method with the code editor.

        /// </summary>

        private void InternalStartup()
        {
            this.Startup += new System.EventHandler(ThisAddIn_Startup);
        }

        #endregion
    }
}

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