Search code examples
htmlvbainternet-explorer

VBA to site login


i try to use the VBA code found from Login to a website using VBA

unfortunately i cannot access it as VBA is prompting some error message.

error VBA received Run time error '91' Object variable or With Block variable not set

Sub login()

    Const Url$ = "https://kn.slync.app/login"

    Dim UserName As String
    Dim Password As String
    Dim LoginData As Worksheet

    Set LoginData = ThisWorkbook.Worksheets("Sheet1")
    
    UserName = LoginData.Cells(1, 2).Value
    Password = LoginData.Cells(2, 2).Value

    Dim ie As Object
    Set ie = CreateObject("InternetExplorer.Application")

    With ie

        .navigate Url
        ieBusy ie
        .Visible = True

    Dim oLoging As Object
    Dim oPassword As Object

    Set oLogin = .document.getElementsByName("username")(0)
    Set oPassword = .document.getElementsByName("password")(0)
    
        oLogin.Value = UserName
        oPassword.Value = Password
        .document.forms(0).submit

    End With

End Sub


Sub ieBusy(ie As Object)
    Do While ie.Busy Or ie.readyState < 4
        DoEvents
    Loop
End Sub

Amendment made, Username and Password is populate as below screenshot enter image description here

This is the error message received on next Line .getElementsByClassName("ant-row-flex ant-row-flex-center ant-row-flex-middle")(0).Click

enter image description here


Solution

  • I see what the issue you're facing now. Set value actually doesn't trigger the inputs as having values. The values of username and password were still considered empty. In this situation, we can use SendKeys to simulate user input then the events of the inputs will be triggered successfully.

    You can change the code according to below:

    With document
         .getElementById("username").Focus
         SendKeys (UserName)
         Application.Wait (Now + TimeValue("00:00:01"))
         .getElementById("password").Focus
         SendKeys (Password)
         Application.Wait (Now + TimeValue("00:00:01"))
         .getElementsByClassName("ant-row-flex ant-row-flex-center ant-row-flex-middle")(0).Click
    End With
    

    I test with a fake username/password and the message now is "Invalid username/password" which showing it works:

    enter image description here