Log in

View Full Version : API Calls or VBKey Commands in IE



chacanger
02-21-2011, 04:38 PM
Hi

Recently I was querying about how to automate certian things in Internet Explorer on this site, I have read up about it since and thought about this...

Is there was a way to use the VBkeyReturn to actually press enter in an Input Box rather than the ASCCI Number or if there was a way to use API call to press enter in an Input box, basically I want to use it in this example below but I'm not allowed to use the Buttons on the website as this is a test version of something with no buttons.

I have used SendKeys but they have a low success rate and I really mustn't use them for my code.

I would also need to get something to prevent the code going to fast, I used Sleep which works well, but sometimes the sleeps need to vary in time so I wondered can you get something to detect when the web page is ok to continue with the code.

This is the code which also uses the Internet Controls Reference, also you would need to disable the "Instant On" feature on the Google page...



Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Sub Auto()
Dim ie As InternetExplorer
Set ie = New InternetExplorer


ie.Navigate www.Google.co.uk (http://www.Google.co.uk)


Do Until ie.ReadyState = READYSTATE_COMPLETE
Loop


ie.Document.all("q").Value = "Chacanger"
SendKeys "ENTER", True
Sleep 200
ie.Document.all("q").Value = "Random Text"
SendKeys "ENTER", True
Sleep 200
ie.Document.all("q").Value = "More Random Text"
SendKeys "ENTER", True
Sleep 200


ie.Document.all("q").Value = "Another Random Text"
SendKeys "ENTER", True
Sleep 200
ie.Quit
Set ie = Nothing


End Sub


Thanks in advance for any help.

p45cal
02-23-2011, 02:02 AM
To answer the first part of your question:
ie.Document.all("f").submitand you don't need to turn off "Instant On"
For the second part, the following worked with Instant On:
Sub Auto()
Dim ie As InternetExplorer
Set ie = New InternetExplorer
ie.Visible = True
ie.Navigate "www.Google.co.uk"
Do Until ie.ReadyState = READYSTATE_COMPLETE: DoEvents: Loop
Do: DoEvents: Loop Until Not ie.Busy

ie.Document.getElementById("q").Value = "Chacanger"
ie.Document.getElementById("f").submit
Do Until ie.ReadyState = READYSTATE_COMPLETE: DoEvents: Loop
Do: DoEvents: Loop Until Not ie.Busy

ie.Document.getElementById("q").Value = "Random Text"
ie.Document.getElementById("f").submit
Do Until ie.ReadyState = READYSTATE_COMPLETE: DoEvents: Loop
Do: DoEvents: Loop Until Not ie.Busy

ie.Document.getElementById("q").Value = "Something"
ie.Document.getElementById("f").submit
Do Until ie.ReadyState = READYSTATE_COMPLETE: DoEvents: Loop
Do: DoEvents: Loop Until Not ie.Busy

ie.Document.getElementById("q").Value = "Automate"
ie.Document.getElementById("f").submit
Do Until ie.ReadyState = READYSTATE_COMPLETE: DoEvents: Loop
Do: DoEvents: Loop Until Not ie.Busy

'stop

ie.Quit
Set ie = Nothing
End Sub
[the automatic indentation has gone ape]
If Instant Off, use "gs" instead of "f". I think.

If I find how to detect whether it's on or off I'll post again.

p45cal
02-23-2011, 02:58 AM
Found out how to circumvent Instant On/Off changing the name of the form to submit: Don't use the name, it's a child of the "q" element, so the following works regardless of Instant On/Off:Sub Auto2()
Dim ie As InternetExplorer
Set ie = New InternetExplorer
ie.Visible = True
ie.Navigate "www.Google.co.uk"
Do Until ie.ReadyState = READYSTATE_COMPLETE: DoEvents: Loop
Do: DoEvents: Loop Until Not ie.Busy

Set theform = ie.Document.getElementByid("q")
theform.Value = "Chacanger"
theform.form.submit
Do Until ie.ReadyState = READYSTATE_COMPLETE: DoEvents: Loop
Do: DoEvents: Loop Until Not ie.Busy

Set theform = ie.Document.getElementByid("q")
theform.Value = "something"
theform.form.submit
Do Until ie.ReadyState = READYSTATE_COMPLETE: DoEvents: Loop
Do: DoEvents: Loop Until Not ie.Busy

Set theform = ie.Document.getElementByid("q")
theform.Value = "automate"
theform.form.submit
Do Until ie.ReadyState = READYSTATE_COMPLETE: DoEvents: Loop
Do: DoEvents: Loop Until Not ie.Busy

Set theform = ie.Document.getElementByid("q")
theform.Value = "can't think"
theform.form.submit
Do Until ie.ReadyState = READYSTATE_COMPLETE: DoEvents: Loop
Do: DoEvents: Loop Until Not ie.Busy

Stop

ie.Quit
Set ie = Nothing
End Sub

chacanger
02-23-2011, 05:23 AM
Found out how to circumvent Instant On/Off changing the name of the form to submit: Don't use the name, it's a child of the "q" element, so the following works regardless of Instant On/Off:Sub Auto2()
Dim ie As InternetExplorer
Set ie = New InternetExplorer
ie.Visible = True
ie.Navigate "www.Google.co.uk"
Do Until ie.ReadyState = READYSTATE_COMPLETE: DoEvents: Loop
Do: DoEvents: Loop Until Not ie.Busy

Set theform = ie.Document.getElementByid("q")
theform.Value = "Chacanger"
theform.form.submit
Do Until ie.ReadyState = READYSTATE_COMPLETE: DoEvents: Loop
Do: DoEvents: Loop Until Not ie.Busy

Set theform = ie.Document.getElementByid("q")
theform.Value = "something"
theform.form.submit
Do Until ie.ReadyState = READYSTATE_COMPLETE: DoEvents: Loop
Do: DoEvents: Loop Until Not ie.Busy

Set theform = ie.Document.getElementByid("q")
theform.Value = "automate"
theform.form.submit
Do Until ie.ReadyState = READYSTATE_COMPLETE: DoEvents: Loop
Do: DoEvents: Loop Until Not ie.Busy

Set theform = ie.Document.getElementByid("q")
theform.Value = "can't think"
theform.form.submit
Do Until ie.ReadyState = READYSTATE_COMPLETE: DoEvents: Loop
Do: DoEvents: Loop Until Not ie.Busy

Stop

ie.Quit
Set ie = Nothing
End Sub

Thanks p45cal for this, I'll see if I can get my ASPX to work with this and I'll post back with what results I get.

chacanger
02-23-2011, 11:03 AM
Thanks p45cal for this, I'll see if I can get my ASPX to work with this and I'll post back with what results I get.

I tried it against a hidden name within my ASPX page and it works!

Hurrah!!!:clap:

Is there a way you can refer to the Do...Loop rather than have to keep retyping lines with the same Do...Loop?

Thanks so much for your help.

p45cal
02-23-2011, 11:38 AM
[quote=chacanger]Is there a way you can refer to the Do...Loop rather than have to keep retyping lines with the same Do...Loop?[/quote
Put those lines in a sub:Sub waitTilReady(iiee)
Do Until iiee.ReadyState = READYSTATE_COMPLETE: DoEvents: Loop
Do: DoEvents: Loop Until Not iiee.Busy
End Sub
then either:
Sub Auto2()
Dim ie As InternetExplorer
Set ie = New InternetExplorer
ie.Visible = True
ie.Navigate "www.Google.co.uk"
waitTilReady ie

Set theform = ie.Document.getElementByid("q")
theform.Value = "Chacanger"
theform.form.submit
waitTilReady ie

Set theform = ie.Document.getElementByid("q")
theform.Value = "something"
theform.form.submit
waitTilReady ie

Set theform = ie.Document.getElementByid("q")
theform.Value = "automate"
theform.form.submit
waitTilReady ie

Set theform = ie.Document.getElementByid("q")
theform.Value = "can't think"
theform.form.submit
waitTilReady ie

Stop

ie.Quit
Set ie = Nothing
End Sub
or:
Sub Auto3()
Dim ie As InternetExplorer
Set ie = New InternetExplorer
ie.Visible = True
ie.Navigate "www.Google.co.uk"
waitTilReady ie
TextArray = Array("Chacanger", "something", "automate", "can't think")
For Each Strng In TextArray
Set theform = ie.Document.getElementByid("q")
theform.Value = Strng
theform.form.submit
waitTilReady ie
Next Strng

Stop

ie.Quit
Set ie = Nothing
End Sub