2013-11-04 45 views
3

长时间读写器,第一次张贴海报。不能强调这个网站对于一个完整的新手有多么有用。下面运行时错误,但仅在第二个循环

代码通过日期的列中的一列(列11),用于3个集合(在第2列)循环形成的URL(其然后下载文件),

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

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

我已经花了整个周末tryng来计算这个,没有运气。请通过解决让我看起来很愚蠢!我搜索了,没有人似乎有问题,第一次在循环中连接是好的,而不是第二次。如果我错过了这个,请给我链接。

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个代码。你如何看待这个?你会做这种方式?:

{私人声明函数URLDownloadToFile库 “URLMON的” 别名 “URLDownloadToFileA”(BYVAL pCaller1长,BYVAL szURL1作为字符串,BYVAL szFileName1作为字符串,BYVAL dwReserved1长,BYVAL lpfnCB1只要,BYVAL pCaller2长,BYVAL szURL2作为字符串,BYVAL 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中定义private子do wnloadme()?

再次感谢!

+1

该代码工作正常,我(假设有在第3列的唯一值的文件名),我猜想,这是你使用特定的网址在第二个循环周围,返回的状态是200,但是写入流时会出错或为空或格式错误的响应。您是否在失败情况下手动验证目标网址? –

回答

2

不知道什么可能会导致你的问题,但我想我记得尝试你在某个时候使用的'流'方法并遇到问题。这里有一个不同的方法,我结束了使用该做的工作对我来说:

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 
+0

太棒了!您的代码对我来说更加顺畅,谢谢!我现在必须以您的格式组合2个代码。你对我上面的编辑有什么看法?谢谢! – user2952447

相关问题