首页 > 解决方案 > 通过 Visual Basic 脚本从 Sharepoint 下载文件

问题描述

我正在尝试通过 VBScript 从共享点下载文件。

不幸的是,我在下载 Excel 文件后收到以下错误消息:

“Excel 无法打开文件 'xxxxx.xlsx',因为文件格式或文件扩展名无效。验证文件没有损坏并且文件扩展名与文件格式匹配”

我的代码剪断如下:

dim bStrm: Set bStrm = createobject("Adodb.Stream")
xHttp.Open "GET", "https://company.sharepoint.com/:x:/r/teams/xxxxx/_layouts/15/Doc.aspx?sourcedoc=%7B5AA396CB-4711-4E73-AEC5-3CB8B6E174D3%7D&file=xxxx%20Automation%xxxx.xlsx&wdOrigin=OFFICECOM-WEB.START.REC&action=default&mobileredirect=true", False
xHttp.Send

with bStrm
    .type = 1 '//binary
    .open
    .write xHttp.responseBody
    .savetofile "c:\temp\TestNow.xlsx", 2 '//overwrite
end with 



标签: sharepointvbscript

解决方案


该方法没有错,但在处理来自 URL 的下载时,您应该始终检查响应状态代码以查看它是否有效,然后再继续。

调用后Send()总是在这样的条件语句中包围响应;

If xHttp.Status = 200 Then 'Expecting a HTTP 200 OK response
    With bStrm
        .Type = 1 '//binary
        .Open
        .Write xHttp.responseBody
        .SaveToFile "c:\temp\TestNow.xlsx", 2 '//overwrite
    End With
Else
    'Check the response body for details of the error.
    MsgBox("Unexpected response: " & xHttp.Status & " - " & xHttp.StatusText, 48, "Error")
End If

因为您正在尝试从 SharePoint 网站下载,所以请求失败并返回HTTP 403 Forbidden不提供任何身份验证的可能性很大。


推荐阅读