我有下面的代碼從兩個需要登錄的頁面拉出一個表。該代碼打開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
你可以檢查的)'ie.locationUrl' B)'ie.getElementsByTagName( 「形式」)(0).name' C)'ie.document.title'。這兩頁之間必須有許多不同之處。 – Jeeped
細節有點不清楚,但是您可以嘗試添加IF語句,而不是僅僅添加代碼塊,無論狀態如何(通過適當的錯誤處理,如果失敗),嘗試首先強制註銷。 –