解析HTML以使用VBA在Word文档中重新创建表 [英] Parsing HTML to recreate tables in a Word Document using VBA

查看:136
本文介绍了解析HTML以使用VBA在Word文档中重新创建表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法使用html代码进行表格打印,使用VBA打印出Word文档中的同一个表(VBA应该可以解析一个表的html代码块)?

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)?

可以将表的内容复制到Word中创建的新表中,但是可以使用html代码和vba重新创建一个表?

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?

编辑:
谢谢到R3uK:这是VBA脚本的第一部分,从文件中读取一行HTML代码,并使用R3uK的代码将其打印到excel工作表中:

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


推荐答案

这可能是一个好的开始,你只需要检查内容后看看是否有任何问题,然后将其复制到单词。

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

这篇关于解析HTML以使用VBA在Word文档中重新创建表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆