|
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.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.