Home > Software engineering >  When using osstream.SaveToFile, is there any way to save the downloaded CSV as a sheet as opposed to
When using osstream.SaveToFile, is there any way to save the downloaded CSV as a sheet as opposed to

Time:07-02

I am currently trying to download a file with a link that outputs a CSV file. I have confirmed that it downloads, but I want it to download into the same workbook, as a separate sheet, without creating another workbook. Is there any parameter/different function that achieves this? My current code is as follows

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 ("C:\Users\ppppp\Downloads\file1.csv"), 2
        ostream.Close
    End If

End Sub

CodePudding user response:

How about this:

With Workbooks.Open(myUrl)
    .Sheets(1).copy After:=ThisWorkbook.Worksheets(ThisWorkbook.Worksheets.Count)
    .Close false
End With
  • Related