2017-07-31 183 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