[英]How to edit HTML files with Word VBA XMLHTTP object
我正在写一个Word VBA宏,该宏最终创建了等效的HTML文件。 创建此HTML文件之后,我想将它的纯HTML代码放在字符串中以进行进一步编辑(在同一宏脚本中)。 我正在处理的所有文件都通过HTTP请求访问本地服务器,而不是本地驱动器。 这是我的一些代码:
...{other code}...
Dim httpreq as Object
Dim htmlread as String
Set httpreq = CreateObject("MSXML.XMLHTTP")
...{other code}...
ActiveDocument.SaveAs2 FileName := HTMLFilePath, FileFormat: wdFormatFilteredHTML
httpreq.Open "POST", HTMLFilePath, False
httpreq.send
htmlread = httpreq.responseText
..{htmlread string is modified using VBA methods like Replace}...
如何使用HTTP方法使用修改后的字符串覆盖HTMLFilePath文件?
对于最新版本的MS Office,您可以使用Microsoft XML, v 6.0
和Microsoft HTML Object Library
在VBA窗口中,选择Tools -> References -> Microsoft XML, v 6.0
和Tools -> References -> Microsoft HTML Object Library
尝试以下代码:
Sub parse()
Dim http As New MSXML2.XMLHTTP60
Dim html As New HTMLDocument
Dim htmlread As String
With CreateObject("MSXML2.serverXMLHTTP")
.Open "GET", "http://www.google.com", False
.send
htmlread = .responseText
End With
html.body.innerHTML = htmlread 'raw full source code
Debug.Print html.body.innerHTML
'..{htmlread string is modified using VBA methods like Replace}...
html.body.innerHTML = htmlread 'edited source code
Debug.Print html.body.innerHTML
Set html = Nothing
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.