vba login to internet

wxue

New Member
Joined
May 13, 2008
Messages
35
how can I login to a website using vba. I googled many examples but still failed. any one can help me? Thanks
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
By using InternetExplorer object and HTML Object Library to populate the form field(s) and 'click' the login button. This shows the basics.
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
The specifics will depend on the particular web site. Search this forum for InternetExplorer.Application and HTMLdocument for similar code.
 
Upvote 0
Thanks

By using InternetExplorer object and HTML Object Library to populate the form field(s) and 'click' the login button. This shows the basics.
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
The specifics will depend on the particular web site. Search this forum for InternetExplorer.Application and HTMLdocument for similar code.
 
Upvote 0
I run the code in excel. it saids for "Dim doc As HTMLDocument"
user-defined type not defined.

the web I need

https://pronto.firsthorizonwholesale.com/Index.asp
ID: *********
pw: *********<TABLE style="WIDTH: 94pt; BORDER-COLLAPSE: collapse" cellSpacing=0 cellPadding=0 width=125 border=0 x:str><COLGROUP><COL style="WIDTH: 94pt; mso-width-source: userset; mso-width-alt: 4571" width=125><TBODY><TR style="HEIGHT: 15.75pt" height=21><TD class=xl24 style="BORDER-RIGHT: #d4d0c8; BORDER-TOP: #d4d0c8; BORDER-LEFT: #d4d0c8; WIDTH: 94pt; BORDER-BOTTOM: #d4d0c8; HEIGHT: 15.75pt; BACKGROUND-COLOR: transparent" width=125 height=21 x:num="776912304"></TD></TR></TBODY></TABLE>
Thanks
 
Last edited by a moderator:
Upvote 0
Now that OP's userid and password have been removed, I can post my code (already sent by PM).
Code:
'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
 
Upvote 0
John, I have been following your code responses to the above query and have adapted them to meet my needs - just one quick question :

How do you CLOSE the IE Webpage when I have finished with it.

I am using your code to great effect and I am left with an open webpage. It would be a great help if you could advise of the code to close it down.

Many Thanks

Mulderman
 
Upvote 0

Forum statistics

Threads
1,225,155
Messages
6,183,208
Members
453,151
Latest member
Lizamaison

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top