如何在本地机器中使用 VBA 动态保存网页源 html?
谢谢,奥雅纳
这是获取 html 文件并将其保存到临时文件夹以进行处理的简单方法。它需要对 Microsoft XML 3.0 或 Microsoft XML 6.0 的引用。
Sub GetHTTP()
Dim objHttp As Object
Dim CachedFilePath As String
Set objHttp = CreateObject("MSXML2.ServerXMLHTTP")
Call objHttp.Open("GET", "http://www.example.com/", False)
Call objHttp.Send("")
CachedFilePath = Environ("temp") & "\" & "ReplaceThisWithFilename" & ".html"
Call CreateFile(CachedFilePath, objHttp.ResponseText)
End Sub
Function CreateFile(FileName As String, Contents As String) As String
' creates file from string contents
Dim tempFile As String
Dim nextFileNum As Long
nextFileNum = FreeFile
tempFile = FileName
Open tempFile For Output As #nextFileNum
Print #nextFileNum, Contents
Close #nextFileNum
CreateFile = tempFile
End Function
要在完成后删除文件,请尝试:
Sub DeleteFile(ByVal FileToDelete As String)
SetAttr FileToDelete, vbNormal
Kill FileToDelete
End Sub