Macro to autologin to Website

benpey

New Member
Joined
Jan 21, 2016
Messages
2
Hi I am having issues with a macro that I am hoping to use to login into a website (https://www.kewilltransport.net/tms/servlet/Login).
I have been able to successfully execute this code on other websites that I use, but keep getting the following error message when I get to line 6 (where it tries to find the logon tbox using GetElement)," Run-time error '13': Type mismatch "

From what I've read online I think it has something to do with the website using frames, instead divs (I'm a noob so apologies if I'm way off)

Here's the code I'm using:
Code:
Sub LogonScript()


Const READYSTATE_COMPLETE = 4




Set IE = CreateObject("InternetExplorer.Application")


    IE.Visible = 1
    
    IE.navigate "https://kewilltransport.net/tms/servlet/Login"
    




 Application.Wait (Now + TimeValue("0:00:02"))




 Set Login = IE.document.getElementByID("f_username") 'username textbox
 Login.Value = "USERNAME" 'where I enter by username
 
 Set Password = IE.document.getElementByID("f_password") 'password textbox
 Password.Value = "PASSWORD" ' Where I enter my password
 
 Set LogOn = IE.document.getElementByID("f_btn2") 'login button name
 LogOn.Click
 
End Sub

Thanks!
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
Welcome to MrExcel forums.

Yes, the site uses frames, actually nested frames to complicate things. You need to access the HTMLDocument within each frame, and then you can access the input elements, like this:

Code:
Sub LogonScript()

    Const READYSTATE_COMPLETE = 4

    Dim IE As Object
    Dim AccountNumber As Object, Username As Object, Password As Object, LogOn As Object
    Dim HTMLdoc As Object, frame As Object
    
    Set IE = CreateObject("InternetExplorer.Application")
    
    With IE
        .Visible = True
        .navigate "https://kewilltransport.net/tms/servlet/Login"
        While .Busy Or .ReadyState <> READYSTATE_COMPLETE: DoEvents: Wend
        Set HTMLdoc = .document
    End With
    
    Set frame = HTMLdoc.frames("_sliverFrame")
    Set HTMLdoc = frame.document
    Set frame = HTMLdoc.frames("MAIN")
    Set HTMLdoc = frame.document
    
    '< input name="f_accountNumber" maxlength="20" class="text" type="text">
    
    Set AccountNumber = HTMLdoc.getElementsByName("f_accountNumber")(0)
    AccountNumber.Value = "123456"
    
    Set Username = HTMLdoc.getElementsByName("f_username")(0) 'username textbox
    Username.Value = "USERNAME" 'where I enter my username
    
    Set Password = HTMLdoc.getElementsByName("f_password")(0) 'password textbox
    Password.Value = "PASSWORD" ' Where I enter my password
    
    Set LogOn = HTMLdoc.getElementById("f_btn2") 'login button name
    LogOn.Click
    
    With IE
        While .Busy Or .ReadyState <> READYSTATE_COMPLETE: DoEvents: Wend
    End With
    
End Sub
 
Last edited:
Upvote 0
John,
Thanks for you help the code worked great, but I did have to make one change to the deceleration statements on line 4 (HTMLdoc). Do you happen to know if this was due to a lack of checking one of the references in the VBAProject tools or excel version issue? Either way thanks again!
Here's the code I used:
Code:
Code:
Sub LogonScript()

    Const READYSTATE_COMPLETE = 4


    Dim IE As Object
    Dim AccountNumber As Object, Username As Object, Password As Object, LogOn As Object
    Dim HTMLdoc As MSHTML.HTMLDocument  'INSTEAD OF DIM HTMLdoc AS Object
    Dim frame As Object
    
    Set IE = CreateObject("InternetExplorer.Application")
    
    With IE
        .Visible = True
        .navigate "https://somesite/Login"
        While .Busy Or .readyState <> READYSTATE_COMPLETE: DoEvents: Wend
        Set HTMLdoc = .document
    End With
    
    Set frame = HTMLdoc.frames("_sliverFrame")
    Set HTMLdoc = frame.document
    Set frame = HTMLdoc.frames("MAIN")
    Set HTMLdoc = frame.document
    
    '< input name="f_accountNumber" maxlength="20" class="text" type="text">
    
    Set AccountNumber = HTMLdoc.getElementsByName("f_accountNumber")(0)
    AccountNumber.Value = "xxxxxx"
    
    Set Username = HTMLdoc.getElementsByName("f_username")(0) 'username textbox
    Username.Value = "xxxxxx" 'where I enter my username
    
    Set Password = HTMLdoc.getElementsByName("f_password")(0) 'password textbox
    Password.Value = "xxxxxx" ' Where I enter my password
    
    Set LogOn = HTMLdoc.getElementById("f_btn2") 'login button name
    LogOn.Click
    
    With IE
        While .Busy Or .readyState <> READYSTATE_COMPLETE: DoEvents: Wend
    End With
    
End Sub
 
Last edited by a moderator:
Upvote 0

Forum statistics

Threads
1,215,143
Messages
6,123,282
Members
449,094
Latest member
GoToLeep

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