That is too cool... I suppose I need to make my word application object owned by the project rather than buried in a user control.... let me go wire that up and I will get back to you and let you know how it went.
Main Topics
Browse All TopicsI have an application that starts an instance of word, determines the handle of the window and then changes the parent application from the desktop to a panel in my application. When my application is done with word, it resets the parent to the desktop and then closes (gracefully) the instance of word.
That works fine.
The problem is when for either because I am debugging another portion of the application, or because I forgot about a bug in the release, I have to terminate my application ungracefully (i.e. stop in the compiler) or even worse (gasp) taskmgr and stop process.
What I would like to see is that somehow my application send the word application object a {wordapplicationobject}.qu
I think the simplest solution would be if VB.net has a way to run that certain code on crash, any crash, even when I press stop...
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
I created s standalone class that instantiated the MS word and MS Project objects at program start. I added the following code to the application events as follows:
Private Sub MyApplication_UnhandledExc
SetParent(hWndAdoptedWordA
WordApp.Quit()
WordApp = Nothing
ProjectApp.Quit()
ProjectApp = Nothing
End Sub
However though this will work if there was an exception, If I or the user "terminated abnormally" such as if I hit the stop button in the compiler, or the user hit taskmgr>End Process the code won't run. Is there something like an AbnormalTermination Event on the application object?
Maybe run some kind of vb for apps code in each of the word and the project instances that checks to see if the main app is running every 10 seconds, and if not, then it kills the instance?
I hould also mention I did add the same code to the "shutdown" application event. Under normal circumstances this works fine, however I'm looking for a way to clean things up in all cases. (the 1% of the time an abnormal termination occurs either because of lack of good coding or because the user terminates using the taskmgr)
You can use the Shutdown event. Keep a global setting (for example, a Shared bololean variable in a class, defaulting to False) indicating when the user has exited normally (in that case, you know you've closed your COM objects the right way). In the Shutdown event, if that global setting is False, you know that you've got to close the COM objects.
Hope that helps.
wont the variable be destroyed if the application was sent a kill? If not, where do I put the if bClosedNormally = false then wordapp.close code?
vs 2008 puts this comment at the top of the application event:
' The following events are availble for MyApplication:
'
' Startup: Raised when the application starts, before the startup form is created.
' Shutdown: Raised after all application forms are closed. This event is not raised if the application terminates abnormally.
' UnhandledException: Raised if the application encounters an unhandled exception.
' StartupNextInstance: Raised when launching a single-instance application and the application is already active.
' NetworkAvailabilityChanged
That seems to suggest that shutdown won't run if the app terminates abnormally.
Thanks for your help... we are 99% there with the current improvement, but I am still having to manually kill the two processes when I make another coding error and terminate the application with the "stop" button because the compiler won't let me make the particular change I want to make.
Mmm... unfortunately, I've been testing the workaround with bad results. When stopping compiler in Visual Studio, or when closing application terminating process with taskbar, none of the following events is raised:
- Form.Closing
- Form.Closed
- Form.HandleDestroyed
- Application.Shutdown
So I think is not possible to control all posibilities, unless you keep a completely external process (another application, maybe in background) monitoring your main application and closing COM objects if detects that main application has been closed but COM objects has been not.
Hope that helps.
Business Accounts
Answer for Membership
by: RolandDeschainPosted on 2009-10-19 at 08:33:02ID: 25606124
Well, I think you can use application events.
If you don't know, you can use a series of limited global application events for your .net windows forms applications. To use them in your application, follow this steps (sorry if some description of button text, or similar, is not exact, because my Visual Studio is not in english):
- Right-click project node on Solution Explorer, then click on Properties to view Properties window.
- In the first tab of properties window, named "Application", there's a button at the bottom of the window named "View application events" (or something similar, remember that my Visual Studio is not in english). Click on this button.
- A newly class is added to your project (ApplicationEvents.vb -well, I'm working with VB but I guess that it will be similar in C#, but with changed extension).
- Use the objects ComboBox to view events of MyApplication object. One of those events is UnhandledException.
This events raises up every time an unhandled exception throws in your app. Parameter "e" contains information about the exception, and the ability to cancel the application shutdown (with ExitApplication property). You can use this event to put your code to close word object properly.
Hope that helps.