Login to webpage.

User avatar
adam
SilverLounger
Posts: 2195
Joined: 23 Feb 2010, 12:07

Login to webpage.

Post by adam »

Hi anyone,

I'm using the following code to login to a web page.

Code: Select all

Sub login()

    Const Url$ = "https://abc.com/"

    Dim UserName As String, Password As String, LoginData As Worksheet
    Set LoginData = ThisWorkbook.Worksheets("Sheet3")
    UserName = LoginData.Cells(1, "B").Value
    Password = LoginData.Cells(2, "B").Value

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

    With ie

        .navigate Url
        ieBusy ie
        .Visible = True

        Dim oLogin As Object, oPassword As Object
        Set oLogin = .Document.getElementsByName("id")(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
The code does login me into the page. But Each time I run the code I get the runtime error91;
Object variable or with block variable not set highlighting the line oLogin.Value = UserName

How can I overcome this? any help on this would be kindly appreciated. Thanks in advance.
Best Regards,
Adam

User avatar
HansV
Administrator
Posts: 71983
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Login to webpage.

Post by HansV »

Not sure you'll find help here. I. for one, have no idea - sorry!
Regards,
Hans

User avatar
adam
SilverLounger
Posts: 2195
Joined: 23 Feb 2010, 12:07

Re: Login to webpage.

Post by adam »

Thanks for the reply.

I don't get the error when I run the code for the first time. But If I close the explorer without logging out and then again run the code I get the error.
Best Regards,
Adam

User avatar
HansV
Administrator
Posts: 71983
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Login to webpage.

Post by HansV »

Perhaps .Document.getElementsByName("id")(0) is not available anymore after logging in?

(Just guessing, I don't know anything about this)
Regards,
Hans

User avatar
adam
SilverLounger
Posts: 2195
Joined: 23 Feb 2010, 12:07

Re: Login to webpage.

Post by adam »

Thanks for the reply Hans.
Best Regards,
Adam

BilasTasol
NewLounger
Posts: 24
Joined: 09 Feb 2010, 17:25
Location: Rhondda Cynon Taff, S. Wales

Re: Login to webpage.

Post by BilasTasol »

Have you tried Selenium? Wise Owl Training has a playlist at https://www.youtube.com/watch?v=FoxWcvZzYVk . It allows you to access and manipulate websites through VBA using a variety of browsers.

Tony.
Regards,

Tony.

http://www.sylviahugheswilliams.com" onclick="window.open(this.href);return false;