Link to home
Start Free TrialLog in
Avatar of mainrotor
mainrotor

asked on

I need help closing Excel.exe processes from my ASP.Net application

Hi experts,
I have written a simple asp.net 3.5 application with vb.net codebehind.  All the application does is open an Excel file and gets the last used row in the spreadsheet.  Then i close the file and try to close any Excel.exe processes running in the Task Manager.  
I am able to successfully stop all my Excel.exe processes when I comment out the portion of code that gets the last used row.  If I uncomment the portion of code that gets the last used row, the Excel.exe process won't stop running in the Task Manager.  Why is that?  Why won't the Excel.exe process stop running?
I have included my codebehind in this post.

Thanks in advance,
mrotor
Imports System.Data.OleDb
Imports System.Runtime.InteropServices


Partial Class _Default
    Inherits System.Web.UI.Page

    Dim objXL As Microsoft.Office.Interop.Excel.Application
    Dim objWkB As Microsoft.Office.Interop.Excel.Workbook

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim LastRow As Long

        Try
            objXL = New Microsoft.Office.Interop.Excel.Application
            objWkB = objXL.Workbooks.Open("C:\Test.xls")
            objXL.Visible = False

            ''My Excel.Exe process won't close if I uncomment the following 3 lines of code.  Why???
            'With objWkB
            '    LastRow = .ActiveSheet.UsedRange.Rows(.ActiveSheet.UsedRange.Rows.Count).Row
            'End With


            Call sL2_CloseExcel()
        Catch ex As OleDbException
            Dim strErr2 As String
            strErr2 = ex.Message
            lblErr.Text = strErr2
        End Try
    End Sub

    Protected Sub sL2_CloseExcel()
        ''Quit the existing Excel application.
        objWkB.Close(False)
        objXL.Quit()

        Marshal.ReleaseComObject(objWkB)
        Marshal.ReleaseComObject(objXL)


        objWkB = Nothing
        objXL = Nothing

        System.GC.Collect()
        System.GC.WaitForPendingFinalizers()
        System.GC.Collect()
    End Sub
End Class

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Rick
Rick

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mainrotor
mainrotor

ASKER

rick_gwu,
nothing happens.  Actually I get an error because it doesn't know whar Process is.
Intelisense keeps trying to put ProcessInfo or ProcessStatus.  
What is the issue?  Do I have to do an Imports of something?

You need to import Diagnostics
... or rewrite like this:

 Dim pr() As Diagnostics.Process = Diagnostics.Process.GetProcessesByName("excel")

        For Each p As Diagnostics.Process In pr
            p.CloseMainWindow()
        Next
Imports System.Diagnostics
I did that and got it to work, with one major flaw.  If a user has another Excel spreadsheet/Process open it will also close that spreadsheet/Process.  It closes every Excel process, not just the one created by my application.  I need it to close only the Excel process(es) created by my app.  How can I do this?
Is this not running on a server?
It shouldn't kill a process running on a client's machine.

...

Anyway, if you need to kill only the process created by your application, you will need to get its process id

   http://www.daniweb.com/software-development/vbnet/code/217127

Then :

   Diagnostics.Process.GetProcessById(YourProcessId).CloseMainWindow()

Avatar of Nasir Razzaq
Try adding

GC.Collect()

at the end of your original code.