解决VBA运行时91错误

时间:2017-07-31 19:52:52

标签: html excel vba excel-vba internet-explorer

我正在尝试使用Excel VBA将值输入网站的输入字段:

Dim FileN As Object
Set FileN = ie.document.getElementsByName("MsgNamePattern")
MsgBox (FileN)
If Not FileN Is Nothing And FileN.Length > 0 Then
        FileN(0).Value = fileName
End If

这是输入字段的html代码

<input name="MsgNamePattern" onblur="validateMessageName(this)" type="text" size="20">

我做了一个MsgBox用于调试,如果它成功设置了对象,应该说“对象HTMLinputelement”,但是我一直得到运行时91错误,因为某些原因没有设置对象变量。我使用以下代码成功登录了该站点:

Dim UserN As Object
Set UserN = ie.document.getElementsByName("userid")
    MsgBox (UserN)
    If Not UserN Is Nothing And UserN.Length > 0 Then
        UserN(0).Value = "username"
    End If

MsgBox将返回“object HTMLinputelement”。以下是登录输入字段的HTML:

<input name="userid" class="inputStyle" onchange="document.login.password.focus();" type="text" size="20">

我看不出我做错了什么,我以为我使用相同的方法成功登录,所以我很困惑为什么登录后它不能用于搜索字段。

以下是整个代码:

Sub getComponents()

   Dim WebAddressIn As String
   Dim ie As Object
   Set ie = New InternetExplorer
   WebAddressIn = "https://edx.standardandpoors.com/mailbox/jsp/login.jsp"

    Set ie = CreateObject("internetexplorer.application")
    ie.Navigate2 WebAddressIn

    Do While (ie.Busy Or ie.readyState <> READYSTATE_COMPLETE)
        DoEvents
    Loop

    ie.Visible = True

    Dim UserN As Object ' MSHTML.IHTMLElement
    Dim PW As Object ' MSHTML.IHTMLElement
    Dim ElementCol As Object ' MSHTML.IHTMLElementCollection

    Do While ie.Busy
    Loop

    ' enter username and password in textboxes
    Set UserN = ie.document.getElementsByName("userid")
    MsgBox UserN
    If Not UserN Is Nothing And UserN.Length > 0 Then
        ' fill in first element named "username", assumed to be the login name field
        UserN(0).Value = "username"
    End If

    Set PW = ie.document.getElementsByName("password")
    ' password
    If Not PW Is Nothing And PW.Length > 0 Then
        ' fill in first element named "password", assumed to be the password field
        PW(0).Value = "password"
    End If

    Do While ie.Busy
    Loop

    Set ElementCol = ie.document.getElementsByName("submit")
    MsgBox ElementCol
    For Each btnInput In ElementCol
        If btnInput.Value = "*Sign In" Then
            btnInput.Click
           Exit For
        End If
    Next btnInput

    Do While ie.Busy
    Loop

    Do While (ie.Busy Or ie.readyState <> READYSTATE_COMPLETE)
        DoEvents
    Loop

    Do Until ie.readyState = 4
    Loop

     Dim fileName As String
     fileName = Format(Now(), "yyyyMMdd") & "_SPGSCI_PRE_STD.TXT"

    Dim FileN As Object ' MSHTML.IHTMLElement
    Dim SearchBox As Object ' MSHTML.IHTMLElementCollection

    Do While ie.Busy
    Loop

    ie.Visible = False
    ie.Visible = True

    'Modified to add Tehscript's edit
    'Set FileN = ie.document.getElementsByName("MsgNamePattern")
    Do
        On Error Resume Next
        Set FileN = ie.document.getElementsByName("MsgNamePattern")(0)
    Loop Until Err.Number <> 91

    MsgBox FileN
    If Not FileN Is Nothing And FileN.Length > 0 Then
        FileN(0).Value = fileName
    End If

    Do While ie.Busy
    Loop

    Set SearchBox = ie.document.getElementsByTagName("a")
    For Each l In SearchBox
        If l.href = "javascript:myFunction('/mailbox/jsp/MBIList.jsp')" Then
            l.Click
    Exit For
        End If
    Next l

    Do While ie.Busy
    Loop

End Sub

1 个答案:

答案 0 :(得分:0)

导航到网址后,请确保代码中包含以下内容。

Do While (ie.Busy Or ie.readyState <> READYSTATE_COMPLETE)
    DoEvents
Loop

如果仍有“运行时错误91”,则替换

Set UserN = ie.document.getElementsByName("userid")

以下内容:

Do
    On Error Resume Next
    Set UserN = ie.document.getElementsByName("userid")
Loop Until Err.Number <> 91