I am extremely green with VBA, and am attempting to set up a macro sheet in Excel using VBA to partially automate a manual data entry process for my company to help cut down on time and error costs.
A successful script would accomplish the following:
Currently I have only accomplished step 1, in which the script launches IE and navigates to the website in question ( https://iapps.courts.state.ny.us/chrs/SignIn ), however, I am getting a Run-time error 438 (Object doesn't support this property or method) when attempting to insert the username and password for the site.
I've attempted multiple different codes that I found in google searches, all which return the same error. Currently I have the Username and Password listed on sheet 2 of the macro workbook, as referenced in the code. Given my lack of knowledge, I am unsure where my error exists.
For reference, we're running on VBA 7.1, Excel 2013, and IE 11.
Sub login()
Const Url$ = "https://iapps.courts.state.ny.us/chrs/SignIn"
Dim UserName As String, Password As String, LoginData As Worksheet
Set LoginData = ThisWorkbook.Worksheets("Sheet2")
UserName = LoginData.Cells(1, "B").Value
Password = LoginData.Cells(2, "B").Value
Dim ie As Object
Set ie = CreateObject("InternetExplorer.Application")
With ie
.navigate Url
ieBusy ie
.Visible = True
Dim oLogin As Object, oPassword As Object
Set oLogin = .document.getElementsByName("txtUserName")(0)
{This is where the 438 error occurs ^}
Set oPassword = .document.getElementsByName("pwPassword")(0)
oLogin.Value = UserName
oPassword.Value = Password
.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
Currently, Sheet 2 is set up with the user ID in cell B1, and the Password in Cell B2.
The expected results would be a successful login, at which point I would work towards figuring out the code for the page to follow, and for the actual data entry page.
Use a proper page load wait. Use ids as well as they are faster. I use css id selectors eg #txtUserName
, which are equivalent to getElementById
but faster.
Option Explicit
'VBE > Tools > References: Microsoft Internet Controls
Public Sub Login()
Dim ie As InternetExplorer
Const URL As String = "https://iapps.courts.state.ny.us/chrs/SignIn"
Dim userName As String, password As String, loginData As Worksheet
Set ie = New InternetExplorer
Set loginData = ThisWorkbook.Worksheets("Sheet2")
userName = loginData.Cells(1, "B").Value
password = loginData.Cells(2, "B").Value
With ie
.Visible = True
.Navigate2 URL
While .Busy Or .readyState < 4: DoEvents: Wend
With .document
.querySelector("#txtUserName").Value = userName
.querySelector("#pwPassword").Value = password
.querySelector("#btnSubmit").Click
End With
Stop
End With
End Sub
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.