如題,例如此網址:http://download.post.gov.tw/post/download/Village_H_10602.xls
後面的10602每天都會變化,請問該如何以vba每天自動下載檔案,又不受數字的變化影響呢?
沒寫過vba, 臨時改了一下,測試過可下載,參考Daniel意見
下載函數
Sub download(str As String)
Dim myURL As String
'來源檔案
myURL = "http://download.post.gov.tw/post/download/Village_H_1" & str & ".xls"
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 ("d:\Village_H_1" & str & ".xls")
oStream.Close
End If
End Sub
執行函數
Sub exec()
Dim i, j As Integer
Dim d, h, str As String
For i = 1 To 12
d = Format(i, "00")
For j = 1 To 23
h = Format(j, "00")
str = d & h
download str
Next
Next i
End Sub