我正在尝试构建一个Excel VBA命令,该命令将自动执行以下操作:
到目前为止,我已经开始做的是第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步,我们将非常感激。