download a file from onedrive

vmjan02

Well-known Member
Joined
Aug 15, 2012
Messages
1,132
Office Version
  1. 365
  2. 2021
  3. 2019
  4. 2016
  5. 2013
I have this code and is working perfect, it downloads a excel file from onedrive, only problem is once the file is downloaded, it gives an error (ref screen shot), after download, its not opening a downloaded file

any suggestion..

VBA Code:
Sub DownloadFile()
'Declare the Object and URL
Dim myURL As String
Dim WinHttpReq As Object
'Assign the URL and Object to Variables
myURL = "https://qtechsoftware-my.sharepoint.com/:x:/p/viral_shah/EQDzOJrS8EFMkvQibRv21IABkWlaehkR8wHYJm3G103-Kw?e=AyJLXT/Daily work sheet1.xlsx"
Set WinHttpReq = CreateObject("Microsoft.XMLHTTP")

'Provide Access Token and PWD to the URL for getting the service from API
WinHttpReq.Open "GET", myURL, False, "abcdef", "12345"
WinHttpReq.Send
Debug.Print WinHttpReq.Status
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:\Daily Work Sheet1.xlsx" ,  2
        oStream.Close
    End If
End Sub
 

Attachments

  • donwload file.jpg
    donwload file.jpg
    112.9 KB · Views: 126

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
Do not use ADODB.Stream but the standard VBA method Open file For Binary with an array variable as Byte for the responseBody …​
 
Upvote 0
Do not use ADODB.Stream but the standard VBA method Open file For Binary with an array variable as Byte for the responseBody …​
well i did tried the same, but no luck, any chance can you help me to modify the code
 
Upvote 0

See the yesterday thread "Download pictures …" about the same subject with all the necessary …​
 
Upvote 0
See the yesterday thread "Download pictures …" about the same subject with all the necessary …
It would be helpful if you could post the actual link. Rather than some vague rubbish.
 
Upvote 0
See the yesterday thread "Download pictures …" about the same subject with all the necessary …​
you mean this post

Request you to please advice a correct post so that it will be easy to ref.. thanks
 
Upvote 0
Hi Every One,
I am still not able to find a solution as getting the same error (ref screen shot on my 1st Post). It is downloading the file, but not allowing to open.
Any other idea's
 
Upvote 0
Got the solution,

This code now works perfect, thank you all for your support. :)


VBA Code:
Sub DownloadFileFromURL()

Sheets("DownloadFiles").Select
With Worksheets("DownloadFiles")
    With .Range("N2:N" & .Range("a" & .Rows.Count).End(xlUp).Row)
          .Formula = "=RIGHT(B2,LEN(B2)-FIND(""?"",B2))"
        '.Formula = "=F2"
        .Value = .Value
        '.NumberFormat = "DD-MM-YYYY"
     End With
End With

With Worksheets("DownloadFiles")
    With .Range("O2:O" & .Range("a" & .Rows.Count).End(xlUp).Row)
          .Formula = "=SUBSTITUTE(B2,N2,""&download=1"")"
        '.Formula = "=F2"
        .Value = .Value
        '.NumberFormat = "DD-MM-YYYY"
     End With
End With


Dim mainFolder As String
    mainFolder = "D:\Viral"
    If Right(mainFolder, 1) <> "\" Then
        mainFolder = mainFolder & "\"
    End If

    Dim lastRow As Long
    lastRow = Cells(Rows.Count, "A").End(xlUp).Row
 
    Dim i As Long
    Dim destinationFolder As String
    Dim url As String
    Dim errorMessage As String
    For i = 2 To lastRow
        destinationFolder = Cells(i, "A").Value
        url = Cells(i, "O").Value

    Dim objXmlHttpReq As Object
    Dim objStream As Object

Set objXmlHttpReq = CreateObject("Microsoft.XMLHTTP")
     objXmlHttpReq.Open "GET", url, False, "username", "password"
     objXmlHttpReq.send
     If objXmlHttpReq.Status = 200 Then
          Set objStream = CreateObject("ADODB.Stream")
          objStream.Open
          objStream.Type = 1
          objStream.Write objXmlHttpReq.responsebody
          objStream.SaveToFile ThisWorkbook.Path & "\" & "test1.xlsx", 2
          objStream.Close
     End If

Next i
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,904
Messages
6,175,295
Members
452,631
Latest member
a_potato

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top