Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to get object reference to existing IE window

Status
Not open for further replies.

mkrausnick

Programmer
Apr 2, 2002
766
US
When automating IE to run a process, under certain conditions the web site I deal with pops up a modal window with an error message. That causes my app to hang of course, because navigation to the intended page is interrupted.

What I want to do is find the modal window and have my app click OK in it so it goes away and my app can continue.

Using Chris Chamberlain's code from Thread1251-895526 with modification I am able to get the handle of the 2nd IE window, but then what? How do I objectify it so I can issue a oIE.Submit() or similar command?

Thanks for your help!


Mike Krausnick
Dublin, California
 
Mike,

Good question. I'm sure what you want to do is possible, but unfortunately I don't know how.

However, if nobody gives you a better answer, you might try this (off the top of my head and completely untested):

Code:
oWSH = CREATEOBJECT("wscript.shell")
IF oWSH.AppActivate("Some Caption")
  oWSH.SendKeys("{ENTER}")
ELSE
  * Cannot find the specified window
ENDIF

"Some caption" is the caption of the window in question (not case sensitive).

In this example, the code is sending an Enter key to the window. If the window's Submit button has focus at this point, the effect will be to click the button. You might need to send "{TAB}" or other characters to put focus on the appropiate button. Alternatively, you could send Alt+F4 to close the window.

As I said earlier, if anyone can give you a better idea, go with that.

Mike


__________________________________
Mike Lewis (Edinburgh, Scotland)

My Visual FoxPro site: www.ml-consult.co.uk
 
Need to use windows API FindWindow and FindWindowEx. Here are a couple snips I use for IE download management.

I believe I got a fair amount of this code from Craig Boyd's web site, but I can't quite remember.

Code:
wndDialog = FindWindow( 0, [Download File])
wndDialogFail = FindWindow( 0, "Microsoft Internet Explorer" )
lnTries = 0
DO WHILE wndDialog <> 0 AND wndDialogFail = 0 AND lnTries < 20
 wndDialog = FindWindow( 0, [Download File])
 sleep(1000)
 lnTries =lnTries +1
ENDDO

IF wndDialogFail > 0
 MESSAGEBOX("Failed!")
 CANCEL
ENDIF

Code:
PROCEDURE getfiles
LPARAMETERS sURLandFile
LOCAL sURLandFile, nRet

WAIT WINDOW NOWAIT lcUrl + lcFile
DECLARE INTEGER FindWindowEx IN user32;
 INTEGER  hwndParent,;
 INTEGER  hwndChildAfter,;
 STRING @ lpszClass,;
 STRING @ lpszWindow

sleep(200)

DECLARE DoFileDownload IN shdocvw.DLL STRING lpszFile

sleep(200)

IF !EMPTY(sURLandFile)
 nRet=DoFileDownload(STRCONV(sURLandFile,5))
ELSE
 MESSAGEBOX("Function returned an empty length string.",16,"Something is Wrong")
ENDIF

lcMessage = SaveAs_Save([Save As], [Save], [])
RETURN
ENDPROC

FUNCTION SaveAs_Save
LPARAMETERS pcTitle, pcButtonCaption, lcMessage

#DEFINE WM_COMMAND   0x0111
#DEFINE WM_LBUTTONDOWN 0x0201
#DEFINE WM_LBUTTONUP 0x0202

DECLARE INTEGER FindWindow IN user32;
 STRING lpClassName,;
 STRING lpWindowName

DECLARE INTEGER FindWindowEx IN user32;
 INTEGER hwndParent,   ;
 INTEGER hwndChildAfter, ;
 STRING lpszClassName,  ;
 STRING lpszWindowCaption

DECLARE INTEGER SendMessage IN user32;
 INTEGER HWND,;
 INTEGER Msg,;
 INTEGER wParam,;
 INTEGER LPARAM

nTries = 0
wndDialog = 0
wndDialogFail = 0
nStart = SECONDS()
DO WHILE wndDialog = 0 AND wndDialogFail =0 AND nTries<30
 wndDialog = FindWindow( 0, pcTitle )
 wndDialogFail = FindWindow( 0, "Microsoft Internet Explorer" )
 nTries = nTries + 1
 sleep(500)
ENDDO

IF wndDialogFail > 0
 MESSAGEBOX("Failed")
 CANCEL
ENDIF

IF SECONDS()-nStart>=30
 RETURN [Time Out.]
ENDIF

wndButton = 0

IF wndDialog > 0
* Sometimes Message WM_COMMAND 0x00110818 isn't enough,
*  if you MUST click "Cancel" or "Yes", etc
*  then provide a button caption
 IF VARTYPE(pcButtonCaption)='C'
  wndButton = FindWindowEx( wndDialog, 0, 'Button', ;
   pcButtonCaption )
  IF wndButton > 0
   SendMessage(wndButton, WM_LBUTTONDOWN, 1, 0x00120025 )
   SendMessage(wndButton, WM_LBUTTONUP, 0, 0x00120025 )
* Button Clicked message
   SendMessage(wndDialog, WM_COMMAND,   3, wndButton  )
  ENDIF
 ENDIF

 SendMessage(wndDialog, WM_COMMAND, 1, 0x00110818 )
ENDIF
RETURN STR(wndDialog,10)+STR(wndButton,10)
ENDFUNC
 
Thanks for the responses. I discovered that yes, I can generally close a window using an API call, but of course, not THIS window. I determined that this window is raised by a javascript ALERT statement. Once the VFP line of code executes that clicks 'Submit' on my form with incorrect data, the JS code determines the error, constructs a message, and issues an ALERT statement. The VFP code freezes at that point. Because the window is modal, I can't even switch to the debugger to step to the next statement until I click OK or close the window.

Maybe I'll try a timer or something, but I'm not optimistic at this point.

Mike Krausnick
Dublin, California
 
Easy to do with IE if you know the window's caption (or a reliable substring), but not if browser is something else. Are you in an IE only environment?
 
A bit messy, but works.

Code:
cMyString="goog"

DECLARE Sleep IN Win32API INTEGER

oIE = CREATEOBJECT("internetexplorer.application")
oIE.navigate2("[URL unfurl="true"]www.google.com")[/URL]
oIE.visible = .t.
*SET STEP ON 

DO WHILE oIE.ReadyState <> 4
	sleep(500)
ENDDO

oShell = CREATEOBJECT("shell.application")
FOR EACH Win IN oShell.Windows
	?Win.LocationName,Win.LocationURL
	
	IF m.cMyString$Win.LocationURL &&OneOfAbove
		Win.Quit
	ENDIF 
ENDFOR 

oShell = NULL
 
On a quick review of what a javascript alert looks like, I revert to my original post. The SaveAs_Save function should be adaptable.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top