VBA 从 OneDrive 下载文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43848083/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 12:33:43  来源:igfitidea点击:

VBA download a File from OneDrive

vbaonedriveoffice365api

提问by MMS

I am trying to download a file from OneDrive. I am new to API Concepts and here is Code,

我正在尝试从 OneDrive 下载文件。我是 API 概念的新手,这里是代码,

Sub OneDrive_Download()

'Declare the Object
Dim oRequest As Object

'Create and Assign Object
Set oRequest = CreateObject("MSXML2.XMLHTTP")

'Input User OneDrive URL
URL = "https://xxxx-my.sharepoint.com/personal/sidxxx_ie/"

'Post the URL in the Object
oRequest.Open "POST", URL, False

'Send Keys to the API
oRequest.send ("{""client_id"":myclientid,""CLIENT_SECRET"":myclientsecret}")


'Print the Response in the Immediate Window
 Debug.Print oRequest.ResponseText

 End Sub

And this is the Response that I got from Debug.Print on my Immediate window,

这是我从立即窗口上的 Debug.Print 得到的响应,

// Setup cta message fields.
window.$Do.when("User", 0, function ()
{
    User.setupCallToActionMessages();
});

// Other tile
var Tiles = Tiles || {};
Tiles.otherJSON = {
    'name': 'Use another account',
    'login': '',
    'imageAAD': 'other_glyph.png',
    'imageMSA': 'other_glyph.png',
    'isLive': false,
    'link': 'other',
    'authUrl': '',
    'sessionID': '',
    'domainHint': 'other'
};
</script>
</body>
</html>

Now I am looking to download a file named as test.xlsx in my OneDrive. What is the way to go about it.

现在我想在我的 OneDrive 中下载一个名为 test.xlsx 的文件。有什么方法可以解决它。

UPDATED - 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://xxx-my.sharepoint.com/personal/Sidxxx/Documents/test.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.SaveToFile "C:\testdownload.xlsx", 2

        oStream.Close

    End If

End Sub

The file is now getting downloaded. But it appears to be empty.

该文件现在正在下载。但它似乎是空的。

回答by MMS

This code works for me. Thanks everyone for your advice.

这段代码对我有用。谢谢大家的建议。

Sub DownloadFile()

'Declare the Object and URL
Dim myURL As String
Dim WinHttpReq As Object

'Assign the URL and Object to Variables
myURL = "https://xxx-my.sharepoint.com/personal/Sidxxx/Documents/test.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 "C:\testdownload.xlsx", 2

        oStream.Close

    End If

End Sub