COM Addin terminating Word but no exception thrown

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
}
}
 
C

Cindy M.

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'm not following exactly what you mean, here. Are you sure your code
is finishing?

Which version of Word? Is it still in memory after it crashes (Task
manager)? Does it recover?

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question
or reply in the newsgroup and not by e-mail :)
 
G

guyza

I am running Word 2003. I have put in the Debugger.Launch() so as soon
as my COM addin is loaded I can follow the path of execution which is:

1) Start word which loads my COM Addin
2) Visual Studio: COM Addin loads up and Debugger.Launch() initiates
debugger. Code adds event handlers and returns execution to Word
3) Word: Open new template instance which fires my event handler
4) Visual Studio: Event handler (appEvents_NewDocument()) fires, I
step through all code and leave my component without execption being
thrown
5) Word: On Word resuming execution it crashes i.e. after my component
has finished execution with no exceptions thrown

Before the Word application terminates it shows an error dialog that
states: "Microsoft Office Word has encountered a problem and needs to
close. We are sorry for the inconvenience." Where it has a check box
to recover documents and restart Word. If this is checked it recovers
doc otherwise the Word process is terminated.

As far as I know the COM Addin runs internally to the Word process so
since the Word process is terminated the COM Addin is also terminated.

Any ideas?
 
G

guyza

Just to remove some ambiguity here, when the dialog appears Word
always crashes. Checking the checkbox just recovers the document and
restarts Word with the recovered document. Not checking the checkbox
just terminates Word with no document recovery.
 
C

Cindy M.

I am running Word 2003. I have put in the Debugger.Launch() so as soon
as my COM addin is loaded I can follow the path of execution which is:

1) Start word which loads my COM Addin
2) Visual Studio: COM Addin loads up and Debugger.Launch() initiates
debugger. Code adds event handlers and returns execution to Word
3) Word: Open new template instance which fires my event handler
4) Visual Studio: Event handler (appEvents_NewDocument()) fires, I
step through all code and leave my component without execption being
thrown
5) Word: On Word resuming execution it crashes i.e. after my component
has finished execution with no exceptions thrown

Before the Word application terminates it shows an error dialog that
states: "Microsoft Office Word has encountered a problem and needs to
close. We are sorry for the inconvenience." Where it has a check box
to recover documents and restart Word. If this is checked it recovers
doc otherwise the Word process is terminated.

As far as I know the COM Addin runs internally to the Word process so
since the Word process is terminated the COM Addin is also terminated.
OK, thanks for the clarification. When reading through your code, one
line does make me stop and wonder:

//Need to save doc to integrate with document managment system
Word.Application app = Doc.Application;

Why aren't you using the application object provided by the Add-in? This
could be causing a conflict.


Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question
or reply in the newsgroup and not by e-mail :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top