长期的读者,第一次的海报。不能强调这个网站对一个完整的新手有用。

下面的代码通过遍历3列行(在第2列)的一列(第11列)中的日期列来形成URL(然后下载文件),



URL = row1.date1的下载文件,
然后是row1.date2,
然后是row1.date3。
然后,row2.date1,
然后是row2.date2,
然后是row2.date3。
然后,row3.date1,
然后是row3.date2,
然后是row3.date3。

它完成了row1.date1,然后是row1.date2,然后是row1.date3,就好了。当它循环并启动row2时,就在下载row2.date1之前,它在oStream上生成运行时错误'3001'。写入WinHttpReq.responseBody
错误是:参数类型错误,超出可接受范围或彼此冲突。

我整个周末都在努力地想办法,但是没有运气。请解决让我看起来很愚蠢!我进行了搜索,似乎没有人遇到循环中第一次连接良好的问题,而第二次则不是。如果我错过了,请给我发送链接。

  Sub download_file()
  Dim myURL As String
  Dim y As Integer
  Dim row As Integer

  row = 1

  Do
    y = 1

    Do
      myURL = "XXXXXX" & Cells(row, 2) & "XXXXXX" & Cells(y, 11)
      Dim WinHttpReq As Object
      Set WinHttpReq = CreateObject("Microsoft.XMLHTTP")
      WinHttpReq.Open "GET", myURL, False
      WinHttpReq.send
      myURL = WinHttpReq.responseBody

      If WinHttpReq.Status = 200 Then
        Set oStream = CreateObject("ADODB.Stream")
        oStream.Open
        oStream.Type = 1
        oStream.Write WinHttpReq.responseBody
        oStream.SaveToFile ("Z:\XXXX\" & Cells(row, 3) & Cells(y, 11) & ".txt.gz")
        oStream.Close
      End If

      y = y + 1
    Loop Until Len(Cells(y, 11)) = 0

    row = row + 1
  Loop Until Len(Cells(row, 2)) = 0
End Sub

编辑:@Cilla
太棒了!您的代码对我来说非常顺利,谢谢!我现在必须以您的格式组合2个代码。您对此有何看法?你会这样吗?

{ private 声明函数URLDownloadToFile Lib“urlmon”别名“URLDownloadToFileA”(ByVal pCaller1为长,ByVal szURL1为字符串,ByVal szFileName1为字符串,ByVal dwReserved1为长,ByVal lpfnCB1为长,ByVal pCaller2为长,ByVal szURL2 szFileName2作为字符串,ByVal dwReserved2作为长,ByVal lpfnCB2作为长)

子DownloadMe()
昏暗x整数
昏暗的整数
y = 1

Do

Dim strGetFrom1 As String, strSaveTo1 As String, strURL1, intResult As Long
strURL1 = "AAAAA" & Cells(y, 1) & "BBBBB"
strSavePath1 = "C:\test\" & Cells(y, 1) & ".csv"
myResult = URLDownloadToFile(0, strURL1, strSavePath1, 0, 0, 0, 0, 0, 0, 0)
If intResult <> 0 Then MsgBox "Oops!  There was an error with iOS"

y = y + 1

Loop Until Len(Cells(y, 1)) = 0



x = 1

Do

y = 1

Do

Dim strGetFrom2 As String, strSaveTo2 As String, strURL2, intResult As Long
strURL2 = "MMMMM" & Cells(x, 2) & "NNNNN" & Cells(y, 3) & "PPPPP"
strSavePath2 = "C:\test\" & (y, 3) & ".csv"
myResult = URLDownloadToFile(0, 0, 0, 0, 0, 0, strURL2, strSavePath2, 0, 0)
If intResult <> 0 Then MsgBox "Oops!  There was an error with iOS"

y = y + 1
Loop Until Len(Cells(y, 3)) = 0


x = x + 1
Loop Until Len(Cells(x, 2)) = 0

End Sub}

可以在sub downloadme()内定义专用子吗?

再次感谢!

最佳答案

不知道是什么原因导致了您的问题,但我想我记得在某个时候尝试过使用“流”方法并遇到问题。这是我最终使用的另一种对我有用的方法:

Private Declare Function URLDownloadToFile Lib "urlmon" Alias "URLDownloadToFileA" (ByVal pCaller As Long, ByVal szURL As String, ByVal szFileName As String, ByVal dwReserved As Long, ByVal lpfnCB As Long) As Long

Sub DownloadMe()
Dim strGetFrom As String, strSaveTo As String, intResult As Long
strURL = "http://mydata.com/data-11-07-13.csv"
strSavePath = "C:\MyUser\Desktop\data-11-07-13.csv"
myResult = URLDownloadToFile(0, strURL, strSavePath, 0, 0)
If intResult <> 0 Then MsgBox "Oops!  There was an error!"
End Sub

09-26 23:18