excel 如何使用SharePoint下载文件?

xiozqbni  于 2023-10-22  发布在  其他
关注(0)|答案(2)|浏览(263)

我正在尝试使用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
8fsztsew

8fsztsew1#

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

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。

kzipqqlq

kzipqqlq2#

URLDownloadToFile函数对我不起作用。它下载了一些html代码inestead的Excel文件
仅对于Excel文件,我使用Excel.Application对象在后台打开文件,然后将其保存到本地文件夹中:

Public Sub DownloadExcelFileFromSharepoint(ByVal SharepointFileRoute As String, ByVal TargetDestinationFile As String)
    'No neccesary reference to Microsoft Excel Object Library, using late binding

    Dim oExcelApplication As Object 'Excel.Application
    Dim oExcelWorkbook As Object 'Excel.Workbook

    Set oExcelApplication = CreateObject("Excel.Application")
    Set oExcelWorkbook = oExcelApplication.Workbooks.Open(SharepointFileRoute, ReadOnly:=True)

    ' Don't display alerts on saving, so if TargetDestinationFile exists will be overwrited
    oExcelApplication.DisplayAlerts = False
    oExcelWorkbook.SaveAs TargetDestinationFile
    oExcelApplication.DisplayAlerts = True

    oExcelWorkbook.Close
    Set oExcelWorkbook = Nothing
    
    oExcelApplication.Quit
    Set oExcelApplication = Nothing
End Sub

获得正确的URL对我来说并不明显。只是复制Sharepoint的URL,而文件在浏览器中打开,并使用它与此功能不会工作。我不得不在Excel中使用打开文件对话框打开文件,写入shareponit文件夹并导航到文件子文件夹。我得到了这样的东西:

https://michelingroup.sharepoint.com/sites/TestSite/Shared documents/TestFile.xlsx

相关问题