How to find the process id of excel application in ASP.Net

G

Guest

I am writing an asp.net applicaition using VB coding.

In a function, I am opening an excel file with the following code,
Dim objExcel As Object
Dim objWorkBook As Object
objExcel = CreateObject("Excel.Application")
objWorkBook = objExcel.Workbooks.Open(targetfilename)
* targetfilename is a variable that stores the excel file name and path.

In the error handler,

I want to kill only the excel application created using previous line of
code. There can be any other excel application running in the server that
were not created by this application. How will I identify the process ID of
this excel application through code and kill the process.
 
P

Paul Clement

¤ I am writing an asp.net applicaition using VB coding.
¤
¤ In a function, I am opening an excel file with the following code,
¤ Dim objExcel As Object
¤ Dim objWorkBook As Object
¤ objExcel = CreateObject("Excel.Application")
¤ objWorkBook = objExcel.Workbooks.Open(targetfilename)
¤ * targetfilename is a variable that stores the excel file name and path.
¤
¤ In the error handler,
¤
¤ I want to kill only the excel application created using previous line of
¤ code. There can be any other excel application running in the server that
¤ were not created by this application. How will I identify the process ID of
¤ this excel application through code and kill the process.

You could use the FindWindow API function call
(http://msdn.microsoft.com/library/d...indowreference/windowfunctions/findwindow.asp)

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
Dim WindowHandle As Int32
Dim ReturnVal As Int32
Const WM_QUIT = &H12

Do

ClassName = "XLMain"
WindowHandle = FindWindow(ClassName, Nothing)

If WindowHandle Then
ReturnVal = PostMessage(WindowHandle, WM_QUIT, 0, 0)
End If

Loop Until WindowHandle = 0

End Function


Paul
~~~~
Microsoft MVP (Visual Basic)
 
G

Guest

Thank you. I have one more issue in context to this same kill process
I have a excel report generated in asp.net.
I create a temporary excel file in the server and the report is generated in
the temproray file. The file is created only through asp.net code and can
never be opened manually in the microsoft excel application since security
rights doesn't allow the user to view the path.

The code,
If File.Exists(targetfilename) Then File.Delete(targetfilename)

throws exception some times, if the file is used by some other process.
The reason could be due to some error created when the report was prevuiosly
executed.

The I open the taskmanager, goto process, select Image name excel.exe ,
Image name as asp.net and click end process. Thus the line of code executes
after this.

Please let me know how I can do this in asp.net

Your advice will greatly help me to program better.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top