我正在尝试从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会抛出一个错误:'此对象的访问被拒绝错误'
它在.send
我发现错误代码是 -2147024891 。
我检查了Sharepoint权限,他们应该没问题。
答案 0 :(得分:1)
我的URL使用http,但只能使用https授予对sharepoint的访问权限(至少在某个版本中)。更改URL解决了访问问题。
答案 1 :(得分:1)
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
答案 2 :(得分:0)
您需要定义身份验证用户信息。
oHttp.SetRequestHeader "Authorization", "Basic " + _
Base64Encode(authUser + ":" + authPass)
这是一些信息
http://ramblings.mcpher.com/Home/excelquirks/snippets/basicauth