Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > VB Script

Vista - Script problem with Windows 7

Reply
 
Old 09-07-2009   #1 (permalink)
Mike Elder


 
 

Script problem with Windows 7

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.


My System SpecsSystem Spec
Old 09-07-2009   #2 (permalink)
mayayana


 
 

Re: Script problem with Windows 7

There's a window.focus method. That might
be worth a try.




My System SpecsSystem Spec
Old 09-07-2009   #3 (permalink)
Mike Elder


 
 

Re: Script problem with Windows 7

"mayayana" <mayaXXyana@xxxxxx> wrote:
Quote:

> 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.

My System SpecsSystem Spec
Old 09-08-2009   #4 (permalink)
mayayana


 
 

Re: Script problem with Windows 7

> > There's a window.focus method. That might
Quote:
Quote:

> >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.






My System SpecsSystem Spec
Old 09-08-2009   #5 (permalink)
Mike Elder


 
 

Re: Script problem with Windows 7

"mayayana" <mayaXXyana@xxxxxx> wrote:
Quote:

> 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.

My System SpecsSystem Spec
Old 09-08-2009   #6 (permalink)
mayayana


 
 

Re: Script problem with Windows 7

Quote:

> 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.
Yes. I like With/End With in general. It's
more efficient because it doesn't have to keep
resolving the object reference on each line.
I was just talking about using it with CreateObject
It's especially confusing to new people who
are having trouble following which variable is
which in the first place. But of course, it's really
a matter of personal preference.

In any case, being "respectfully" disagreed
with is quite a pleasant way to start my day.
I don't remember the last time I saw that
particular courtesy.



My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Problem with login script General Discussion
Logon script problem Vista networking & sharing
problem passing args to script 'There is no script engine for file extenstion' VB Script
Windows Script Host has no script engine for ".js" files Vista General
Problem with convert from WSH-script PowerShell


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46