在当前网站上查询Web表

时间:2017-10-10 21:59:17

标签: excel vba excel-vba csv

我遇到了一些问题。通常当我拉桌子时,我会使用来自网络的"数据" excel中的工具,但我现在有很多地方需要提取数据,首先要求我输入用户名和密码。我想出了一些代码(尽管可能不是最优雅的),但意识到一旦我到达我想要的页面,我就不知道如何提取表格。这是我到目前为止所拥有的。

Sub Login()
    Sheets("IOL").Select
    Set ie = CreateObject("InternetExplorer.application")
    ie.Visible = True
    ie.Navigate ("https://internalsite.company.com/secure/login" & ActiveCell)
    Do
        If ie.ReadyState = 4 Then
            ie.Visible = True
            Exit Do
        Else
            DoEvents
        End If
    Loop
    ie.Document.forms(0).all("badgeBarcodeId").Value = "00000"
    ie.Document.forms(0).submit
'used because it redirects to a new page after submitting and I couldn't figure out how to make it wait for the new page to load before proceeding. 
    Application.Wait (Now + TimeValue("0:00:02"))
    ie.Document.forms(0).all("password").Value = "00000"
    ie.Document.forms(0).submit
End Sub

登录完成后,我想转到http://internalsite.company.com/csv并将csv直接导入表格。每当我建立新连接时,它都会让我再次登录,所以我认为必须有一种方法来提取文件而不添加新连接。对于更复杂的VBA我是新手,所以请耐心等待。

1 个答案:

答案 0 :(得分:1)

我能够获得此代码来完成这项工作,但最好直接获取CSV而不是表格。有时桌子不喜欢加载。

 Sub Login()
        Dim clip As DataObject
        Dim ieTable As Object
        Set ie = CreateObject("InternetExplorer.application")
        ie.Visible = True
        ie.Navigate ("https://internalsite1.company.com/secure/login" & ActiveCell)
        Do
            If ie.ReadyState = 4 Then
                ie.Visible = True
                Exit Do
            Else
                DoEvents
            End If
        Loop
        ie.Document.forms(0).all("badgeBarcodeId").Value = "00000"
        ie.Document.forms(0).submit
        Do While ie.Busy: DoEvents: Loop
        Do Until ie.ReadyState = 4: DoEvents: Loop
        ie.Document.forms(0).all("password").Value = "000000"
        ie.Document.forms(0).submit
        Do While ie.Busy: DoEvents: Loop
        Do Until ie.ReadyState = 4: DoEvents: Loop
        ie.Navigate "http://internalsite2.company.com/site/Inbound?filter=1To3Days"
        Do While ie.Busy: DoEvents: Loop
        Do Until ie.ReadyState = 4: DoEvents: Loop
        Set ieTable = ie.Document.all.Item("DataTables_Table_0")
        If Not ieTable Is Nothing Then
        Set clip = New DataObject
        clip.SetText "" & ieTable.outerHTML & ""
        clip.PutInClipboard
        Workbooks("Production Meeting Dashboard.xlsm").Activate
        Sheets("IOL").Select
        Range("A1").Select
        ActiveSheet.PasteSpecial Format:="Unicode Text", link:=False, _
            DisplayAsIcon:=False, NoHTMLFormatting:=True
         End If
    End Sub