[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 457
  • Last Modified:

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?  
0
TheUndecider
Asked:
TheUndecider
  • 2
1 Solution
 
omegaomegaDeveloperCommented:
Hello, TheUndecider,

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
0
 
TheUndeciderAuthor Commented:
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

Open in new window

0
 
omegaomegaDeveloperCommented:
Hello, TheUndecider,

I couldn't see anything in your posted code that would cause the "unqualified reference" problem mentioned.  So (after substituting simple values for the ExportDataSet object) I tried running it.  

In my case it didn't have any trouble at all closing/quiting Excel.  Perhaps the problem lies in some other piece of code called before or after this routine.  Or could it be in ExportDataSet?  If you run just the code posted (replacing ExportDataSet with appropriate constants) do you still have the problem?

Cheers,
Randy
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now