MSHTML:CreateDocumentFromString而不是CreateDocumentFromUrl

mwo*_*e02 4 vb6 vba mshtml html-parsing

我想使用MSHTML库来解析我在字符串变量中的一些HTML.但是,我无法弄清楚如何做到这一点.我可以轻松地解析给定已知URL的网页内容,但不能直接解析源HTML.这可能吗?如果是这样,怎么样?

Public Sub ParseHTML(sHTML As String)
Dim oHTML As New HTMLDocument, oDoc As HTMLDocument

    'This works:'
    Set oDoc = oHTML.createDocumentFromUrl("http://www.google.com", "")

    'I would like to do the following but no such method actually exists:'
    Set oDoc = oHTML.createDocumentFromString(sHTML)

    ....
    'Parse the HTML using the oDoc variable'
    ....
Run Code Online (Sandbox Code Playgroud)

Ale*_* K. 14

您可以;

Dim odoc As Object

Set odoc = CreateObject("htmlfile") '// late binding

'// or:
'// Set odoc = New HTMLDocument 
'// for early binding

odoc.open
odoc.write "<p> In his house at R'lyeh, dead <b>Cthulhu</b> waits dreaming</p>"
odoc.Close
MsgBox odoc.body.outerHTML
Run Code Online (Sandbox Code Playgroud)

  • 太好了!别人注意:当我尝试声明`odoc As HTMLDocument`时,我在VBA中收到编译错误:*编译错误:标记为受限制的函数或接口,或者该函数使用Visual Basic*中不支持的自动化类型.将声明更改为`odoc As Object`(如此答案清楚地显示)修复了问题. (4认同)