从 vba 检查共享点上是否存在文件时拒绝访问

Access Denied when checking file exists on sharepoint from vba

我正在尝试检查 Excel 2010 VBA 中的文件是否存在于 SharePoint 2010 中。我从 another question 中获取了这段代码。

Function checkFile(URLStr As String) As Boolean
Dim oHttpRequest As Object
Set oHttpRequest = New MSXML2.XMLHTTP60
With oHttpRequest
    .Open "GET", URLStr, False
    .setRequestHeader "Cache-Control", "no-cache"
    .setRequestHeader "Pragma", "no-cache"
    .setRequestHeader "If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT"
    .send
End With
If oHttpRequest.Status = 200 Then
    checkFile = True
Else
    checkFile = False
End If
End Function

当我这样做时,vba 抛出一个错误:'Access Denied Error for this object'

.send

失败

我发现错误代码是-2147024891

我检查了 Sharepoint 权限,它们应该没问题。

您需要定义认证用户信息。

oHttp.SetRequestHeader "Authorization", "Basic " + _
        Base64Encode(authUser + ":" + authPass)

这是一些信息

http://ramblings.mcpher.com/Home/excelquirks/snippets/basicauth

How to pass authentication credentials in VBA

我的 URL 使用 http,但仅通过 https 授予对 sharepoint 的访问权限(至少在特定版本中)。更改 URL 解决了访问问题。

Function checkFile( URLStr As String) As Boolean
Dim oHttpRequest As Object
Dim GetResult As  Integer
Set oHttpRequest = New MSXML2.XMLHTTP60
With oHttpRequest
    .Open "GET", URLStr, False
    .setRequestHeader "Cache-Control", "no-cache"
    .setRequestHeader "Pragma", "no-cache"
    .setRequestHeader "If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT"

End With
On Error GoTo HttpError
    oHttpRequest.send '!!!!!!here code stops!!!!!!!
HttpError:
     GetResult    = 00

GetResult = oHttpRequest.Status
If  GetResult  = 200 Then
    checkFile = True
Else
    checkFile = False
End If
End Function