2015-01-10 66 views
1

我有下面的代碼從兩個需要登錄的頁面拉出一個表。該代碼打開IE瀏覽器,進入登錄頁面,輸入憑據,然後拉2個表格。修改數據檢索宏

但是,如果IE已經登錄與用戶,它直接把你帶到這個頁面(因此代碼中的錯誤,因爲沒有登錄字段): https://www.example.com/taskprocessing/manage.jsp

我需要添加一個IF聲明說,如果它的土地此頁面上,這個鏈接點擊註銷,然後用憑據登錄進行: https://www.example.com/taskprocessing/logout.jsp

Sub GetTable() 

    Set ie = CreateObject("InternetExplorer.Application") 
    With ie 
     .Visible = True 
     .Navigate "https://www.example.com/taskprocessing/login.jsp" 
     Do Until .ReadyState = 4 
      DoEvents 
     Loop 
     .Document.all.Item("Username").Value = "username123" 
     .Document.all.Item("Password").Value = "password123" 
     .Document.forms(0).submit 
    End With 


With ActiveSheet.QueryTables.Add(Connection:= _ 
"URL;https://www.example.com/taskprocessing/report_pending_tasks.jsp", Destination:=Range("J1")) 


     .WebSelectionType = xlAllTables 
     .WebFormatting = xlWebFormattingNone 
     .WebPreFormattedTextToColumns = True 
     .WebConsecutiveDelimitersAsOne = True 
     .WebSingleBlockTextImport = False 
     .WebDisableDateRecognition = False 
     .WebDisableRedirections = False 
     .Refresh BackgroundQuery:=False 


    With ActiveSheet.QueryTables.Add(Connection:= _ 
     "URL;https://www.example.com/taskprocessing/report_task_processing_stats.jsp", Destination:=Range("A1")) 
     .WebSelectionType = xlSpecifiedTables 
     .WebFormatting = xlWebFormattingNone 
     .WebTables = """user""" 
     .WebPreFormattedTextToColumns = True 
     .WebConsecutiveDelimitersAsOne = True 
     .WebSingleBlockTextImport = False 
     .WebDisableDateRecognition = False 
     .WebDisableRedirections = False 
     .Refresh BackgroundQuery:=False 
    End With 


End With 

End Sub 
+0

你可以檢查的)'ie.locationUrl' B)'ie.getElementsByTagName( 「形式」)(0).name' C)'ie.document.title'。這兩頁之間必須有許多不同之處。 – Jeeped

+0

細節有點不清楚,但是您可以嘗試添加IF語句,而不是僅僅添加代碼塊,無論狀態如何(通過適當的錯誤處理,如果失敗),嘗試首先強制註銷。 –

回答

0

您可以直接跳過登錄過程,如果沒有必要的。

Set ie = CreateObject("InternetExplorer.Application") 
    With ie 
     .Visible = True 
     .Navigate "https://www.example.com/taskprocessing/login.jsp" 
     Do Until .ReadyState = 4 
      DoEvents 
     Loop 

     On Error Resume Next 'this will help to understand if there was an error when trying retrieving the UserName field 
     Set userField = .Document.getElementById("Username") 
     If Err.Number = 0 Then 'if there was no error when trying to search for the username... 
      .Document.all.Item("Username").Value = "username123" 
      .Document.all.Item("Password").Value = "password123" 
      .Document.forms(0).submit 
     End If 
     On Error GoTo 0 'turn-off the error handling once again 
End With 

我添加的行是:

On Error Resume Next 

...避免了密碼破譯,如果看的時候有一個錯誤要做到這一點,你可以按照以下方式重新構造您的頂級代碼對於不存在的元素;

Set userField = .Document.getElementById("Username") 

...查看網頁是否登錄或註銷。如果它已註銷,則在執行此操作時不會出現錯誤。如果不是,那麼這將導致「對象不存在錯誤」。

If Err.Number = 0 Then 

...所以如果在做這件事情時沒有錯誤,我們繼續登錄......否則我們就繼續做我們正在做的事情。

On Error GoTo 0 

......最後,我們關閉了錯誤處理,所以如果有一個在你將繼續進行定期通報頁面中的任何其他錯誤。

0

謝謝!我需要它註銷,所以我添加了一條Else語句。

On Error Resume Next 'this will help to understand if there was an error when trying retrieving the UserName field 
     Set userField = .Document.getElementById("Username") 

     If Err.Number = 0 Then 'if there was no error when trying to search for the username... 
     .Document.all.Item("Username").Value = "username123" 
     .Document.all.Item("Password").Value = "password123" 
     .Document.forms(0).submit 

     Else 
     .Navigate "https://www.example.com/taskprocessing/logout.jsp" 
     Do While ie.Busy: DoEvents: Loop 
     Do Until .ReadyState = 4 
      DoEvents 
     Loop 

     .Document.all.Item("Username").Value = "username123" 
     .Document.all.Item("Password").Value = "password123" 
     .Document.forms(0).submit 

     End If 
     On Error GoTo 0 'turn-off the error handling once again