2017-09-23 65 views
1

我是新來的VBA。 我正在創建vba登錄網站並獲取信息,但是當我嘗試登錄時請求驗證碼。我需要一個代碼來等待驗證碼輸入並提交表單。請幫助我。VBA等到輸入驗證碼提交表單

Sub iepart2() 
    Dim ie As Object 
    Set ie = CreateObject("internetexplorer.application") 

    With ie 
     .Visible = True 
     .navigate "http://adb.in/ser/" 

     Do While .busy 
      DoEvents 
     Loop 

     Do While .readystate <> 4 
      DoEvents 
     Loop 
     Set txtUname = .document.getelementbyid("txtUname") 
     txtUname.Value = "Amarindaz" 
     Set txtEmail = .document.getelementbyid("txtEmail") 
     txtEmail.Value = "[email protected]" 
     Application.Wait Now + TimeValue("00:00:15") 
     .Quit 
    End With 
End Sub 

回答

0

我建議不要在代碼中使用等待。只需將代碼分成兩部分,然後讓用戶在兩者之間執行驗證碼。

  1. 代碼:充液登錄表單。 (你已經有了)。別做什麼了。
  2. 用戶手動輸入驗證碼。
  3. 代碼:獲取需要,從網站內的信息。 (新代碼)。需要在成功登錄(驗證碼後)後調用/調用。

爲此,您只需更持久地存儲IE對象(使用Static或Public關鍵字而不是Dim)。這將允許您在用戶輸入驗證碼後繼續使用相同的對象(瀏覽器窗口)。

爲了給你明白我的意思了一些想法,這裏是一個僞代碼,利用公共模塊級ie變量。只需複製到空白的模塊:

Public ie As Object 

Sub login() 

    Set ie = CreateObject("internetexplorer.application") 
    ie.Visible = True 
    ie.navigate "http://adb.in/ser/" 
    'all the other login stuff. filling in the input elements 

End Sub 

Sub query() 'Just for demonstration, nothing useful going on here :) 
    ie.Visible = False 
    ie.Visible = True 
End Sub