By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,973 Members | 1,277 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,973 IT Pros & Developers. It's quick & easy.

Loosing event handlers in word

P: n/a
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_D ocumentBeforeCloseEventHandler(app_DocumentBeforeC lose);
app.DocumentBeforeSave += new
Microsoft.Office.Interop.Word.ApplicationEvents4_D ocumentBeforeSaveEventHandler(app_DocumentBeforeSa ve);
app.ApplicationEvents2_Event_Quit += new
Microsoft.Office.Interop.Word.ApplicationEvents2_Q uitEventHandler(app_ApplicationEvents2_Event_Quit) ;
.....

//handler for beforeClose event
private void
app_DocumentBeforeClose(Microsoft.Office.Interop.W ord.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.

Apr 5 '06 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.