我有下面的代码从两个需要登录的页面拉出一个表。该代码打开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语句,而不是仅仅添加代码块,无论状态如何(通过适当的错误处理,如果失败),尝试首先强制注销。 –