如何使用VBA从Sharepoint下载文件

时间:2017-02-23 15:09:24

标签: excel vba excel-vba sharepoint

我正在尝试使用VBA从Sharepoint下载文件,但无法弄清楚如何操作! 该文件实际上是一张图片(不确定是否重要?)并且下载顺利,但是一旦进入系统,图片就无法查看。 (以下代码) 我认为我以错误的格式下载它,但是(就像我说的那样)我无法弄明白。

任何帮助将不胜感激! JP

Sub DownloadFromSharepoint()
Dim myURL As String
myURL = "https://MYSHAREPOINTSITE"

Dim WinHttpReq As Object
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\DOMAIN\temp.jpg")
        oStream.Close
    End If
End Sub

1 个答案:

答案 0 :(得分:3)

以下是我目前用于从Sharepoint网站下载文件的包装代码:

Private Declare Function URLDownloadToFile Lib "urlmon" Alias "URLDownloadToFileA" (ByVal pCaller As Long, _
    ByVal szURL As String, ByVal szFileName As String, ByVal dwReserved As Long, ByVal lpfnCB As Long) As Long

Function DownloadFileFromWeb(strURL As String, strSavePath As String) As Long
    ' strSavePath includes filename
    DownloadFileFromWeb = URLDownloadToFile(0, strURL, strSavePath, 0, 0)
End Function

如果下载成功,则DownloadFileFromWeb函数返回0。