Consulting

Results 1 to 3 of 3

Thread: VBA to access website with passwords

  1. #1

    VBA to access website with passwords

    Hello,
    Trying to create a VBA in excel to link to a website. I was able to do it using the following VBA (changing all the fields in red (sample text) below. I got to the point where it entered the log in credentials in the fields, but could not get it to click the log in buttons. Not sure if different coding is needed below to get it to the point of it logging in. Also, when the website opens, it opens in a small window. Is there a way to get it to open the internet fully expanded?

    [vba]
    Public Sub Sample ()
    Const strURL_c As String = "Sample"
    Const strUsr_c As String = "Sample"
    Const strPwd_c As String = "Sample"
    Dim objIE As SHDocVw.InternetExplorer
    Dim ieDoc As MSHTML.HTMLDocument
    Dim tbxPwdFld As MSHTML.HTMLInputElement
    Dim tbxUsrFld As MSHTML.HTMLInputElement
    Dim btnSubmit As MSHTML.HTMLInputElement
    On Error GoTo Err_Hnd
    'Create Internet Explorer Object
    Set objIE = New SHDocVw.InternetExplorer
    'Navigate the URL
    objIE.Navigate strURL_c
    'Wait for page to load
    Do Until objIE.ReadyState = READYSTATE_COMPLETE: Loop
    'Get document object
    Set ieDoc = objIE.Document
    'Get username/password fields and submit button.
    Set tbxPwdFld = ieDoc.all.Item("Sample")
    Set tbxUsrFld = ieDoc.all.Item("Sample")
    Set btnSubmit = ieDoc.all.Item("Sample")
    'Fill Fields
    tbxUsrFld.Value = strUsr_c
    tbxPwdFld.Value = strPwd_c
    'Click submit
    btnSubmit.Click
    'Wait for page to load
    Do Until objIE.ReadyState = READYSTATE_COMPLETE: Loop
    Err_Hnd: '(Fail gracefully)
    objIE.Visible = True
    End Sub
    [/vba]

  2. #2
    VBAX Mentor Teeroy's Avatar
    Joined
    Apr 2012
    Location
    Sydney, Australia
    Posts
    414
    Location
    Hi krissi18d,

    Welcome to the forum. Your code pretty much worked for me but logging in to a site is quite dependent on the site coding. You can look at the source code and sometimes looping through the elements in test code can help too.

    Using logging in to the KB of VBA Express as an example I got the following to work. The addition of window positioning and sizing is at the bottom of the code.

    [vba]Public Sub Sample()
    Const strURL_c As String = "http://www.vbaexpress.com/kb/login.php"
    Const strUsr_c As String = "Teeroy"
    Const strPwd_c As String = "xxxxxxxxxxxxxxxxxxxxxxxxx"
    Dim objIE As SHDocVw.InternetExplorer
    Dim ieDoc As MSHTML.HTMLDocument
    Dim tbxPwdFld As MSHTML.HTMLInputElement
    Dim tbxUsrFld As MSHTML.HTMLInputElement
    Dim btnSubmit As MSHTML.HTMLInputElement
    On Error GoTo Err_Hnd
    'Create Internet Explorer Object
    Set objIE = New SHDocVw.InternetExplorer
    'Navigate the URL
    objIE.Navigate strURL_c
    'Wait for page to load
    Do Until objIE.ReadyState = READYSTATE_COMPLETE: Loop
    'Get document object
    Set ieDoc = objIE.Document

    'Get username/password fields and submit button.
    Set tbxPwdFld = ieDoc.all.Item("pwd")
    Set tbxUsrFld = ieDoc.all.Item("username")
    Set btnSubmit = ieDoc.all.Item("submit")
    'Fill Fields
    tbxUsrFld.Value = strUsr_c
    tbxPwdFld.Value = strPwd_c
    'Click submit
    btnSubmit.Click
    'Wait for page to load
    Do Until objIE.ReadyState = READYSTATE_COMPLETE: Loop
    Err_Hnd: '(Fail gracefully)
    objIE.Visible = True
    objIE.Left = 100
    objIE.Top = 100
    objIE.Height = 600
    objIE.Width = 800
    End Sub
    [/vba]

    Hope this helps.
    _________________________________________________________________________
    "In theory there is no difference between theory and practice. In practice there is." - Chuck Reid

    Any day you learn something new is a day not wasted.

  3. #3
    or simply:
    [VBA]
    Sub M_snb()
    ' reference to Microsoft Internet Controls
    With New InternetExplorer
    .Navigate "http://www.vbaexpress.com/kb/login.php"
    Do
    DoEvents
    Loop Until .ReadyState = 4

    With .Document
    .all("username").innerText = "snb"
    .all("pwd").innerText = "*****"
    .all("submit").Click
    End With

    Do
    DoEvents
    Loop Until .ReadyState = 4
    .Visible = True
    End With
    End Sub
    [/VBA]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •