来自网址

时间:2016-09-06 10:57:51

标签: excel vba file url download

我正在尝试构建一个Excel VBA命令,该命令将自动执行以下操作:

  1. 从已制作的Excel电子表格中的另一个excel wordbook导入列表,其中的项目将与相应的列匹配(即标识地址,邮政编码等,并将它们分配到包含地址,邮政编码等的列)
  2. 从帖子代码列表(列)复制粘贴子9,999行并将其粘贴到以下网址(http://imd-by-postcode.opendatacommunities.org/))
  3. 然后将这些值粘贴到文本框区域后,我希望按下“获取剥夺数据”按钮,然后下载创建的xlsx文件
  4. 将打开下载的Excel工作簿,并将所有信息复制/粘贴到特定空白工作表上的原始工作簿中
  5. 重复此功能,直到完成所有子9,999行(即如果我有30,000行数据执行此操作3次)
  6. 到目前为止,我已经开始做的是第2步和第3步,我已经设法将值粘贴到文本框区域并“按下”“获取剥夺数据”按钮,但我已经堆积如何下载由网站在下一页创建的生成的xlsx文档。我正在使用的代码如下,但在下载文件时我一直遇到错误:

    Sub papafi_1_command()
    
    Dim ie As Object
    Dim MyURL As String
    
    Set ie = CreateObject("InternetExplorer.Application")
    
    'create new instance of IE. use reference to return current open IE if
    'you want to use open IE window. Easiest way I know of is via title bar.
    MyURL = "http://imd-by-postcode.opendatacommunities.org/"
    
    ie.Navigate MyURL
    
    'go to web page listed inside quotes
    ie.Visible = True
    
    While ie.Busy
        DoEvents 'wait until IE is done loading page.
    Wend
    
    'Generate text string
    Dim str As String
    Dim arr() As Variant
    Dim tableRow As Integer
    Dim tableCol As Integer
    
    'Assign range to an array
    arr = Range("C7:C10005")
    
    Sheets("RPInputs").Range("C7:C10005").Select
    Selection.Copy
    
    'Loop through each row of the range to format a tab delimited text string
    For tableRow = LBound(arr) To UBound(arr)
        For tableCol = LBound(arr, 2) To UBound(arr, 2)
            str = str & arr(tableRow, tableCol) & vbTab
        Next tableCol
        str = str & vbNewLine
    Next tableRow
    
    With ie.Document
    
    'Assign text string to textarea
    .getElementById("postcodes").Value = str
    
    'Timedelay
     Application.Wait (Now + TimeValue("0:00:03"))
    
    'Button Click
    .getElementById("submit").Click
    
    'Timedelay #2
     Application.Wait (Now + TimeValue("0:00:30"))
    
    'Download
    Dim strURL As String
    Dim strPath As String
    Dim strString As String
    Dim iEnd As Long
    Dim iStart As Long
    
    strString = "Visit my webpage at  http://imd-by-postcode.opendatacommunities.org/" 'the string you want to search
    
    iStart = InStrRev(strString, "http") 'This is where your url starts
    iEnd = InStrRev(strString, ".com") 'This is where your url ends
    strURL = Mid(strString, iStart, (iEnd - iStart) + 4)
    
     '~~> URL of the Path
    'strURL = "http://imd-by-postcode.opendatacommunities.org/"
    
    ' ~~> Destination for the file
    strPath = "C:\USers\FilipposP\Downloads\deprivation-by-postcode (test).xlsx"
    
    ret = URLDownloadToFile(0, FieldStart, strPath, 0, 0)
    
    If ret = 0 Then
        MsgBox "File successfully downloaded"
    Else
        MsgBox "Unable to download the file"
    End If
    
    End With
    
    End Sub
    

    我有兴趣设法下载该文件,但是如果您能提供帮助,并且在第1步或第5步,我们将非常感激。

0 个答案:

没有答案