Loosing event handlers in word

V

Vasiliy

Hi all!
I'm developing an application to manage word templates and documents.
Documents are stored in database, so when user wants to edit a document
i make a local copy of this document and then open it through ms word.
I have handlers for beforeClose, beforeSave and Quit events. The
problem is following: i open any number of documents for editing, then
edit any document (say, document A) and don't save it (by ctrl+s, save
button e t.c.) and then close it (by alt+f4, close button) then my
application catch _only_ beforeClose event for document A (quit event
is not handled). For other opened documents application doesn't catch
any events at all. So now i can close all opened documents. After it i
can open documents again and _only now_ all event handlers work for
opened documents.

Here is the code i used

//adding handlers
....
app.DocumentBeforeClose += new
Microsoft.Office.Interop.Word.ApplicationEvents4_DocumentBeforeCloseEventHandler(app_DocumentBeforeClose);
app.DocumentBeforeSave += new
Microsoft.Office.Interop.Word.ApplicationEvents4_DocumentBeforeSaveEventHandler(app_DocumentBeforeSave);
app.ApplicationEvents2_Event_Quit += new
Microsoft.Office.Interop.Word.ApplicationEvents2_QuitEventHandler(app_ApplicationEvents2_Event_Quit);
.....

//handler for beforeClose event
private void
app_DocumentBeforeClose(Microsoft.Office.Interop.Word.Document Doc, ref
bool Cancel)
{
//MessageBox.Show("Event BeforeClose");
//here i check if it is my application's
document or not
if (IsIMTemplate(Doc))
{
this.IMTemplateCheckedOut = false;
if (Doc.Saved)
{
this.imTemplateNeedUpdating = false;
}
else
{
DialogResult dialogResult;
//formDBC - just a form with 3
buttons Yes, No, Cancel
frmDialogBeforeClose formDBC = new frmDialogBeforeClose();
formDBC.TopMost = true;

formDBC.Focus();

dialogResult = formDBC.ShowDialog();

//do nothing
if (dialogResult == DialogResult.Cancel)
{//
Cancel = true;
this.IMTemplateCheckedOut = true;
}

//close documnet without updating it in database
if (dialogResult == DialogResult.No)
{
this.imTemplateNeedUpdating = false;


object saveChanges = false;
object optional = Missing.Value;
Word.ApplicationClass tmpApp = (Word.ApplicationClass)Doc.Parent;
Doc.Close(ref saveChanges, ref optional, ref optional);
tmpApp.Quit(ref saveChanges, ref optional, ref optional);
}

//close with updating in database
if (dialogResult == DialogResult.Yes)
{
this.imTemplateNeedUpdating = true;
Doc.Save();
}
formDBC.Dispose();
}//else
}
}

also i have handlers for other events but i think the problem somewhere
else...
Thnx in advance.
 

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