2017-08-09 51 views
1

我在寫一個Word VBA宏,最終創建一個等效的HTML文件。在創建這個HTML文件後,我想將它的純HTML代碼放入一個字符串中以供進一步編輯(在同一個宏腳本中)。我處理的所有文件都通過HTTP請求訪問本地服務器,而不是本地驅動器。 下面是一些代碼,我有:如何使用Word VBA編輯HTML文件XMLHTTP對象

...{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文件?

回答

2

對於微軟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 
+0

感謝,還怎麼做我上傳/保存更新的文檔到服務器上的實際文檔? – Steven

+0

@Steven如果你要將它保存在本地驅動器中,你可以將它保存爲像'ActiveDocument.SaveAs Filename:= path FileFormat:= xlHtml'這樣的html文件。但事情並不那麼簡單。我不是這方面的專業人士,但任何圖像,C​​SS文件,其他附件等也應保存。但是,如果要將其上載到服務器,則無需這樣做。就我所知,您可以[通過FTP將文件傳輸到服務器](https://officetricks.com/ftp-server-file-transfer-with-excel-macro/)。 – Tehscript