(Solved) – Extract innerText from HTML div tags un a local file

  • by

I was able to extract table from a local HTML file using this code below, but I have no idea how to modify it to extract innertext from nested div tags in the sample HTML code below.

Sub HTML_Table_To_Excel() 

Dim htm As Object 
Dim Tr As Object 
Dim Td As Object 
Dim Tab1 As Object
Dim file as String
'Replace the file path with your own 
file = "c:your_File.html"

'Determine the next file number available for use by the FileOpen function
TextFile = FreeFile

'Open the text file
 Open file For Input As TextFile

 'Create HTMLFile Object 
 Set HTML_Content = CreateObject("htmlfile") 
 HTML_Content.body.innerHtml = Input(LOF(TextFile), TextFile)

 Column_Num_To_Start = 1 
 iRow = 2 
 iCol = Column_Num_To_Start 
 iTable = 0 

'Loop Through Each Table and Download it to Excel in Proper Format 
For Each Tab1 In HTML_Content.getElementsByTagName("table") 
With HTML_Content.getElementsByTagName("table")(iTable) 
    For Each Tr In .Rows 
    For Each Td In Tr.Cells 
        Sheets(1).Cells(iRow, iCol).Select 
        Sheets(1).Cells(iRow, iCol) = Td.innerText 
        iCol = iCol   1 
        Next Td 
        iCol = Column_Num_To_Start 
        iRow = iRow   1 
    Next Tr 
End With 

iTable = iTable   1 
iCol = Column_Num_To_Start 
iRow = iRow   1 
Next Tab1 

MsgBox "Process Completed" 
End Sub

And here is the HTML code without tables

enter image description here

I want to copy all the report-row-label classes into the Sheet

Leave a Reply

Your email address will not be published. Required fields are marked *