I am trying to download and save a .xlsm file using VBA macro. I am wondering how to do that. I can save .xlsx file but the same code does not work when I am trying to save xlsm from the same direction. The error message is: "Run-time error '3004': Write to file filed.
Does anybody know the reason and solution?
Sub getFiles()
Dim WinHttpReq As Object
myPath = Application.ThisWorkbook.Path
Set WinHttpReq = CreateObject("Microsoft.XMLHTTP")
myURL = Cells(2, 3).Value 'URL like http://siteaddress/file.xlsm
myFile = Cells(2, 2).Value 'just a file name
WinHttpReq.Open "GET", myURL, False, "", ""
WinHttpReq.send
If WinHttpReq.Status = 200 Then
Set oStream = CreateObject("ADODB.Stream")
oStream.Open
oStream.Type = 1
oStream.Write WinHttpReq.responseBody
oStream.SaveToFile myPath & "\file.xlsm", 2
oStream.Close
End If
End Sub
Thanks in advance!