簡體   English   中英

如何使用Word VBA XMLHTTP對象編輯HTML文件

[英]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.0Microsoft HTML Object Library

在VBA窗口中,選擇Tools -> References -> Microsoft XML, v 6.0Tools -> 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM