locked
How to Kill Process Excel Object? RRS feed

  • Question







  • I have written Code for Excel File Create via ASP.Net  with VB Code, I want Kill the process
    Explictly

    I already used GC.Collect(), But it takes more time.
    I want Kill immedately.
     

    thanks.

    • Edited by bala ask Tuesday, August 18, 2009 5:59 AM
    Tuesday, August 18, 2009 5:56 AM

Answers

  • Hello bala
                 you can use this code for Kill excel immedately

    Imports System.Management

    Public Class Form5

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

            Try

                Dim query As SelectQuery = New SelectQuery("SELECT * FROM Win32_Process WHERE Name = 'EXCEL.EXE'")

                Dim mgmtSearcher As ManagementObjectSearcher = New ManagementObjectSearcher(query)

                For Each p As ManagementObject In mgmtSearcher.Get()

                    Dim s(1) As String

                    p.InvokeMethod("GetOwner", DirectCast(s, Object()))

                    If p("Name").ToString().ToUpper = "EXCEL.EXE" Then

                        p.InvokeMethod("Terminate", Nothing)

                       ' Exit For

                    End If

                Next

     

            Catch ex As Exception

                MessageBox.Show(ex.Message.ToCharArray)

            End Try

        End Sub

     

    End Class


    Tuesday, August 18, 2009 12:50 PM

All replies

  • Private Sub KillProcess(ByVal processName As String)
        Dim myproc As New System.Diagnostics.Process()
        Try
            For Each thisproc As Process In Process.GetProcessesByName(processName)
                If Not thisproc.CloseMainWindow() Then
                    thisproc.Kill()
                End If
                ' next proc
            Next
        Catch Exc As Exception
            Dim msg As String = Exc.Message
        End Try
    End Sub
    

    If that doesnt work.. you can try the Win32 API calls..

    Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Int32
    
    Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Int32, ByVal wMsg As Int32, _
    
    ByVal wParam As Int32, ByVal lParam As Int32) As Int32
    
    Public Function TerminateExcel()
    
    Dim ClassName As String = "XLMain"
    
    Dim WindowHandle As Int32
    
    Dim ReturnVal As Int32
    
    Const WM_QUIT = &H12
    
    Do
    
        WindowHandle = FindWindow(ClassName, Nothing)
    
        If WindowHandle Then
    
            ReturnVal = PostMessage(WindowHandle, WM_QUIT, 0, 0)
    
        End If
    
    Loop Until WindowHandle = 0
    
    End Function

    Also refer to this..

    50 Ways to Kill Excel


    Life would have been much easier if I had the source-code !!
    Tuesday, August 18, 2009 6:08 AM
  • thanks for ur reply


    its does not kill process immedately after execution of this code.


    • Proposed as answer by Edt1950 Thursday, June 30, 2011 9:28 PM
    Tuesday, August 18, 2009 9:56 AM
  • Hello bala
                 you can use this code for Kill excel immedately

    Imports System.Management

    Public Class Form5

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

            Try

                Dim query As SelectQuery = New SelectQuery("SELECT * FROM Win32_Process WHERE Name = 'EXCEL.EXE'")

                Dim mgmtSearcher As ManagementObjectSearcher = New ManagementObjectSearcher(query)

                For Each p As ManagementObject In mgmtSearcher.Get()

                    Dim s(1) As String

                    p.InvokeMethod("GetOwner", DirectCast(s, Object()))

                    If p("Name").ToString().ToUpper = "EXCEL.EXE" Then

                        p.InvokeMethod("Terminate", Nothing)

                       ' Exit For

                    End If

                Next

     

            Catch ex As Exception

                MessageBox.Show(ex.Message.ToCharArray)

            End Try

        End Sub

     

    End Class


    Tuesday, August 18, 2009 12:50 PM
  • It works Close All Excel Application,

    I want close only current Session Users which Opened Excel.

    Wednesday, August 26, 2009 8:07 AM