如何使用VBA自动登录网页

时间:2019-02-18 12:05:34

标签: vba

我正在尝试使用VBA代码自动登录页面。我已经将此代码用于一页,并且似乎可以正常工作。但是,当我尝试在其他页面上使用它时,它会不断抛出错误 91:

  

对象变量或未设置块变量的情况。

您知道以下代码示例有什么问题吗?

Sub LoginDAWN()
    Const Url$ = "http://pathology01/DawnAC/DawnAppPool_1/polarserver.asp?Screen=FrameSet&SID=BF8C62FB9D438AB0313E751C077F3B92&PageID=0"
    Dim UserName As String, Password As String, LoginData As Worksheet

    Set LoginData = ThisWorkbook.Worksheets("Accounts")

    UserName = LoginData.Cells(2, "A").Value
    Password = LoginData.Cells(3, "A").Value

    Dim ie As Object

    Set ie = CreateObject("InternetExplorer.Application")

    With ie

        .navigate Url

        ieBusy2 ie

        .Visible = True

        Dim oLogin As Object, oPassword As Object

        Set oLogin = .Document.getElementsByName("UserName")(0)

        Set oPassword = .Document.getElementsByName("PassWord")(0)

        oLogin.Value = UserName

        oPassword.Value = Password

        .Document.forms(0).FireEvent

    End With
    'Application.SendKeys ("{enter}")
End Sub

Sub ieBusy2(ie As Object)
    Do While ie.Busy Or ie.readyState < 4
        DoEvents
    Loop
End Sub

0 个答案:

没有答案