Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Excel Process ID

42 views
Skip to first unread message

Powerguy

unread,
Aug 31, 2004, 9:16:11 PM8/31/04
to
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

Tom Shelton

unread,
Sep 1, 2004, 1:05:29 AM9/1/04
to

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 :)
--
Tom Shelton [MVP]

Powerguy

unread,
Sep 1, 2004, 10:15:35 PM9/1/04
to
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


Tom Shelton <t...@YOUKNOWTHEDRILLmtogden.com> wrote in message news:<15vlg6xhimm33$.1wdlesv3...@40tude.net>...

Tom Shelton

unread,
Sep 2, 2004, 2:33:46 PM9/2/04
to
In article <7912f862.04090...@posting.google.com>, Powerguy wrote:
> 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 :)

--
Tom Shelton [MVP]

0 new messages