-1

I am trying the following code

Sub myConSP()
    Dim oHtmlSP As HTMLDocument
    Dim tSPIndex As HTMLDivElement
    Dim tSPIdx As HTMLDivElement
    Dim tables As Object
    
    Set oHtmlSP = New HTMLDocument
    With CreateObject("WINHTTP.WinHTTPRequest.5.1")
        .Open "GET", "C:\Users\Future\Desktop\Test.html", False
        .send
        oHtmlSP.body.innerHTML = .responseText
    End With
    Set tables = oHtmlSP.querySelectorAll("table[width='100%'] table:first-child")
    Debug.Print tables.Length
End Sub

I tried different ways to read the HTML local file through that code. How can I refer to the local file HTML file in such a code?

YasserKhalil
  • 9,138
  • 7
  • 36
  • 95

2 Answers2

1

I think you are overcomplicating things.

Try:

Sub myConSP()
    Dim oHtmlSP As HTMLDocument

Set oHtmlSP = New HTMLDocument
oHtmlSP.body.innerHTML = "full_path"

'process the file
End Sub

Ron Rosenfeld
  • 53,870
  • 7
  • 28
  • 60
  • Thank you very much. I didn't complicate things but tries to solve another problem in a different way. Using your approach, and tried that `Set tables = oHtmlSP.querySelectorAll("table[width='100%'] table:first-child")` Then when trying to display the length `Debug.Print tables.Length` I got null as a result. You can have a look at this url http://try.jsoup.org/~LsArJXLumBWg2HOgluVzOXm9bRs – YasserKhalil Apr 10 '21 at 14:15
  • 1
    @YasserKhalil glad you found a solution that works for you. – Ron Rosenfeld Apr 10 '21 at 18:35
0

This approach solved the problem. I have used late binding as for the variable html and this is important as when using the early binding, I got invalid use of NULL as for the property of tables length.

Sub NewTest()
    Dim fStream As Object, html As Object, tables As Object, sFolder As String, sFile As String

    sFolder = ThisWorkbook.Path & "\"
    sFile = "Test.html"
  
    Set fStream = CreateObject("ADODB.Stream")
    Set html = CreateObject("HtmlFile")

    With fStream
        .Charset = "UTF-8"
        .Open
        .LoadFromFile sFolder & sFile
        html.body.innerHTML = .ReadText
        .Close
    End With
  
    Set tables = html.querySelectorAll("table[width='100%'] table:first-child")
    Debug.Print tables.Length
End Sub
YasserKhalil
  • 9,138
  • 7
  • 36
  • 95
  • This solved the problem at this link too (a nother question related to that question) https://stackoverflow.com/questions/66983813/css-selector-invalid-use-of-null-tables-length-property – YasserKhalil Apr 10 '21 at 15:55