Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Xpath + VBA

Status
Not open for further replies.

kalle82

Technical User
Apr 2, 2009
163
SE
Hello!

Im trying to use Xpath to do collect some data from a page i save, RESULTS ON THE PAGE DIFFER.

I save everything(the html source) into my "C" Variable.

Thing is that, I can parse that like a LOOONGGG string(I do it with the mid() function and some ohter), but now I want to use the more flexible xpath cause there times when the search results generated from my page fails to load due to the string parsing.

So my questions are:
1. Why can't I get it to work? It does not load the html string, into the dom object? What do I do wrong?

2. I know the code structure for doing xpath queries, but do i do that with the dom object is it... domobject.SelectSingleNode("xpathquery")

I have attached my code below!

Code:
Dim ie As Object
Set ie = CreateObject("internetexplorer.application")

ie.Visible = False

ie.Navigate "[URL unfurl="true"]https://the-page-with-my-data.com"[/URL]
Do While ie.ReadyState <> 4
  DoEvents

Loop

C = ie.Document.body.innerhtml

Dim oXml As New MSXML2.DOMDocument

'Assign

UserForm1.TextBox88.Text = C


Dim oDom As MSXML2.DOMDocument
'
'    'Create the DomDocument Object
    Set oDom = CreateObject("MSXML2.DOMDocument")
'
'    'Load entire Document before moving on
    oDom.async = False
'
'    'Don't Validate
    oDom.validateOnParse = False
'
    oDom.Load (ie.Document.body.innerhtml)
'
    MsgBox oDom.XML
 
HTML is not the same as XML. Is your file well formed? Otherwise loading fails. An example (with a little help of
Userform controls: webbrowser control, two multiline textboxes and two commandbuttons.
References: SHDocVw - Microsoft Internet Controls (ieframe.dll, IE8), MSXML2 - Microsoft XML, v6 (msxml6.dll), MSHTML - Microsoft HTML Object Library (MSHTML.TLB).
Here's the test code on the userform.
Code:
Private WithEvents oWB As SHDocVw.WebBrowser

Dim oHTMLDoc As MSHTML.HTMLDocument
Dim oXMLDom As MSXML2.DOMDocument

Private Sub CommandButton1_Click()
Me.WebBrowser1.Navigate "[URL unfurl="true"]http://tek-tips.com"[/URL]
End Sub

Private Sub CommandButton2_Click()
Set oXMLDom = New MSXML2.DOMDocument
oXMLDom.async = False
oXMLDom.validateOnParse = False
If oXMLDom.loadXML(Me.TextBox1.Text) = False Then
    With oXMLDom.parseError
        strErrText = "XML Document failed to load " & _
            "due the following error." & vbCrLf & _
            "Error #: " & .errorCode & ": " & .reason & _
            "Line #: " & .Line & vbCrLf & _
            "Line Position: " & .linepos & vbCrLf & _
            "Position In File: " & .filepos & vbCrLf & _
            "Source Text: " & .srcText & vbCrLf & _
            "Document URL: " & .URL
     End With
    MsgBox strErrText, vbExclamation
Else
    Me.TextBox2.Text = oXMLDom.XML
End If
End Sub

Private Sub oWB_DocumentComplete(ByVal pDisp As Object, URL As Variant)
If oWB.ReadyState = READYSTATE_COMPLETE Then
    Set oHTMLDoc = Me.WebBrowser1.Document
    Me.TextBox1.Text = oHTMLDoc.body.innerHTML
End If
End Sub

Private Sub UserForm_Initialize()
Set oWB = Me.WebBrowser1
End Sub

Private Sub UserForm_Terminate()
Set oWB = Nothing
End Sub

combo
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top