Retrieve Process ID of a background application

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
ShubhamM
Posts: 38
Joined: 08 Nov 2017, 21:38

Retrieve Process ID of a background application

Post by ShubhamM » 11 Jan 2019, 14:25

Hi,

I'm trying to obtain the unique process ID for a run of an excel application that's running in the background via COM so that I can kill/control that specific instance of excel (WinKill and IfWinActive, etc. don't respond to it as it doesn't show as a window). The following enables the excel file to run in the background; please assist in obtaining its unique PID:

Code: Select all

Example_FilePath := A_Desktop "\Example.xlsx"
Example_File := ComObjGet(Example_FilePath)
Thanks!

User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Retrieve Process ID of a background application

Post by jeeswg » 06 Mar 2019, 04:42

The Excel Application object has an Hwnd properly, from which you can obtain the PID. Cheers.

Code: Select all

;[Excel_Get function]
;MS-Office-COM-Basics/Excel_Get.ahk at master · ahkon/MS-Office-COM-Basics · GitHub
;https://github.com/ahkon/MS-Office-COM-Basics/blob/master/Examples/Excel/Excel_Get.ahk

q:: ;excel - get hWnd from object
DetectHiddenWindows, On
;oXl := Excel_Get()
oXl := ComObjActive("Excel.Application")
hWnd := oXl.Hwnd
WinGet, vPID, PID, % "ahk_id " hWnd
WinGet, vPName, ProcessName, % "ahk_id " hWnd
MsgBox, % hWnd "`r`n" vPID "`r`n" vPName
oXl := ""
return
Link:
Application object (Excel) | Microsoft Docs
https://docs.microsoft.com/en-us/office/vba/api/excel.application%28object%29
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA

Post Reply

Return to “Ask for Help (v1)”