0

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!

braX
  • 11,506
  • 5
  • 20
  • 33
jaHolden
  • 41
  • 3
  • not sure if this is relevant: https://stackoverflow.com/questions/16952451/vba-write-new-file-to-program-files-folder ... either way it raises the question: when you were successful with the XLSX, were you saving **to** the same folder that you're trying for the XLSM? (You won't be able to save to protected folders, such as Windows system folders.) – ashleedawg Oct 27 '21 at 10:25
  • Does this answer your question? [Run-time eror '3004' write to file failed when downloading file](https://stackoverflow.com/questions/48964640/run-time-eror-3004-write-to-file-failed-when-downloading-file) – Foxfire And Burns And Burns Oct 27 '21 at 12:43

0 Answers0