Skip to content Skip to sidebar Skip to footer

Parsing Html To Recreate Tables In A Word Document Using Vba

Is there a way of taking html code for a table and printing out the same table in a word document using VBA (VBA should be able to parse the html code block for a table)? It is po

Solution 1:

This could be a good place to start, you will only need to check content after to see if there is any problem and then copy it to word.

    Sub PrintHTML_Table(ByVal StrTable as String)
    Dim TA()

    Dim Table_String as String
    Table_String = " " & StrTable & " "

    TA = SplitTo2DArray(Table_String, "</tr>", "</td>")

    For i = LBound(TA, 1) To UBound(TA, 1)
        For j = LBound(TA, 2) To UBound(TA, 2)
            ActiveSheet.Cells(i + 1, j + 1) = Trim(Replace(Replace(TA(i, j), "<td>", ""), "<tr>", ""))
        Next j
    Next i

    End Sub




    Public Function SplitTo2DArray(ByRef StringToSplit As String, ByRef RowSep As String, ByRef ColSep As String) As String()

        Dim Rows                    As Variant
        Dim rowNb                   As Long
        Dim Columns()               As Variant
        Dim i                       As Long
        Dim maxlineNb               As Long
        Dim lineNb                  As Long
        Dim asCells()               As String
        Dim j                       As Long

        ' Split up the table value by rows, get the number of rows, and dim a new array of Variants.
        Rows = Split(StringToSplit, RowSep)
        rowNb = UBound(Rows)
        ReDim Columns(0 To rowNb)

        ' Iterate through each row, and split it into columns. Find the maximum number of columns.
        maxlineNb = 0
        For i = 0 To rowNb
            Columns(i) = Split(Rows(i), ColSep)
            lineNb = UBound(Columns(i))
            If lineNb > maxlineNb Then
                maxlineNb = lineNb
            End If
        Next i

        ' Create a 2D string array to contain the data in <Columns>.
        ReDim asCells(0 To maxlineNb, 0 To rowNb)

        ' Copy all the data from Columns() to asCells().
        For i = 0 To rowNb
            For j = 0 To UBound(Columns(i))
                asCells(j, i) = Columns(i)(j)
            Next j
        Next i

        SplitTo2DArray = asCells()

    End Function

Post a Comment for "Parsing Html To Recreate Tables In A Word Document Using Vba"