473,322 Members | 1,614 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Excel Process ID

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
Nov 21 '05 #1
2 21854
On 31 Aug 2004 18:16:11 -0700, Powerguy wrote:
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 :)
--
Tom Shelton [MVP]
Nov 21 '05 #2
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 <to*@YOUKNOWTHEDRILLmtogden.com> wrote in message news:<15******************************@40tude.net> ...
On 31 Aug 2004 18:16:11 -0700, Powerguy wrote:
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 :)

Nov 21 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Tim Marsden | last post by:
Hi, I have a routine which is call from a ASP.NET web form. This routine creates an excel application, opens a workbook , runs some code to update the workbook, saves it as HTML on the sever and...
2
by: Praveen K | last post by:
I have a problem in communicating between the C# and the Excel Interop objects. The problem is something as described below. I use Microsoft Office-XP PIA dll’s as these dll’s were been...
18
by: lgbjr | last post by:
Hi All, I have a VB.NET app that, among other things, writes data to Excel. I am having trouble getting the Excel process to terminate after I quit Excel. I found an article related to this...
16
by: LP | last post by:
Hello, I am trying to use .NET with Excel. I installed Office 2003 and selected ..NET programming suport option, so it installed all those PIA, as MS sugests. But I can not find a way to destroy...
1
by: Tony Hedge | last post by:
Hello, In my .NET code behind, I open up an excel file, and process all the rows in the worksheet that I'm interested in. Problem is when I'm done and issue the quit method. The EXCEL process...
13
by: chuckie_9497 | last post by:
hello all you gurus. I am struggling with releasing com objects. I have isolated the problem to the code below. Objects are released and the process ends until I use "int k = sheet.Count;" Then...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.