简体   繁体   English

如何使用VBA / Excel通过IE自动执行数据输入过程?

[英]How to use VBA/Excel to automate a data entry process using IE?

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. 我对VBA非常满意,并且正在尝试使用VBA在Excel中设置宏表以部分自动化我公司的手动数据输入过程,以帮助减少时间和错误成本。

A successful script would accomplish the following: 成功的脚本将完成以下任务:

  1. Launch IE and navigate to the specified website. 启动IE并导航到指定的网站。
  2. Log in using provided credentials. 使用提供的凭据登录。
  3. Enter a value into a field with a unique ID and press the "Next" button 在具有唯一ID的字段中输入值,然后按“下一步”按钮
  4. Enter corresponding data into the 200 fields, each with unique ID's (Ex. LastName_0 would correspond to cell A2, LastName_1 Corresponds to A3, etc.) 在200个字段中输入相应的数据,每个字段具有唯一的ID(例如LastName_0对应于单元格A2,LastName_1对应于A3等)

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. 目前,我仅完成了步骤1,其中脚本启动IE并导航到有问题的网站( https://iapps.courts.state.ny.us/chrs/SignIn ),但是,我正在运行尝试插入网站的用户名和密码时出现错误438(对象不支持此属性或方法)。

I've attempted multiple different codes that I found in google searches, all which return the same error. 我尝试了在Google搜索中找到的多个不同的代码,所有代码均返回相同的错误。 Currently I have the Username and Password listed on sheet 2 of the macro workbook, as referenced in the code. 目前,我在宏工作簿的第2页上列出了用户名和密码,如代码中所引用。 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. 作为参考,我们在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

Currently, Sheet 2 is set up with the user ID in cell B1, and the Password in Cell B2. 当前,工作表2的设置是在单元格B1中设置用户ID,在单元格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. 使用ID及其更快。 I use css id selectors eg #txtUserName , which are equivalent to getElementById but faster. 我使用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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM