Option Explicit
Public Enum IE_READYSTATE
Uninitialised = 0
Loading = 1
Loaded = 2
Interactive = 3
complete = 4
End Enum
Sub Test()
Dim ie As Object
Dim doc As HTMLDocument
Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = True
ie.navigate "http://www.thewebsite.com/loginpage.html"
Do Until ie.readyState = IE_READYSTATE.complete: DoEvents: Loop
Set doc = ie.document
'Now manipulate HTML objects in doc
End Sub
By using InternetExplorer object and HTML Object Library to populate the form field(s) and 'click' the login button. This shows the basics.
The specifics will depend on the particular web site. Search this forum for InternetExplorer.Application and HTMLdocument for similar code.Code:Option Explicit Public Enum IE_READYSTATE Uninitialised = 0 Loading = 1 Loaded = 2 Interactive = 3 complete = 4 End Enum Sub Test() Dim ie As Object Dim doc As HTMLDocument Set ie = CreateObject("InternetExplorer.Application") ie.Visible = True ie.navigate "http://www.thewebsite.com/loginpage.html" Do Until ie.readyState = IE_READYSTATE.complete: DoEvents: Loop Set doc = ie.document 'Now manipulate HTML objects in doc End Sub
I run the code in excel. it saids for "Dim doc As HTMLDocument"
user-defined type not defined.
'Need reference to Microsoft HTML Object Library. Select this in Tools - References in VB editor.
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
'< input class="TextBox" maxlength="15" name="UserName" size="12">
Set UserIdBox = PageForm.elements("UserName")
'Set the User Id
UserIdBox.Value = cUserID
'Get the password textbox
'< input class="TextBox" type="password" maxlength="10" name="Password" size="12">
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
'< input type="submit" value="Accept" name="selection">
'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
You need to set a reference to the HTML Object Library, as noted in comment at top of my code.I run the code in excel. it saids for "Dim doc As HTMLDocument"
user-defined type not defined.