Get elements from HTML form

I was trying to find out how we canget all the elements from html through vba, i think i got the answer (code from: “http://www.mrexcel.com/forum/showthread.php?p=1568942”). But i am not able to understand correctly. Can somebody help me how this works and what are the other functions used in this code.
selvavinaygam


Option Explicit

Public Enum IE_READYSTATE
    Uninitialised = 0
    Loading = 1
    Loaded = 2
    Interactive = 3
    complete = 4
End Enum

Sub Test()
    
    Const cURL = "https://pronto.firsthorizonwholesale.com/Index.asp"
    Const cUserID = "XXXX"     'REPLACE XXXX WITH YOUR USER ID
    Const cPwd = "YYYY"        'REPLACE YYYY WITH YOUR PASSWORD
    
    Dim ie As Object
    Dim doc As HTMLDocument
    Dim PageForm As HTMLFormElement
    Dim UserIdBox As HTMLInputElement
    Dim PasswordBox As HTMLInputElement
    Dim FormButton As HTMLInputButtonElement
    Dim Elem As IHTMLElement
    
    Set ie = CreateObject("InternetExplorer.Application")
    
    ie.Visible = True
    ie.navigate cURL
    
    'Wait for initial page to load
    
    Do While ie.busy Or Not ie.readyState = IE_READYSTATE.complete: DoEvents: Loop
           
    Set doc = ie.document
    
    'Output HTML tags to debug window
    
    Debug.Print "Login page: " & ie.LocationURL
    For Each Elem In doc.all
        'Debug.Print Elem.tagName
    Next

    'Get the only form on the page
    
    Set PageForm = doc.forms(0)
          
    'Get the User Id textbox
    '
    
    Set UserIdBox = PageForm.elements("UserName")
    
    'Set the User Id
    
    UserIdBox.Value = cUserID
    
    'Get the password textbox
    '
    
    Set PasswordBox = PageForm.elements("Password")
    
    'Set the password
    
    PasswordBox.Value = cPwd
    
    'Submit the form (like clicking the 'Submit' button) to navigate to next page
    
    PageForm.submit
    
    'Wait for the new page to load
    
    Do While ie.busy Or Not ie.readyState = IE_READYSTATE.complete: DoEvents: Loop
    
    'Get the HTML document of the new page
    
    Set doc = ie.document
    
    'Output HTML tags to debug window to prove this is the new page
    
    Debug.Print "Terms of Use page: " & ie.LocationURL
    For Each Elem In doc.all
        'Debug.Print Elem.tagName
    Next
    
    'The new page contains 'Terms of Use' conditions and an 'Accept' button within a form
    'Get the only form on the page
       
    Set PageForm = doc.forms(0)

    'Get the form submit button and click it to navigate to next page
    '
    'Note: unlike the login page, can't use PageForm.submit to submit this form because it doesn't have
    'a method="post" attribute
    
    Set FormButton = PageForm.elements("selection")
    FormButton.Click
    
    'Wait for the new page to load
    
    Do While ie.busy Or Not ie.readyState = IE_READYSTATE.complete: DoEvents: Loop
    
    'Get the HTML document of the new page
    
    Set doc = ie.document
    
    'Output HTML tags to debug window to prove this is the new page
    
    Debug.Print "Main Pronto page: " & ie.LocationURL
    For Each Elem In doc.all
        'Debug.Print Elem.tagName
    Next

End Sub

Advertisements

1 Comment »

  1. […] Get elements from HTML form […]

RSS feed for comments on this post · TrackBack URI

Leave a Reply

Please log in using one of these methods to post your comment:

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

%d bloggers like this: