TheUndecider
asked on
Kill Excel process in Visual Studio 2003 (Excel 2000 and VB.NET 2003)
Hello, I am using vb.net 2003 and Office 2000. I am trying to find a way to use an Excel spreadsheet in .NET, but I still have an instance of Excel running in task manager.
I'm trying to find a way to find the ID process for the excel spreadsheet I am using and then kill it.
Is there a way to get the process ID for Excel 2000 and then kill it without killing any other opened Excel applications?
I'm trying to find a way to find the ID process for the excel spreadsheet I am using and then kill it.
Is there a way to get the process ID for Excel 2000 and then kill it without killing any other opened Excel applications?
ASKER
Well... I had pasted my code in another question, but I still have problems. Here's my code again so you can see whether I do have an unqualified reference to an Excel object.
Dim ExlApp As Excel.Application
Dim iCol, iRow, iColVal As Integer
Dim bNew As Boolean
Dim i As Integer
FilePath = "c:\MyExcelFile.xls"
' Open the document that was chosen by the dialog
Dim aBook As Excel.Workbook
Try
're-initialize excel app
ExlApp = New Excel.Application
If ExlApp Is Nothing Then
' throw an exception
Throw (New Exception("Unable to Start Microsoft Excel"))
Else
'supresses overwrite warnings
ExlApp.DisplayAlerts = False
'check if file exists
If File.Exists(FilePath) Then
aBook = ExlApp.Workbooks.Open(FilePath)
Else
aBook = ExlApp.Workbooks.Add(Excel.XlWBATemplate.xlWBATWorksheet)
End If
With ExlApp
.SheetsInNewWorkbook = 1
'.Workbooks.Add()
.Worksheets(1).Select()
'For displaying the column name in the the excel file.
For iCol = 0 To ExportDataSet.Tables(0).Columns.Count - 1
''clear column name before setting a new value
.Cells(1, iCol + 1).Value = ""
.Cells(1, iCol + 1).Value = ExportDataSet.Tables(0).Columns(iCol).ColumnName.ToString
Next
'For displaying the column value row-by-row in the the excel file.
For iRow = 0 To ExportDataSet.Tables(0).Rows.Count - 1
For iColVal = 0 To ExportDataSet.Tables(0).Columns.Count - 1
.Cells(iRow + 2, iColVal + 1).Value = Trim(ExportDataSet.Tables(0).Rows(iRow).ItemArray(iColVal).ToString)
Next
Next
'Check if the file exists
If File.Exists(FilePath) Then
' It does. Save it
.ActiveWorkbook().Save()
Else
'Save it with the given file name
.ActiveWorkbook().SaveAs(Filename:=FilePath)
End If
End With
'Let the user know that the excel file was exported successfully
MsgBox(FilePath & " File exported as Excel spreadsheet sucessfully")
'clear the file path
FilePath = Nothing
End If
'warn users if there's an error
Catch ex As Runtime.InteropServices.COMException
MsgBox("ERROR: " & ex.Message)
Catch ex As Exception
MsgBox("ERROR: " & ex.Message)
Finally
' Make sure that Excel is properly exited and al the COM objects are released
aBook.Close(false)
ExlApp.Quit()
System.Runtime.InteropServices.Marshal.ReleaseComObject(ExlApp)
aBook = Nothing
ExlApp = Nothing
GC.Collect()
GC.WaitForPendingFinalizers()
GC.Collect()
GC.WaitForPendingFinalizers()
End Try
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I wonder if your problem is actually due to an unqualified reference to an Excel object. See the following links for more information:
http://www.tushar-mehta.com/excel/vba/xl_doesnt_quit/
http://support.microsoft.com/kb/178510/
Cheers,
Randy