I have a script that opens an IE window using:
Set objIE = CreateObject("InternetExplorer.Application")
And then I'm doing:
intHWND = objIE.HWND
To get the window handle.
Then, in another part of the script, I want to check that the window
hasn't been closed before trying to modify its properties. Is there
some sort of dream function that works something like:
boolIEWindowOpen = GetWindowHandle(intHWND)
...that'll return true or false if a window with the supplied handle is
open or closed?
Here's hoping!
//Plankmeister
No, there isn't and it wouldn't be something to be relied upon if it
existed. Window handles are only guaranteed to be unique while the window is
open. One you close the window, the OS could assign a newly opened window
the same handle that the previous window had.
There is, if you write it (untested):
Function GetWindowHandle(intHWND)
Dim oWins, win
GetWindowHandle = True
Set oWins = CreateObject("Shell.Application").Windows
For Each win in oWins
If win.hwnd=intHWND Then Exit Function
Next
GetWindowHandle=False
End Function
However, there are two caveats. As I recollect, the HWND may change
without notice (somebody let me know if I am incorrect about that).
More importantly, this is a Huge amount of overhead for what you want
to do. Better to keep the ie object around and then see if you can
access its .hwnd property, properly wrapped in an On Error construct.
If the On Error doesn't kick in, then your IE is still alive.
Csaba Gabor from New York
Maybe you don't even need the hwnd.
Take a look at the "shell.application" object.
That object has a "windows" method, returning a collection
of InternetExplorer (and just plain windows explorer)
windows.
You can pick the InternetExplorer windows out of the
collection by testing for an "HTMLDocument" window type,
as in this snippet.
--- <snip> ---
' found on the vbs ng, original author may have been mikhar.
set oShell = createobject("Shell.Application")
c=-1
redim oWinArray(c)
for each oWin in oShell.windows
Msgbox(typename(oWin.document))
if typename(oWin.document) = "HTMLDocument" then
c = c + 1
redim preserve oWinArray(c)
set oWinArray(c) = oWin
end if
next
--- </snip> ---
That will give you an array of the IE windows,
containing a "window object" for each IE window.
You can pick out the window you want from the url,
or from the titlebar.
url: oWin.locationurl
title: oWin.document.title
And finally, if you perform the above IE inventory
at a later time, and the window you are looking for
is missing, it is safe to assume that it has been
closed.
Mit freundlichen Grüßen, jw