parsingHTML以使用VBA重新创buildWord文档中的表

有没有一种方法来获取表格的HTML代码,并使用VBA打印出一个Word文档中的同一个表(VBA应该能够parsing表格的HTML代码块)?

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

对于这一切,哪里可以开始研究?

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

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 

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

  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