2013-11-04 69 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

相關問題