Thursday, October 15, 2009

Automate Internet Explorer from VB Script

Control Internet Explorer from VB Script using the Internet Explorer COM object.  The code below shows how to do the following:

  • Open Internet Explorer
  • Navigate to different URL’s
  • Make Internet Explorer visible/invisible
  • Populate a textbox on a web page
  • Click a button on a web page

You can use scripts like this to automate anything you do repeatedly to save time, such as: Web logins, screen captures, fake traffic or vote in internet polls.  I used a script similar to this one to enter my self in a drawing for a new house, I set the script up on the windows scheduler to run once a day since the contest specified that you were allowed to enter once daily. I didn't win!

Code – *.vbs

Dim oIE	
IeNew("www.google.com")

' Put your name into the "Search" textbox
' Notice below that the textbox's name is "q"
oIE.document.all.q.value = "Rick Casady"

WScript.Sleep(100)

' Click the Search button
' Notice below that the button's name is "q"
oIE.document.all.btnG.click()


' Create a Internet Explorer COM object and then Navigate the Navigate sub
' Also sets the Internet explorer to be visible
Public Sub IeNew(ByVal strURL)
Set oIE = CreateObject("InternetExplorer.Application")
Navigate(strURL)
oIE.Visible = True
End Sub

' Navigates to the URL provided then waits for IE to complete.
Public Sub Navigate(ByVal strURL)
oIE.Navigate(strURL)
Do
WScript.Sleep(200)
Loop Until Not oIE.Busy
End Sub

No comments: