2017-07-31 161 views
0

我试着输入值到使用Excel VBA网站的输入字段:解决VBA运行时间91错误

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 

这里是输入字段

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

我做的HTML代码一个用于调试的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将返回“对象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 

末次

+0

不相关的问题(我假设是发生在'Set'行),但'如果不UserN是Nothing并且UserN.Length> 0那么''UserN'是'Nothing'将会崩溃 - 因为'Nothing'不会有'Length'属性。 – YowE3K

+0

是否设置了ie.document? FWIW,除非'UserN'对象有一个默认属性*,它返回[可表示为]一个字符串,MsgBox'调用也会爆炸:删除括号,它们迫使参数被评估并通过作为一个值..应该是'MsgBox UserN.Value'。 –

+0

@ Mat's Mug我编辑了MsgBox,但它似乎没有改变任何东西(仍然输出对象...)我如何确保ie.document被设置?我还将我的整个代码添加到问题中。 – user90823745

回答

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 
+0

我的错误是与FileN没有UserN,但我试图更换行和MsgBox甚至不弹出。输入字段仍然留在网站上,因为我的代码永远不会进入If Not FileN ...循环 – user90823745

+0

我在代码中将“UserN”更改为“FileN”和“userid”为“MsgNamePattern”,因为这是代码的一部分与运行时错误,但MsgBox不会弹出,当我F8通过调试时,我看到,它仍然没有进入“如果不是FileN是什么...”循环 – user90823745

+0

@ user90823745除非你有什么我可以做分享HTML或网址,因为它与我测试过的随机网站一起工作。 – Tehscript