我可以在本地计算机中使用VBA动态保存网页源html

时间:2012-11-02 10:12:09

标签: excel-vba vba excel

如何在本地计算机上使用VBA动态保存网页源html?

谢谢, 奥雅纳

1 个答案:

答案 0 :(得分:2)

这是获取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