I am fairly new to VBA and trying to log in to a website. I have gotten the code to get me to the website then look up the input names for the username and password elements. This was important because everytime the page opens the names are slightly different so I can't just inspect the element name and use that as a fixed value.
The username input name always starts with "txt_1_" then some numbers and the password goes "txt_2_" and some numbers. That is the reason I have the if statement that looks for the names similar to those.
The error I am currently receiving is "Run-time error '438': Object doesn't support this property or method"
Below is what I have so far:
Sub login()
Const Url$ = "examplewebsite.com"
Dim HTMLDoc As HTMLDocument
Dim oHTML_Element As IHTMLElement
Dim ie As Object
Set ie = CreateObject("InternetExplorer.Application")
With ie
.Navigate Url
ieBusy ie
.Visible = True
Set HTMLDoc = ie.Document
Dim ID1 As String, ID2 As String
For Each oHTML_Element In HTMLDoc.getElementsByTagName("input")
If oHTML_Element.Name Like "txt_1*" Then ID1 = oHTML_Element.Name
If oHTML_Element.Name Like "txt_2*" Then ID2 = oHTML_Element.Name
Next
Debug.Print ID1
Debug.Print ID2
Dim oLogin As Object, oPassword As Object
Set oLogin = .Document.getElementsByTagName(ID1)
Set oPassword = .Document.getElementsByTagName(ID2)
oLogin.Value = "MyUsername"
oPassword.Value = "MyPassword"
.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