如何用VBA从Sharepoint下载文件

时间:2023-01-27 12:13:08

I am trying to download a file from Sharepoint with VBA and can not figure out how to do it! The file is actually a picture (not sure if that matters?) and the download goes fine, but the picture isn't view-able once it gets onto the system. (Code below) I'm thinking that I'm downloading it in the wrong format, but (like I said) I can't figure it out.

我正在尝试用VBA从Sharepoint下载一个文件,却不知道怎么做!该文件实际上是一张图片(不确定这是否重要?),下载过程正常,但图片一旦进入系统就无法查看。(下面的代码)我认为我下载的格式是错误的,但是(就像我说的)我想不出来。

Any help would be greatly appreciated! 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 个解决方案

#1


1  

Here is the wrapper code I currently use to download files from our Sharepoint site:

下面是我目前用来从我们的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

The function DownloadFileFromWeb returns 0 if the download was successful.

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

#1


1  

Here is the wrapper code I currently use to download files from our Sharepoint site:

下面是我目前用来从我们的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

The function DownloadFileFromWeb returns 0 if the download was successful.

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