2015-01-10 65 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