【问题标题】:VBA download a File from OneDriveVBA 从 OneDrive 下载文件
【发布时间】:2017-05-08 22:18:52
【问题描述】:

我正在尝试从 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

这是我在即时窗口上从 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>

现在我希望在我的 OneDrive 中下载一个名为 test.xlsx 的文件。有什么办法。

更新 - 代码

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

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

【问题讨论】:

  • oRequest.Open "GET", URLofFile, False 我想。不过我不知道 false 是为了什么。
  • OneDrive 没有映射到您计算机上的驱动器号吗?如果是这样,您应该能够使用常规文件命令将文件从它复制到您的 PC。
  • @RichHolton 是的,它被映射到我的本地。但是应用程序需要从与我共享的文件夹中下载文件。这就是问题开始的地方。 OneDrive 不会将共享文件同步到本地并同步。
  • @Sid29:对不起,我看错了你的问题。
  • @GibralterTop 没有。它没有帮助。我也试过了。

标签: vba onedrive office365api


【解决方案1】:

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

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

【讨论】:

  • 是Access Token和PWD你的邮箱地址和密码吗?
猜你喜欢
  • 1970-01-01
  • 2020-07-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-09
  • 2022-12-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多