2016-08-04 31 views
0

我在用戶輸入登錄ID /密碼後正在編寫一個訪問網頁的宏。如果這些字段中的任何一個都爲空,我已經添加了代碼以退出子版本 - 但是,如何處理登錄不起作用的事件?目前,它只是將它們帶到網站登錄頁面,但我寧願讓登錄代碼循環直到登錄工作。VBA通​​過網站登錄處理事件

這樣做的任何方法?我不熟悉互聯網控制。

我的代碼登錄部分看起來是這樣的:

Set SupDOC = SupIE.document 
With SupDOC.forms(0) 
    .j_username.Value = InputBox("To Login Dun & Bradstreet, Enter your D&B Username/Email: ") 
    If .j_username.Value = "" Then 
     MsgBox "No Username was entered." 
     Exit Sub 
    End If 
    .j_password.Value = InputBox("To Login Dun & Bradstreet, Enter your D&B Password: ") 
    If .j_password.Value = "" Then 
     MsgBox "No Password was entered." 
     Exit Sub 
    End If 
    .submit 
End With 

編輯:

有時,登錄次數,並提交正確的登錄信息後,該網站重定向到「會話超時「並且需要再次登錄。登錄後,該站點不會加載到我稍後指定的頁面。任何想法解決這個問題?

感謝,

回答

0

我把這些循環在登錄提交後,並導航到新的網站,並解決了重定向的任何問題。這取決於網站,但這會繞過「會話超時」。

Do While SupIE.Busy: DoEvents: Loop 
Do Until SupIE.readyState = READYSTATE_COMPLETE: DoEvents: Loop 
0

你提交之後,你可以做一個循環等待的readyState要回4,然後檢查是否字段爲空,然後去到頂部,然後再試一次,直到它的工作原理。

編輯:將URL定義爲字符串,如果您重定向,它將導航到登錄頁面。

URL = SupIE.LocationURL 

TryAgain: 

    Set SupDOC = SupIE.document 
    With SupDOC.forms(0) 
     .j_username.Value = InputBox("To Login Dun & Bradstreet, Enter your D&B Username/Email: ") 
     If .j_username.Value = "" Then 
      MsgBox "No Username was entered." 
      Exit Sub 
     End If 
     .j_password.Value = InputBox("To Login Dun & Bradstreet, Enter your D&B Password: ") 
     If .j_password.Value = "" Then 
      MsgBox "No Password was entered." 
      Exit Sub 
     End If 
     .submit 

     Do While SupIE.readyState <> 4: DoEvents: Loop 

     If .j_username.Value = "" Or .j_password.Value = "" Then GoTo TryAgain: 

     If InStr(SupDOC.DocumentElement.outerText, "Session timed") Then 
      SupIE.navigate URL 
      Do While SupIE.readyState <> 4: DoEvents: Loop 
      GoTo TryAgain: 
     End If 

    End With 
+0

ReadyState 4定義了什麼? (我不知道有不同的readystate狀態)。 另外,如果輸入了無效登錄名,此循環是否會執行? – AnthonyT

+0

剛剛測試了代碼,無論我輸入什麼,它都會提示消息框 – AnthonyT

+0

IE有4個就緒狀態,實際上它們中的每一個都表示頁面的不同加載狀態,1完全未加載,4完全加載並且你可以互動和一切。 https://msdn.microsoft.com/en-us/library/ms534361(v=vs.85).aspx – Alex