![](/img/trans.png)
[英]Trying to automate data entry on a sap webapp using IE and excel vba
[英]How to use VBA/Excel to automate a data entry process using IE?
我對VBA非常滿意,並且正在嘗試使用VBA在Excel中設置宏表以部分自動化我公司的手動數據輸入過程,以幫助減少時間和錯誤成本。
成功的腳本將完成以下任務:
目前,我僅完成了步驟1,其中腳本啟動IE並導航到有問題的網站( https://iapps.courts.state.ny.us/chrs/SignIn ),但是,我正在運行嘗試插入網站的用戶名和密碼時出現錯誤438(對象不支持此屬性或方法)。
我嘗試了在Google搜索中找到的多個不同的代碼,所有代碼均返回相同的錯誤。 目前,我在宏工作簿的第2頁上列出了用戶名和密碼,如代碼中所引用。 由於我缺乏知識,我不確定我的錯誤在哪里。
作為參考,我們在VBA 7.1,Excel 2013和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
當前,工作表2的設置是在單元格B1中設置用戶ID,在單元格B2中設置密碼。
預期的結果將是成功登錄,這時我將努力確定要遵循的頁面和實際數據輸入頁面的代碼。
使用適當的頁面加載等待。 使用ID及其更快。 我使用css id選擇器,例如#txtUserName
,它與getElementById
等效,但速度更快。
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.