Skip to content Skip to sidebar Skip to footer

Copy A Specific Format Table From Outlook To Email

I need to copy many tables from outlook to Excel at work. I know how to use .getElementsByTagName('table') to do it. However, my company merges and split some of the table cells.

Solution 1:

This should do the trick. It breaks the elements out into a collection first, so you don't have to worry about the variation in Cells.Length. In the HTML table some rows have 4 cells, and others have only one that spans the row 2x high. Which is why it wasn't populating your table properly

You'll have to reformat the target, but it will keep the data under Header 2 separate, and avoid the wasted rows.

enter image description here

Replace This:

With Worksheets("sheet2")
For x = 0 To oElColl(0).Rows.Length - 1
    For y = 0 To oElColl(0).Rows(x).Cells.Length - 1
        .Range("a1").Offset(x, y).Value = oElColl(0).Rows(x).Cells(y).innerText
    Next y
Next x

End With

With This:

Dim colTabEle As New Collection
Dim i As Integer: i = 1

For x = 0 To oElColl(0).Rows.Length - 1
    For y = 0 To oElColl(0).Rows(x).Cells.Length - 1
        colTabEle.Add oElColl(0).Rows(x).Cells(y).innerText
    Next y
Next x

With Worksheets("sheet2")
    For x = 0 To (colTabEle.Count + 1) / 4
        For y = 0 To 2
            .Range("a1").Offset(x, y).Value = colTabEle(i)
            If IsNumeric(colTabEle(i)) Then
                i = i + 2
            Else
                i = i + 1
            End If
        Next y
    Next x
End With

Post a Comment for "Copy A Specific Format Table From Outlook To Email"