Excel VBA 如何从网络(未缓存)中读取文本文件?
Excel VBA how to read in text file from web (not cached)?
我一直在使用以下代码从网络上读取文本文件:
'import the text file into a string
Function DownloadTextFile(URL As String) As String
On Error GoTo Err_GetFromWebpage
Dim objWeb As Object
Dim strXML As String
' Instantiate an instance of the web object
Set objWeb = CreateObject("Microsoft.XMLHTTP")
' Pass the URL to the web object, and send the request
objWeb.Open "GET", URL, False
objWeb.send
' Look at the HTML string returned
strXML = objWeb.responseText
DownloadTextFile = strXML
End_GetFromWebpage:
' Clean up after ourselves!
Set objWeb = Nothing
Exit Function
Err_GetFromWebpage:
' Just in case there's an error!
MsgBox Err.Description & " (" & Err.Number & ")"
Resume End_GetFromWebpage
End Function
第一次工作正常,但是当我对文本文件进行更改时,它没有反映在返回的字符串中。就好像 Excel 正在缓存文件。我该怎么做才能解决这个问题?谢谢!
没有 URL
就无法测试它,但是您尝试过使用 setRequestHeader with no-cache 吗?
示例:
objWeb.setRequestHeader "Content-Type", "text/xml"
objWeb.setRequestHeader "Cache-Control", "no-cache"
objWeb.setRequestHeader "Pragma", "no-cache"
除了按照@dee 的建议设置请求 headers 外,我还将 "Microsoft.XMLHTTP" 更改为 "Msxml2.ServerXMLHTTP",现在可以使用了!这是最终代码:
'import the text file into a string
Function DownloadTextFile(URL As String) As String
On Error GoTo Err_GetFromWebpage
Dim objWeb As Object
Dim strXML As String
' Instantiate an instance of the web object
Set objWeb = CreateObject("Msxml2.ServerXMLHTTP")
' Pass the URL to the web object
objWeb.Open "GET", URL, False
'don't cache the file
objWeb.setRequestHeader "Content-Type", "text/xml"
objWeb.setRequestHeader "Cache-Control", "no-cache"
objWeb.setRequestHeader "Pragma", "no-cache"
'send the request
objWeb.send
' Look at the HTML string returned
strXML = objWeb.responseText
DownloadTextFile = strXML
End_GetFromWebpage:
' Clean up after ourselves!
Set objWeb = Nothing
Exit Function
Err_GetFromWebpage:
' Just in case there's an error!
MsgBox Err.Description & " (" & Err.Number & ")"
Resume End_GetFromWebpage
End Function
我一直在使用以下代码从网络上读取文本文件:
'import the text file into a string
Function DownloadTextFile(URL As String) As String
On Error GoTo Err_GetFromWebpage
Dim objWeb As Object
Dim strXML As String
' Instantiate an instance of the web object
Set objWeb = CreateObject("Microsoft.XMLHTTP")
' Pass the URL to the web object, and send the request
objWeb.Open "GET", URL, False
objWeb.send
' Look at the HTML string returned
strXML = objWeb.responseText
DownloadTextFile = strXML
End_GetFromWebpage:
' Clean up after ourselves!
Set objWeb = Nothing
Exit Function
Err_GetFromWebpage:
' Just in case there's an error!
MsgBox Err.Description & " (" & Err.Number & ")"
Resume End_GetFromWebpage
End Function
第一次工作正常,但是当我对文本文件进行更改时,它没有反映在返回的字符串中。就好像 Excel 正在缓存文件。我该怎么做才能解决这个问题?谢谢!
没有 URL
就无法测试它,但是您尝试过使用 setRequestHeader with no-cache 吗?
示例:
objWeb.setRequestHeader "Content-Type", "text/xml"
objWeb.setRequestHeader "Cache-Control", "no-cache"
objWeb.setRequestHeader "Pragma", "no-cache"
除了按照@dee 的建议设置请求 headers 外,我还将 "Microsoft.XMLHTTP" 更改为 "Msxml2.ServerXMLHTTP",现在可以使用了!这是最终代码:
'import the text file into a string
Function DownloadTextFile(URL As String) As String
On Error GoTo Err_GetFromWebpage
Dim objWeb As Object
Dim strXML As String
' Instantiate an instance of the web object
Set objWeb = CreateObject("Msxml2.ServerXMLHTTP")
' Pass the URL to the web object
objWeb.Open "GET", URL, False
'don't cache the file
objWeb.setRequestHeader "Content-Type", "text/xml"
objWeb.setRequestHeader "Cache-Control", "no-cache"
objWeb.setRequestHeader "Pragma", "no-cache"
'send the request
objWeb.send
' Look at the HTML string returned
strXML = objWeb.responseText
DownloadTextFile = strXML
End_GetFromWebpage:
' Clean up after ourselves!
Set objWeb = Nothing
Exit Function
Err_GetFromWebpage:
' Just in case there's an error!
MsgBox Err.Description & " (" & Err.Number & ")"
Resume End_GetFromWebpage
End Function