G
guyza
Hi,
I have created a COM Addin for Word. However when I create a
document from a custom template located in WorgroupTemplate path i.e.
File > New > Templates on my computer > MyCustomTemplate.dot. Word
crashes after leaving my COM addin, NOTE: No exception thrown.
I have disabled all other COM Addins (Except mine &
WordDesignTimeAdaptor) in the registry by changing the LoadBehavior =
2 on all com addins located at:
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Office\Word\Addins\
WorkArounds: I am able to double click the template from the windows
explorer and it opens and creates the doc fine. I am also able to
close the Document1, opened on word start up, and create a new one as
described above and it works fine. I have tried to programmatically
shut Document1 before doing anything however I encounter teh same word
termination without exception thrown.
Here is the code:
//System namespaces
using System;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Diagnostics;
//Microsoft Namespaces
using Microsoft.Office;
using Microsoft.Office.Core;
using Extensibility;
using Word = Microsoft.Office.Interop.Word;
using System.Windows.Forms;
namespace TBU.Enterprise.OfficePlugin
{
[ComVisible(true)]
public class Connect : Object, Extensibility.IDTExtensibility2
{
object missingValue = System.Reflection.Missing.Value;
object falseValue = false;
public Connect()
{
}
public void OnConnection(object Application,
Extensibility.ext_ConnectMode ConnectMode, object AddInInst, ref
System.Array custom)
{
try
{
object appName =
Application.GetType().InvokeMember("Name", BindingFlags.GetProperty,
null, Application, null);
string applicationName = appName.ToString();
if (applicationName == "Microsoft Word")
{
#if DEBUG
if (!Debugger.IsAttached)
Debugger.Launch();
#endif
Word.ApplicationEvents3_Event appEvents =
(Word.ApplicationEvents3_Event)Application;
appEvents.NewDocument += new
Word.ApplicationEvents3_NewDocumentEventHandler(appEvents_NewDocument);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
void appEvents_NewDocument(Word.Document Doc)
{
try
{
//Need to save doc to integrate with document
managment system
Word.Application app = Doc.Application;
object newFileName = Path.Combine(Path.GetTempPath(),
"GuyTest.doc");
Doc.SaveAs(ref newFileName, ref missingValue, ref
missingValue, ref missingValue, ref falseValue,
ref missingValue, ref missingValue, ref
missingValue, ref missingValue, ref missingValue,
ref missingValue, ref missingValue, ref
missingValue, ref missingValue, ref missingValue, ref missingValue);
Doc.Close(ref falseValue, ref missingValue, ref
missingValue);
Doc = null;
//**
//Here is where I would check in to doc man sys but
has to be closed
//**
//Now need to open doc that doc man sys has updated,
then update myself, then save
Doc = app.Documents.Open(ref newFileName, ref
missingValue, ref missingValue, ref falseValue,
ref missingValue, ref missingValue, ref
missingValue, ref missingValue, ref missingValue, ref missingValue,
ref missingValue, ref missingValue, ref
missingValue, ref missingValue, ref missingValue, ref missingValue);
string documentName =
Path.GetFileNameWithoutExtension(newFileName.ToString());
object customProp = "DocName";
DocumentProperties custProps =
(DocumentProperties)Doc.CustomDocumentProperties;
if (custProps[customProp] != null)
custProps[customProp].Value =
(object)documentName;
else
custProps.Add(customProp.ToString(), false,
MsoDocProperties.msoPropertyTypeString,
(object)documentName, (object)false);
Doc.Save();
Doc.Close(ref falseValue, ref missingValue, ref
missingValue);
Doc = null;
//Open doc again for user to see
Doc = app.Documents.Open(ref newFileName, ref
missingValue, ref missingValue, ref falseValue,
ref missingValue, ref missingValue, ref
missingValue, ref missingValue, ref missingValue, ref missingValue,
ref missingValue, ref missingValue, ref
missingValue, ref missingValue, ref missingValue, ref missingValue);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
#region Methods required by base class but not used
public void OnDisconnection(Extensibility.ext_DisconnectMode
RemoveMode, ref System.Array custom)
{
}
public void OnAddInsUpdate(ref System.Array custom)
{
}
public void OnStartupComplete(ref System.Array custom)
{
}
public void OnBeginShutdown(ref System.Array custom)
{
}
#endregion
}
}
I have created a COM Addin for Word. However when I create a
document from a custom template located in WorgroupTemplate path i.e.
File > New > Templates on my computer > MyCustomTemplate.dot. Word
crashes after leaving my COM addin, NOTE: No exception thrown.
I have disabled all other COM Addins (Except mine &
WordDesignTimeAdaptor) in the registry by changing the LoadBehavior =
2 on all com addins located at:
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Office\Word\Addins\
WorkArounds: I am able to double click the template from the windows
explorer and it opens and creates the doc fine. I am also able to
close the Document1, opened on word start up, and create a new one as
described above and it works fine. I have tried to programmatically
shut Document1 before doing anything however I encounter teh same word
termination without exception thrown.
Here is the code:
//System namespaces
using System;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Diagnostics;
//Microsoft Namespaces
using Microsoft.Office;
using Microsoft.Office.Core;
using Extensibility;
using Word = Microsoft.Office.Interop.Word;
using System.Windows.Forms;
namespace TBU.Enterprise.OfficePlugin
{
[ComVisible(true)]
public class Connect : Object, Extensibility.IDTExtensibility2
{
object missingValue = System.Reflection.Missing.Value;
object falseValue = false;
public Connect()
{
}
public void OnConnection(object Application,
Extensibility.ext_ConnectMode ConnectMode, object AddInInst, ref
System.Array custom)
{
try
{
object appName =
Application.GetType().InvokeMember("Name", BindingFlags.GetProperty,
null, Application, null);
string applicationName = appName.ToString();
if (applicationName == "Microsoft Word")
{
#if DEBUG
if (!Debugger.IsAttached)
Debugger.Launch();
#endif
Word.ApplicationEvents3_Event appEvents =
(Word.ApplicationEvents3_Event)Application;
appEvents.NewDocument += new
Word.ApplicationEvents3_NewDocumentEventHandler(appEvents_NewDocument);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
void appEvents_NewDocument(Word.Document Doc)
{
try
{
//Need to save doc to integrate with document
managment system
Word.Application app = Doc.Application;
object newFileName = Path.Combine(Path.GetTempPath(),
"GuyTest.doc");
Doc.SaveAs(ref newFileName, ref missingValue, ref
missingValue, ref missingValue, ref falseValue,
ref missingValue, ref missingValue, ref
missingValue, ref missingValue, ref missingValue,
ref missingValue, ref missingValue, ref
missingValue, ref missingValue, ref missingValue, ref missingValue);
Doc.Close(ref falseValue, ref missingValue, ref
missingValue);
Doc = null;
//**
//Here is where I would check in to doc man sys but
has to be closed
//**
//Now need to open doc that doc man sys has updated,
then update myself, then save
Doc = app.Documents.Open(ref newFileName, ref
missingValue, ref missingValue, ref falseValue,
ref missingValue, ref missingValue, ref
missingValue, ref missingValue, ref missingValue, ref missingValue,
ref missingValue, ref missingValue, ref
missingValue, ref missingValue, ref missingValue, ref missingValue);
string documentName =
Path.GetFileNameWithoutExtension(newFileName.ToString());
object customProp = "DocName";
DocumentProperties custProps =
(DocumentProperties)Doc.CustomDocumentProperties;
if (custProps[customProp] != null)
custProps[customProp].Value =
(object)documentName;
else
custProps.Add(customProp.ToString(), false,
MsoDocProperties.msoPropertyTypeString,
(object)documentName, (object)false);
Doc.Save();
Doc.Close(ref falseValue, ref missingValue, ref
missingValue);
Doc = null;
//Open doc again for user to see
Doc = app.Documents.Open(ref newFileName, ref
missingValue, ref missingValue, ref falseValue,
ref missingValue, ref missingValue, ref
missingValue, ref missingValue, ref missingValue, ref missingValue,
ref missingValue, ref missingValue, ref
missingValue, ref missingValue, ref missingValue, ref missingValue);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
#region Methods required by base class but not used
public void OnDisconnection(Extensibility.ext_DisconnectMode
RemoveMode, ref System.Array custom)
{
}
public void OnAddInsUpdate(ref System.Array custom)
{
}
public void OnStartupComplete(ref System.Array custom)
{
}
public void OnBeginShutdown(ref System.Array custom)
{
}
#endregion
}
}