Link to home
Start Free TrialLog in
Avatar of bmcelroy7
bmcelroy7

asked on

Excel VBA: Login to website

I need to connect an excel spreadsheet to a website that requires login credentials. I have seen a few threads that discuss these but were unable to follow.  Could provide how I would go about logging in to "https://www.dinersclub.com/ptl/login.do"?  My ultimate goal would be to login and obtain statements and dump them into excel but maybe piece mealing would be best for now.

Thanks,
ASKER CERTIFIED SOLUTION
Avatar of jeverist
jeverist
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Put this code in a module as is.  Change "YourLoginName" and "YourPassword" to your real username and password, then try this just like it is.  (Be sure to leave the double quotes")  If it works, but the web address is wrong, then you will have to find the correct web address and put in.  The one you supplied did not take me to a login screen, and there were many to choose from depending on which country you wanted.  So I picked the one for US and Canada and used it.  If that is not correct, then change it to the one you need.  If it doesn't work, then give me the address of the exact web address you are on when you login.

Here is the code.  After you put it in a module...run the LogIn macro:

Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Private Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long

Sub LogIn()

Dim oIE As Object

    Set oIE = CreateObject("InternetExplorer.Application")
            oIE.Visible = True
            oIE.Navigate ("http://www.dinersclubus.com/dce_content/home")
            While oIE.Busy
               Sleep 2000
             Wend ' wait for page to load...
             Sleep 500
             SetForegroundWindow (oIE.hwnd)
   
    If Not oIE.Document.Forms(1).Elements("login") Is Nothing Then
             oIE.Document.Forms(1).Elements("login").Value = "YourLoginName"
             oIE.Document.Forms(1).Elements("password").Value = "YourPassword"
             oIE.Document.Forms(1).Elements("password").Select
    End If
Sleep 100
oIE.Document.Forms(1).submit

End Sub




:-)
ah
sorry jeverist...I should have refreshed first.  I didn't try yours yet...but looks good!
I would like to know what Document.All actually means though.  Does that like look for any element on any form with the name that you put in double quotes??
How does it know which element to use if there were 2 different forms with elements of the same name??  I am not a web page designer at all!  so maybe you can't have 2 different forms with elements of the same name.   I don't know.  I'm just trying to understand your code a little better.

:-)
ah