如何使用 VBA 从 Sharepoint 下载文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42419486/
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
How to download a file from Sharepoint with VBA
提问by JP1
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 下载文件,但不知道该怎么做!该文件实际上是一张图片(不确定这是否重要?)并且下载可以正常进行,但是一旦进入系统就无法查看该图片。(下面的代码)我想我正在以错误的格式下载它,但是(就像我说的)我无法弄清楚。
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
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
回答by Tragamor
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。