I'm a bit lost on why this particular code gets hung up, it's supposed to webscrape for .xlsm files that reside via a particular api path. I'm trying to have the code go through a list of identifiers/variables, build the urls and download the file and store it locally.
It seems to get hung up in the Docxlsm.send part, or at least when I interrupt the process that's where the debug/error sends me. This particular code I have modified from another webscrape tool I built , but it was for a different api and it was downloading csv files, not .xlsm, and it works fine.
Option Explicit
Sub NJ_DCA_Muni_Budget_Download()
Dim Sh As Worksheet
Dim Rng As Range
Dim Cell As Range
Dim DCAcode As String
Dim year As String
Dim StrURL As String
Set Sh = Worksheets("NJDCAcode")
Set Rng = Sh.Range("c2:c" & Sh.Range("A65536").End(xlUp).Row)
For Each Cell In Rng
DCAcode = Cell.Value
year = Sh.Range("D1")
StrURL = "http://www.state.nj.us/dca/divisions/dlgs/resources/fiscal_docs/"
StrURL = StrURL & year & "_data/" & year & "_fba/" & DCAcode & "_fba_" & year & ".xlsm"
Debug.Print StrURL
DownLoadIntoFolder StrURL, DCAcode & "FY" & year
Next Cell
End Sub
Sub DownLoadIntoFolder(URL As String, FileName As String)
Dim Docxlsm As Object, StreaMFile As Object
Dim Filepath As String
Filepath = Range("E1")
Set Docxlsm = CreateObject("WinHTTP.WinHTTPRequest.5.1")
Docxlsm.Open "GET", URL, False
Docxlsm.send
Set StreaMFile = CreateObject("ADODB.Stream")
With StreaMFile
.Type = 1
.Open
.Write Docxlsm.responseBody
.SaveToFile Filepath & FileName & ".xlsm", 2
'file path is determined where this module is saved and the folder as to which report year is being retrieved
'the folder needs to be established prior to running the code, i.e. if your retrievng "2017" data - that folder needs to exist etc.
.Close
End With
Set StreaMFile = Nothing
Set Docxlsm = Nothing
End Sub
Another secondary challenge: I've seen it done before but i do not know how to establish it. I know for the list of my identifiers certain ones will not yield a downloadable .xlsm file because it doesn't exist yet. how can i instruct this process to continue down the list, and keep a record or log of the identifiers that don't retrieve anything, or at least cause a problem?
Any help in further understanding is greatly appreciated.
Bookmarks