Question

First of all, I can not use any different software/OS for this other than Win XP and IE 8.

Secondly I can only use VB Script to accomplish this.

Thirdly the code I am trying:

Set WshShell = WScript.CreateObject("WScript.Shell")
WScript.Sleep 500
Set objShell = CreateObject("WScript.Shell")
WScript.Sleep 500
objShell.AppActivate "title of page - Windows Internet Explorer" 


WScript.Sleep 500
WshShell.SendKeys "{TAB}"
WScript.Sleep 500
objShell.SendKeys "{DELETE}{DELETE}{DELETE}{DELETE}{DELETE}{DELETE}{DELETE}{DELETE}{DELETE}{DELETE}"
WScript.Sleep 500
WshShell.SendKeys "05/30/2014"
WScript.Sleep 500
WshShell.SendKeys "{TAB}"
WScript.Sleep 500
WshShell.SendKeys "{TAB}"
WScript.Sleep 500
objShell.SendKeys "{DELETE}{DELETE}{DELETE}{DELETE}{DELETE}{DELETE}{DELETE}{DELETE}{DELETE}{DELETE}"
WScript.Sleep 500
WshShell.SendKeys "05/31/2014"
WScript.Sleep 500
WshShell.SendKeys "{TAB}"
WScript.Sleep 500
WshShell.SendKeys "{TAB}"
WScript.Sleep 500
WshShell.SendKeys "{TAB}"
WScript.Sleep 500
WshShell.SendKeys "{TAB}"
WScript.Sleep 500
WshShell.SendKeys "{DOWN}"
WScript.Sleep 500  
WshShell.SendKeys "{DOWN}"
WScript.Sleep 500
WshShell.SendKeys "{DOWN}"
WScript.Sleep 500
WshShell.SendKeys "{DOWN}"
WScript.Sleep 500


x=msgbox("MACRO HAS FINISHED RUNNING!!!" ,0, "MACRO HAS FINISHED RUNNING!!!")

The problem is that it seems to always skip the first text box, it is trying to enter in on a webpage, on line six where I have indicated WshShell.SendKeys "05/30/2014" it just will not enter into the text box on the webpage, I have tried messing around with the code, using WShShell rather than WScript, running the code differently, changing the sleep from 500 to other various times, is there something I am missing here overall as to why it does enter into the first text box on the webpage? Something I need to input in my code to make it work, like a variable or something?

Please note where I have put "title of page" near the top I have omitted the real title of the page for security reasons.

Was it helpful?

Solution

Don't use SendKeys for GUI automation. It's extremely, terribly, awfully unreliable. SendKeys is like typing blindfold — you can't be sure you send keys to the correct object, because other windows can steal the input focus from your target at any time.

In your case, you can use Internet Explorer's scripting object - InternetExplorer.Application. It lets you access the web page's DOM (Document Object Model), so you can enter data, click links and buttons, submit forms and navigate web pages programmatically.

Dim objIE
Set objIE = CreateObject("InternetExplorer.Application")
objIE.Visible = True

objIE.Navigate "https://twitter.com/search-home"

While objIE.Busy
  WScript.Sleep 100
Wend

objIE.Document.getElementById("search-home-input").value = "haiku"
objIE.Document.getElementsByClassName("search-btn")(0).click

To connect to an already opened web page, you can use the following function instead of CreateObject("InternetExplorer.Application"):

Set objIE = GetIE("Twitter / Search")
...

Function GetIE(Title)
  Dim objShell, objIE
  Set objShell = CreateObject("Shell.Application")
  For Each objIE In objShell.Windows
    If InStr(objIE.LocationName, Title) > 0 Then
      Set GetIE = objIE
      Exit Function
    End If
  Next
End Function

This code searches for a web page by its title. To search by URL instead of title, replace

If InStr(objIE.LocationName, Title) > 0 Then

with

If InStr(objIE.LocationURL, Title) > 0 Then
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top