DocumentBeforeClose event fires but Cancel does not Cancel

J

jbarton

I'm writing a C# app in VS 2005 Pro using SQL Express and Word 2000, 2002,
2003, 2007. I intend to have a separate deployment for each version as I was
not able to find a way to do otherwise.

My problem is strange. I am using the following code to restrict the user
from closing Word without using my app:

#define useWord2000; //Build for Word 2000
Word_App = new Word.Application(); //Instantiate the Word
Application object.

//connect the exit event to a handler.
#if (useWord2000)
Word_App.DocumentBeforeClose += new
Word.ApplicationEvents2_DocumentBeforeCloseEventHandler(OnClose);
#elif (useWord2002)
Word_App.DocumentBeforeClose += new
ApplicationEvents3_DocumentBeforeCloseEventHandler(OnClose);
#elif (useWord2003)
Word_App.DocumentBeforeClose += new
ApplicationEvents4_DocumentBeforeCloseEventHandler(OnClose);
#elif (useWord2007)
//Not yet implemented
#endif

//Turn off anything that would allow the user to screw up.

Word_App.ActiveWindow.Application.CommandBars["Toolbar List"].Enabled =
false;
Word_App.ActiveWindow.Application.CommandBars["Menu
Bar"].Controls["File"].Enabled = false;
Word_App.ActiveWindow.Application.CommandBars["Tools"].Controls["Customize..."].Enabled = false;
Word_App.ActiveWindow.Application.CommandBars[1].Controls[1].Enabled =
false;
Word_App.ActiveWindow.Application.CommandBars[1].Controls[2].Enabled =
false;

/// <summary>
/// Prevent the user from closing Word. Renamed and then genenerated by
pressing "Tab".
/// </summary>
/// <param name="Doc"></param>
/// <param name="Cancel"></param>
void OnClose(Word.Document Doc, ref bool Cancel)
{
if (!AllowDocClose)
Cancel = true;
}



I was originally using Word 2003 and OnClose() was working as expected. It
worked for two days of coding and testing. Then it stopped working in that
although the event continues to fire, execution does infact set "Cancel" to
true; but the Document is not prevented from closing - leaving the
Application window still open. This presents an unsolvable problem for my
application and a big mess for the user. Task Manager is the only way out -
not a very desirable option since my users don't really know what Task Manger
is or how to use it.

Summary: DocumentBeforeClose event fires, OnClose handler is called - but
Cancel does not Cancel!

Any thoughts? I've seen this before using the Custom Validator. No one had
a clue!
 

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