如何使用VBA从Sharepoint下载文件 [英] How to download a file from Sharepoint with VBA

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

问题描述

我正在尝试使用VBA从Sharepoint下载文件,但不知道该怎么做! 该文件实际上是一张图片(不确定是否重要吗?)并且下载正常,但是一旦进入系统,该图片将无法查看. (下面的代码) 我以为我下载的格式错误,但是(就像我说的那样)我无法弄清楚.

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.

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

推荐答案

这是我当前用于从我们的Sharepoint网站下载文件的包装器代码:

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

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.

The function DownloadFileFromWeb returns 0 if the download was successful.

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

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