Parsing HTML to recreate tables in a Word Document

2019-09-06 03:35发布

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 possible to take the contents of the table and copy them into a new table created in Word, however is it possible to recreate a table using the html code and vba?

For any of this, where can one begin to research?

EDIT: Thanks to R3uK: here is the first portion of the VBA script which reads a line of html code from a file and uses R3uK's code to print it to the excel worksheet:

Private Sub button1_Click()

    Dim the_string As String
    the_string = Trim(ImportTextFile("path\to\file.txt"))
    ' still working on removing new line characters
    Call PrintHTML_Table(the_string)

End Sub

Public Function ImportTextFile(strFile As String) As String

    ' http://mrspreadsheets.com/1/post/2013/09/vba-code-snippet-22-read-entire-text-file-into-string-variable.html
    Open strFile For Input As #1
    ImportTextFile = Input$(LOF(1), 1)
    Close #1

End Function

' Insert R3uK's portion of the code here

1条回答
小情绪 Triste *
2楼-- · 2019-09-06 04:17

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
查看更多
登录 后发表回答