Solved

Close Excel instance when Windows Form is closed by user in a VB 2008 application

Posted on 2012-04-06
2
343 Views
Last Modified: 2012-06-22
I'm currently developing an application using Visual Basic, in Visual Studio 2008 Professional, on a Windows XP machine. However, we have both XP and Windows 7 client workstations in our office.

In my application, I'm exporting the results displayed in a DataGridView control to MS Excel, using the methods in the Microsoft.Office.Interop.Excel namespace. The procedure which opens the instance of Excel and exports the data is running in a separate thread from the form which contains the DataGridView control. This is working well.

However, if the user closes the form before the export is completed I want to cancel the thread and close the instance of Excel without saving it. I can use the Thread.Abort method in my form's FormClosing event handler to kill the thread, but the Excel process is still hanging out there even after the application is closed.

What would be the best way to identify and kill that Excel process if the user closes the form prior to the export being completed?
0
Comment
Question by:JeffEdmunds
2 Comments
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 500 total points
Comment Utility
I don't think there is any event on the thread which would indicate thread being aborted.

One option is to declare the Excel app variable in common place such as module. Thread then populates it. When calling thread.abort, you can dispose that app variable after aborting thread.
0
 
LVL 1

Author Closing Comment

by:JeffEdmunds
Comment Utility
You nailed it... I had the declaration inside the module that was being called by the separate thread. Declaring within the form's Declarations section gave it the necessary scope.

Private xlApp As Excel.Application
Private myThread As Thread
....

Then in the FormClosing event handler:

If Not myThread.Equals(Nothing) Then
    If myThread.IsAlive Then
        myThread.Abort()
        xlApp.Quit()
    End If
End If

Seems to be working well - thanks!
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
How to batch remove spreadsheet password 19 80
Excel formula 6 20
Get String split 5 31
File size limit in SharePoint 2010 3 12
Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
Introduction While answering a recent question (http:/Q_27311462.html), I created an alternative function to the Excel Concatenate() function that you might find useful.  I tested several solutions and share the results in this article as well as t…
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now