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 =0To oElColl(0).Rows.Length -1For y =0To oElColl(0).Rows(x).Cells.Length -1
        .Range("a1").Offset(x, y).Value= oElColl(0).Rows(x).Cells(y).innerText
    Next y
Next x

EndWith

With This:

Dim colTabEle AsNew Collection
Dim i AsInteger: i = 1For x = 0To oElColl(0).Rows.Length - 1For y = 0To 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 = 0To (colTabEle.Count + 1) / 4For y = 0To2
            .Range("a1").Offset(x, y).Value = colTabEle(i)
            If IsNumeric(colTabEle(i)) Then
                i = i + 2Else
                i = i + 1EndIfNext y
    Next x
EndWith

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