VBA从OneDrive下载文件 [英] VBA download a File from OneDrive

查看:203
本文介绍了VBA从OneDrive下载文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

这是我从即时"窗口中的Debug.Print获得的响应,

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

// 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>

现在,我要在OneDrive中下载一个名为test.xlsx的文件.怎么办呢?

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

已更新-代码

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.

推荐答案

此代码对我有用.谢谢大家的建议.

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

这篇关于VBA从OneDrive下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆