Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

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

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

0
mainrotor
Asked:
mainrotor
  • 5
  • 2
1 Solution
 
RickCommented:
What happens when you try this?


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

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


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

        For Each p As Process In pr
          p.CloseMainWindow()
        Next

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

Open in new window

0
 
mainrotorAuthor Commented:
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?

0
 
RickCommented:
You need to import Diagnostics
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
RickCommented:
... or rewrite like this:

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

        For Each p As Diagnostics.Process In pr
            p.CloseMainWindow()
        Next
0
 
RickCommented:
Imports System.Diagnostics
0
 
mainrotorAuthor Commented:
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?
0
 
RickCommented:
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()

0
 
CodeCruiserCommented:
Try adding

GC.Collect()

at the end of your original code.
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.

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