IE (Internet Explorer) Automation using Excel VBA

Manostaxx

The text that follows is owned by the site above referred.

Here is only a small part of the article, for more please follow the link

SOURCE: http://www.excely.com/excel-vba/ie-automation.shtml

Example below tries to give web automation using IE (internet explorer). VBA macro drives internet explorer using its DOM properties and methods. The macro allows to interact with web page controls (text fields and buttons). The example opens web site www.excely.com and fill a search form to find “excel vba” text.

First we open manually web site and found html-code of search form:

<!-- searchbox START --> 

To automate searching we need:

  • Create Internet Explorer object
  • Load web site www.excely.com
  • Find 2 input tags:
    • Text field
      <input type="text" class="textfield" name="s" size="24" value="" />
    • Button
      <input type="submit" class="button" value="" />
  • Set searching text and click button
  • Wait while IE loading
  • Clean up objects

IE Autiomation source code:

Private Sub IE_Autiomation()
    Dim i As Long
    Dim IE As Object
    Dim objElement As Object
    Dim objCollection As Object
 
    ' Create InternetExplorer Object
    Set IE = CreateObject("InternetExplorer.Application")
 
    ' You can uncoment Next line To see form results
    IE.Visible = False
 
    ' Send the form data To URL As POST binary request
    IE.Navigate "http://www.excely.com/"
 
    ' Statusbar
    Application.StatusBar = "www.excely.com is loading. Please wait..."
 
    ' Wait while IE loading...
    Do While IE.Busy
        Application.Wait DateAdd("s", 1, Now)
    Loop
 
    ' Find 2 input tags:
    '   1. Text field
    '   <input type="text" class="textfield" name="s" size="24" value="" />
    '
    '   2. Button
    '   <input type="submit" class="button" value="" />
    
    Application.StatusBar = "Search form submission. Please wait..."
 
    Set objCollection = IE.document.getElementsByTagName("input")
 
    i = 0
    While i < objCollection.Length
        If objCollection(i).Name = "s" Then
 
            ' Set text for search
            objCollection(i).Value = "excel vba"
 
        Else
            If objCollection(i).Type = "submit" And _
               objCollection(i).Name = "" Then
 
                ' "Search" button is found
                Set objElement = objCollection(i)
 
            End If
        End If
        i = i + 1
    Wend
    objElement.Click    ' click button to search
    
    ' Wait while IE re-loading...
    Do While IE.Busy
        Application.Wait DateAdd("s", 1, Now)
    Loop
 
    ' Show IE
    IE.Visible = True
 
    ' Clean up
    Set IE = Nothing
    Set objElement = Nothing
    Set objCollection = Nothing
 
    Application.StatusBar = ""
End Sub

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s