Excel Process ID

P

Powerguy

Hi all,

I am looking for a way to get the Process id (or a handle) of an EXCEL
process created from within my code.

For example when the following code is executed:

Dim EXL As Excel.Application = New Excel.Application

a new instance of EXCEL.EXE is created in the task manager.

I am looking for some code (or ideas) of how to identify this
particular instance of EXCEL.EXE AS IT IS CREATED. I know I can use
something like this:

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

but this code will give me ALL instances of EXCEL processes running on
the computer. I know I can also get the users of the processes but
what if I myself had just say 5 instances of EXCEL already running on
my computer?? How do I identify which is the Process Id of EXCEL
created (opened) by my code??

I'm sure some smart people out there have the answers! Appreciate any
help.

PowaGuy
 
T

Tom Shelton

Hi all,

I am looking for a way to get the Process id (or a handle) of an EXCEL
process created from within my code.

For example when the following code is executed:

Dim EXL As Excel.Application = New Excel.Application

a new instance of EXCEL.EXE is created in the task manager.

I am looking for some code (or ideas) of how to identify this
particular instance of EXCEL.EXE AS IT IS CREATED. I know I can use
something like this:

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

but this code will give me ALL instances of EXCEL processes running on
the computer. I know I can also get the users of the processes but
what if I myself had just say 5 instances of EXCEL already running on
my computer?? How do I identify which is the Process Id of EXCEL
created (opened) by my code??

I'm sure some smart people out there have the answers! Appreciate any
help.

PowaGuy

Well, it looks like the Application object has an hWnd property that
returns a handle to the main application window... Using that information
and a little API majic, we can get what your after :)

Private Declare Function GetWindowThreadProcessId Lib "user32" ( _
ByVal hWnd As IntPtr, _
ByRef lpdwProcessId As IntPtr) As IntPtr

' create the excel instance...
Dim exl As Excel.Application = New Excel.Application

' let's get it's process!
Dim processId As IntPtr

' we ignore the return value, since we don't care
' about the thread id...
GetWindowThreadProcessId (exl.hWnd, processId)

Dim excelProcess As Process = Process.GetProcessById (processId.ToInt32())

That should do it :)
 
P

Powerguy

Thanks Tom!!

Your Idea worked! I had to modify your code alittle bit to make it
work. The Modified code is as follows:

Private Declare Function GetWindowThreadProcessId Lib "user32" ( _
ByVal hWnd As Integer, _ <-- Needed to be changed to an Integer
ByRef lpdwProcessId As IntPtr) As IntPtr

' create the excel instance...
Dim exl As Excel.Application = New Excel.Application

' let's get it's process!
Dim processId As IntPtr

' we ignore the return value, since we don't care
' about the thread id...
GetWindowThreadProcessId(exl.Hwnd, processId)

Dim excelProcess As Process =
Process.GetProcessById(processId.ToInt32())
Debug.WriteLine(processId)
excelProcess.Kill()

I am now able to terminate the correct EXCEL process from within my
code if excel does not close properly. Thanks again Tom, You the
man!

Powaguy
 
T

Tom Shelton

Thanks Tom!!

Your Idea worked! I had to modify your code alittle bit to make it
work. The Modified code is as follows:

Well, that's what I get for posting code off the cuff :)
 

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