Hi everybody,
First of all, I'm kind of a newbie at VBA. I'm trying to log in to a website with a username and password using VBA, but the only pieces of code I know either input information into the fields by using getElementsById and then adjusting the .Value of the input field, or they loop through elements via "getElementsByTagName", but then I'm not sure how to adjust the value and put a username or password in there.
My code is below; you'll probably find the relics of many efforts including copy/paste, etc.
(BTW in case there's any confusion as to why there's no explicit password strings within the code, it's for security reasons, I don't actually want to store the login info anywhere--what I'm aiming for eventually is where I can press a button, a window pops up asking for name and password, and then once I type that in and press enter, the VBA code will just do everything on the website. A little ambitious, especially for a newbie, I know. I'm confident I can get all the other little parts done, and I did get this code working on Gmail, but I'm having trouble with this site in particular. (Knowing me, it's probably something small that I'm missing))
Sub ExpenseReportLogin()
If IsEmpty(Range("A1")) Then
Range("E1").Value = "Error: Fill In the username field!"
'Else
'Range("A1").Copy
End If
If IsEmpty(Range("A2")) Then
Range("E2").Value = "Error: Fill In the password field!"
End If
Dim MyHTML_Element As IHTMLElement
Dim MyURL As String
On Error GoTo Err_Clear
MyURL = "URL"
Set MyBrowser = New InternetExplorer
MyBrowser.Silent = True
MyBrowser.navigate MyURL
MyBrowser.Visible = True
Do
Loop Until MyBrowser.readyState = READYSTATE_COMPLETE
Set HTMLdoc = MyBrowser.document
' For Each MyHTML_Element In HTMLDoc.getElementsByName("USER")
' If MyHTML_Element.Type = "text" Then MyHTML_Element.Value = "(MyAwesomeUsername)": Exit For
' Next
'HTMLDoc.all.USER.Value = Range("A1")
HTMLdoc.all.activeElement.Paste
'HTMLDoc.all.Password.Value = Range("A2")
'Range("A1:A2").ClearContents
For Each MyHTML_Element In HTMLdoc.getElementsByTagName("input")
If MyHTML_Element.Type = "submit" Then MyHTML_Element.Click: Exit For
Next
Err_Clear:
If Err <> 0 Then
Err.Clear
Resume Next
End If
End Sub
This is the HTML form:
How 'bout it, guys? What am I doing wrong?
Bookmarks