我可以在本地机器上使用 VBA 动态保存网页源 html 吗
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13193210/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can i save a webpage source html dynamically using VBA in my local machine
提问by Arup Rakshit
How Can i save a webpage source html dynamically using VBA in my local machine?
如何在本地机器上使用 VBA 动态保存网页源 html?
Thanks, Arup
谢谢,阿鲁普
回答by Jamie Bull
This is a simple way of getting and saving an html file to your temp folder for working on. It requires a reference to either Microsoft XML 3.0 or Microsoft XML 6.0.
这是获取 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
To delete the file when you're finished with it try:
要在完成后删除文件,请尝试:
Sub DeleteFile(ByVal FileToDelete As String)
SetAttr FileToDelete, vbNormal
Kill FileToDelete
End Sub