PDA

View Full Version : VBA to access website with passwords



krissi18d
03-19-2013, 11:37 AM
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?


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

Teeroy
03-20-2013, 07:42 PM
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.

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


Hope this helps.

snb
03-21-2013, 01:53 AM
or simply:

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