MSHTML:CreateDocumentFromString而不是CreateDocumentFromUrl

时间:2012-04-03 14:23:19

标签: vba vb6 html-parsing mshtml

我想使用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'
    ....

3 个答案:

答案 0 :(得分:13)

你可以;

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

答案 1 :(得分:1)

这是一个更好的例子。您不会获得null异常,也不会获得后期绑定。

(如果您使用WPF,只需在参考中添加System.Windows.Forms。)

Dim a As Object
        a = New mshtml.HTMLDocument

        a.open()
        a.writeln(code)
        a.close()

        Do Until a.readyState = "complete"
            System.Windows.Forms.Application.DoEvents()
        Loop


        Dim doc As mshtml.HTMLDocument = a



        Dim b As mshtml.HTMLSelectElement = doc.getElementsByTagName("Select").item("lang", 0)

答案 2 :(得分:1)

对于直接HTML代码,例如Access-Rich-Text,这样做:

Dim HTMLDoc As New HTMLDocument

HTMLDoc.Body.innerHTML = strHTMLText