How To: Get the handle of a window from a process ID

K

Kevin McCartney

Hi, many thanks to 'Dan Artuso' for the original help but
I need a little it more code because I need to use the API
call IsWindowVisible and DestroyWindow but both need the
window handle, thus Dan showed me how to get the process
ID but I need the hwnd as well. Thus if some knows of a
function that retrieves the hwnd of a window (does a
hidden window have a hwnd?). The background is that I
export data to an Excel workbook and use 'Dim xlApp as New
Excel.Application' thus each time the procedure is
executed a new Excel Application is used, but when the
user closes the workbook window the Excel Application
becomes hidden and remains as a process. Therefore I need
to Destroy the 'hidden' windows. Alternatively if someone
has a code example that would allow me to use an Excel
session that is currently open this would also help. The
code also needs to able to run in Access 97.

Thanks in advance
regards
KM
 
M

Michel Walsh

Hi,


Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal
lpClassName As String, ByVal lpWindowName As String) As Long



where we generally use vbNullString for the class name. The window has to
exist. The supplied name must be the EXACT name you look for (if the end
user can customize it, or if a * is append to it in some cases, you have to
use the customized caption, or the name with the * append to it, as
example). FindWindow does not search for child windows, FindWindowEx do. See
www.allapi.net


Hoping it may help,
Vanderghast, Access MVP
 
K

Kevin McCartney

Hi Michel
Thanks for your help but what happens if I have several
Excel sessions running how do I now which one is the one
I'm looking for to destroy?

I'd really like to be able to pass the Process ID to a
function. Do you know of function that allows this?

Thanks for your time
KM
 
T

TC

Kevin McCartney said:
Hi, many thanks to 'Dan Artuso' for the original help but
I need a little it more code because I need to use the API
call IsWindowVisible and DestroyWindow but both need the
window handle, thus Dan showed me how to get the process
ID but I need the hwnd as well. Thus if some knows of a
function that retrieves the hwnd of a window (does a
hidden window have a hwnd?). The background is that I
export data to an Excel workbook and use 'Dim xlApp as New
Excel.Application' thus each time the procedure is
executed a new Excel Application is used, but when the
user closes the workbook window the Excel Application
becomes hidden and remains as a process. Therefore I need
to Destroy the 'hidden' windows.

Alternatively if someone
has a code example that would allow me to use an Excel
session that is currently open this would also help. The
code also needs to able to run in Access 97.

dim oXL as object
set oXL = GetObject ("Excel.Application")

HTH,
TC
 

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