简体   繁体   中英

How to edit HTML files with Word VBA XMLHTTP object

I'm writing a Word VBA macro that ultimately creates a HTML file equivalent. After this HTML file is created, I want to put it's bare HTML code in a string for further editing (in the same macro script). All my files I'm dealing with are accessed with HTTP requests to a local server, not to a local drive. Here's some code I have:

...{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}...

How do I overwrite HTMLFilePath file with modified string using HTTP methods?

For latest versions of MS Office you can use Microsoft XML, v 6.0 and Microsoft HTML Object Library

In VBA window, select Tools -> References -> Microsoft XML, v 6.0 and Tools -> References -> Microsoft HTML Object Library

Try the following code:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM