修改数据检索macros

我有下面的代码,从两个页面,需要login拉表。 代码打开IE浏览器,进入login页面,放入证书,然后拉2个表。

但是,如果IE已经与用户login,它会直接将您带到此页面(因此没有login字段,因此代码错误): https : //www.example.com/taskprocessing/manage.jsp

我需要添加一条IF语句,如果它login到此页面上,单击此链接注销,然后继续使用凭​​据login: 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 

如果不需要,您可以跳过login过程。 要做到这一点,你可能会重新构造你的顶级代码,如下所示:

 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") 

…查看该页面是否已login或注销。 如果注销,这样做不会有错误。 如果不是,那么这将导致“对象不存在错误”。

 If Err.Number = 0 Then 

…如果这样做没有错误,我们继续login…否则,我们只是去继续做我们正在做的事情。

 On Error GoTo 0 

…最后,我们closureserror handling,所以如果页面中有任何其他错误,您将继续定期通知。

谢谢! 我需要它注销,所以我添加了一个其他语句。

 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