Prev: Hidding a Div using VBscript
Next: End of Line
From: Mike Elder on 7 Sep 2009 16:03 I use this standard code at the beginning of my script to log in to the Comcast forums: With WScript.CreateObject("InternetExplorer.Application") .Visible = True .Navigate "http://forums.comcast.net/comcastsupport/" Do Until .ReadyState = 4 : WScript.Sleep 50 : Loop End With At this point I use SendKeys to send my login information. This works with no problems under Windows XP. Under Windows 7, at this point the focus is not on the Internet Explorer Application so the SendKeys goes somewhere else. With XP, this worked with IE6 or IE7. With Windows 7, I'm using IE8. Is the change because of the version of Windows or the version of IE, or something else? Any suggestions about what to do? If you feel you must, you can lecture me about SendKeys, but I assure you I'm very familiar with it and would appreciate not hearing it again. This script is just an example. Similar scripts fail with all the sites I have tried, not just Comcast.
From: mayayana on 7 Sep 2009 21:57 There's a window.focus method. That might be worth a try.
From: Mike Elder on 7 Sep 2009 23:21 "mayayana" <mayaXXyana(a)rcXXn.com> wrote: > There's a window.focus method. That might >be worth a try. Thanks for your suggestion. The only reference I could find to window.focus was in html code, which doesn't seem to apply. Please show me exactly how to include it with my sample script.
From: mayayana on 8 Sep 2009 00:02 > > There's a window.focus method. That might > >be worth a try. > > Thanks for your suggestion. The only reference I could find to > window.focus was in html code, which doesn't seem to apply. > Please show me exactly how to include it with my sample script. > You can access the document object in the IE instance, once you have a document loaded. This seems to work for me to bring IE to the top and set focus to it. My test code has a 3 second pause that gives me time to click on another window: Dim IE Set IE = CreateObject("InternetExplorer.Application") IE.Visible = True IE.Navigate "about:blank" Do Until IE.ReadyState = 4 WScript.Sleep 50 Loop WScript.sleep 3000 IE.document.parentwindow.focus Using "With CreateObject...." works but it's a confusing hack that compresses operations and it's really only useful to do a single thing with terse code. It's confusing because it obscures the creation of the object and leaves you with no variable to reference. In the sample you posted, anything you did with IE had to be within With/End With because you don't have a variable to reference the IE instance. By doing it as above you can access the IE instance with the variable "IE", and also access the DOM for the current document. Also, WScript.CreateObject works, but it's unnecessary unless you're going to trap events. The WScript version has a second, optional parameter to name an event variable. This doesn't really apply to what you're doing, but might be of interest for future reference: ----------------------------------- Set IE = WScript.CreateObject("InternetExplorer.Application", "IE_") ..... Sub IE_DocumentComplete(pDisp, URL) End Sub ---------------------- I don't remember offhand whether the paramters of DocumentComplete are usable in script. In any case, you can see how this works with this code: Dim IE, i2, i3 Set IE = WScript.CreateObject("InternetExplorer.Application", "IE_") IE.Visible = True For i2 = 1 to 3 i3 = i2 IE.Navigate "about:blank" WScript.sleep 4000 Next Sub IE_DocumentComplete(pDisp, URL) IE.document.parentwindow.focus IE.document.write "test " & i3 End Sub If you run that code you should get an IE window that says "test 1". If you then bring another window to the top, hiding IE, it will pop back up again 4 seconds later, with the text "test 2" to show that it has actually loaded a new page and fired the DocumentComplete event again. Likewise, in another 4 seconds it will show with "test 3". If you create the object with WScript, assign a variable for event subs, and keep the script alive, you can have event-driven code that keeps running. That works for any object with events. If you're not dealing with events you can just use CreateObject. (I don't know, though, whether there are any actual advantages to that. :)
From: Mike Elder on 8 Sep 2009 10:24
"mayayana" <mayaXXyana(a)rcXXn.com> wrote: > You can access the document object in the IE instance, >once you have a document loaded. This seems to work for >me to bring IE to the top and set focus to it. My test code >has a 3 second pause that gives me time to click on >another window: > >Dim IE >Set IE = CreateObject("InternetExplorer.Application") >IE.Visible = True >IE.Navigate "about:blank" >Do Until IE.ReadyState = 4 > WScript.Sleep 50 >Loop >WScript.sleep 3000 >IE.document.parentwindow.focus > > Using "With CreateObject...." works but >it's a confusing hack that compresses >operations and it's really only useful >to do a single thing with terse code. It's >confusing because it obscures the creation >of the object and leaves you with no variable >to reference. > In the sample you posted, anything you did with >IE had to be within With/End With because >you don't have a variable to reference the IE >instance. By doing it as above you can access >the IE instance with the variable "IE", and also >access the DOM for the current document. > > Also, WScript.CreateObject works, but it's >unnecessary unless you're going to trap >events. The WScript version has a second, >optional parameter to name an event variable. >This doesn't really apply to what you're doing, >but might be of interest for future reference: > >----------------------------------- >Set IE = WScript.CreateObject("InternetExplorer.Application", "IE_") >.... > >Sub IE_DocumentComplete(pDisp, URL) > >End Sub >---------------------- > > I don't remember offhand whether the paramters >of DocumentComplete are usable in script. In any >case, you can see how this works with this code: > >Dim IE, i2, i3 >Set IE = WScript.CreateObject("InternetExplorer.Application", "IE_") >IE.Visible = True >For i2 = 1 to 3 > i3 = i2 > IE.Navigate "about:blank" > WScript.sleep 4000 >Next > >Sub IE_DocumentComplete(pDisp, URL) > IE.document.parentwindow.focus > IE.document.write "test " & i3 >End Sub > > If you run that code you should get an IE window >that says "test 1". If you then bring another window >to the top, hiding IE, it will pop back up again 4 >seconds later, with the text "test 2" to show that >it has actually loaded a new page and fired the >DocumentComplete event again. Likewise, in >another 4 seconds it will show with "test 3". > > If you create the object with WScript, assign >a variable for event subs, and keep the script >alive, you can have event-driven code that keeps >running. That works for any object with events. >If you're not dealing with events you can just >use CreateObject. (I don't know, though, whether >there are any actual advantages to that. :) > mayayana, thank you very much. Your suggestion works and has solved my problem, and I'm grateful to you. I respectfully disagree about using With. Microsoft documentation says "With...End With allows you to perform a series of statements on a specified object without requalifying the name of the object" and "Doing this can make your procedures run faster and help you avoid repetitive typing." I also feel it makes the code cleaner and easier to read. I do agree that I shouldn't have used WScript.CreateObject. My final code, which now works well, is: With CreateObject("InternetExplorer.Application") .Visible = True .Navigate "http://forums.comcast.net/comcastsupport/" Do Until .ReadyState = 4 : WScript.Sleep 50 : Loop .document.parentwindow.focus End With Thanks again. |