2014-09-01 200 views
2

運行此代碼時,我繼續出現上述錯誤。我嘗試了幾十次網頁搜索和調整以解決問題,但未成功。任何幫助,如果非常讚賞VBA錯誤:「運行時錯誤'424'嘗試執行.getElementById時所需的對象

Public Sub Tagg() 

    Dim URL As String 
    Dim ie As SHDocVw.InternetExplorer 'MICROSOFT Internet Controls (shdocvw.dll) 
    Dim HTMLdoc As MSHTML.HTMLDocument 'Microsoft HTML Object Library 
    Dim loginFrame As HTMLIFrame 
    Dim usernameInput As HTMLInputElement, passwordInput As HTMLInputElement 
    Dim username As String, password As String 

    username = "MTorres" 'CHANGE THIS 
    password = "melissa1" 'CHANGE THIS 
    URL = "https://webaccess.tagglogistics.com/cadence/webaccess.net?action=203&Full=Y&args=415878" 

    Set ie = New SHDocVw.InternetExplorer 

    With ie 
     .Visible = True 
     .navigate URL 
     Do While .readyState <> READYSTATE_COMPLETE Or .Busy 
      DoEvents 
     Loop 
     Set loginFrame = .document.getElementById("loginframe") ' ****ERROR HERE**** 
     Set HTMLdoc = loginFrame.contentWindow.document 

     '<input name="username" id="uname" class="ti" maxlength="32" onchange="setUserValue();" 
     'onkeydown="setupUserValue(event);" onmouseup="return false;" onclick="SelectAll();" onfocus="SelectAll();" 
     'aria-describedby="cof_username errormsg" type="text"> 

     Set usernameInput = HTMLdoc.getElementsByName("username")(0) 
     usernameInput.Focus 
     DoEvents 
     usernameInput.Value = username 
     usernameInput.FireEvent "onkeydown" 
     usernameInput.FireEvent "onchange" 
     usernameInput.FireEvent "onmouseup" 

     '<input id="cofisso_ti_passw" name="password" class="ti" maxlength="32" aria-describedby="pass" type="password"> 

     Set passwordInput = HTMLdoc.getElementsByName("password")(0) 
     passwordInput.Focus 
     passwordInput.Value = password 

     'HTMLdoc.forms(0).submit 
     '<input src="/resources/images/btn_login.gif" alt="Login" title="Login" name="cofisso_btn_login" id="cofisso_btn_login" type="image"> 
     HTMLdoc.getElementById("cofisso_btn_login").Click 
     Do While .readyState <> READYSTATE_COMPLETE Or .Busy 
      DoEvents 
     Loop 

     '----------- NEW CODE -------------- 

     'Might need this wait loop 
     While .document.readyState <> "complete" 
      DoEvents 
     Wend 

     'Either reload HTMLdoc from current IE.document: 
     Set HTMLdoc = .document 

     'Or if LNKLOGOUT is inside an iframe: 
     '  Dim iframe As HTMLIFrame 
     '  Set iframe = .document.getElementsByTagName("IFRAME")(0)  '0 = 1st iframe 
     '  Set HTMLdoc = iframe.contentWindow.document 

     'HTMLdoc should now be available here - display webpage TEXT TO verify 

     MsgBox HTMLdoc.body.innerText 

     '---------- END OF NEW CODE ---------- 

     'Click "Sign Out" link 
     '<a id="LNKLOGOUT" class="logout" href="https://servicing.capitalone.com/C1/SelfService/CMLogoutIntercept.aspx">Sign Out</a> 
     HTMLdoc.getElementById("LNKLOGOUT").Click 
     Do While .readyState <> READYSTATE_COMPLETE Or .Busy 
      DoEvents 
     Loop 

    End With 

End Sub 
+0

你確定'.document'不是'nothing'嗎? – Bathsheba 2014-09-01 15:30:23

+1

VBA中的哪一行發生錯誤?爲了找到答案,請單擊錯誤消息中的「debug」,並且導致問題的行將被突出顯示。 – LondonRob 2014-09-01 15:30:30

+0

代碼中已經有一條註釋,指出發生錯誤的位置。 – 2014-09-01 15:58:38

回答

0

您沒有設置文檔和loginframe正確

替換這些行:。

Set loginFrame = .document.getElementById("loginframe") ' ****ERROR HERE**** 
Set HTMLdoc = loginFrame.contentWindow.document 

有了這些:

Set HTMLdoc = ie.Document 
Set loginFrame = HTMLdoc.getElementById("loginframe") ' ****ERROR FIXED**** 

你會得到另一個錯誤的幾行下來,當你試圖找到一個名爲「用戶名」的元素,因爲該網頁上沒有這樣的元素,但希望這將讓你在正確的跟蹤。

相關問題