【发布时间】:2020-12-28 01:04:15
【问题描述】:
我有一个文件(主题文件)存储在 Sharepoint 上,首先需要下载到 temp 目录中,然后才能加载到 word 中。这工作了一段时间,但最近我收到“拒绝访问错误”。
我环顾四周并测试了其他库 CreateObject("MSXML2.ServerXMLHTTP.6.0") 而不是 CreateObject("Microsoft.XMLHTTP")。
有趣的是,我没有收到带有CreateObject("MSXML2.ServerXMLHTTP.6.0") 的访问错误消息,而是下载了一个带有此错误的页面:
[![错误信息截图][1]][1]
我们无法让您登录 您的浏览器当前设置为阻止 cookie。您需要允许 cookie 使用此服务。 Cookie 是存储在您计算机上的小型文本文件,当您登录时会告诉我们。要了解如何允许 Cookie,请查看您的网络浏览器中的在线帮助。
我希望有人知道为什么会出现这个错误以及如何解决它
这是我使用的代码。
Public Sub Download(ByVal URL As String, ByVal FilePath As String, Optional ByVal Overwrite As Boolean = True)
Dim iOverwrite, oStrm
If (IsNull(Overwrite) Or Overwrite) Then
iOverwrite = 2
Else
iOverwrite = 1
End If
Dim HttpReq As Object
'NOTE: There are some issues downloading if not properly logged in! May need to loggin sharepoint again
' https://www.codeproject.com/Questions/1101499/Download-files-from-API-using-vbscript-cmd-prompt
' Based on https://stackoverflow.com/questions/22938194/xmlhttp-request-is-raising-an-access-denied-error
'Set HttpReq = CreateObject("MSXML2.ServerXMLHTTP.6.0")
Set HttpReq = CreateObject("Microsoft.XMLHTTP")
'Set HttpReq = CreateObject("MSXML2.ServerXMLHTTP.3.0")
HttpReq.Open "GET", URL, False, "username", "password"
On Error GoTo ErrorHandler
HttpReq.send
On Error GoTo 0
If HttpReq.Status = 200 Then
Set oStrm = CreateObject("ADODB.Stream")
oStrm.Open
oStrm.Type = 1
oStrm.Write HttpReq.responseBody
oStrm.SaveToFile FilePath, iOverwrite ' 1 = no overwrite, 2 = overwrite
oStrm.Close
End If
Exit Sub
ErrorHandler:
MsgBox "The file could not be downloaded. Verify that you are logged in SharePoint with word and browser.", vbCritical, "Download error"
Debug.Print "Download - Error Downloading file will not be downloaded - Error #: '" & Err.Number & "'. Error description: " & Err.description
End Sub```
[1]: https://i.stack.imgur.com/pdH6v.png
【问题讨论】:
标签: vba sharepoint ms-word download