Copy a specific format table from outlook to email

梦想的初衷 提交于 2019-12-13 01:20:42

问题


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. Could someone have a look on it please?

This is the table I have in my outlook:

This is how I would like to copy it on my Excel:

This is the result:

VBA code:

Option Explicit

Public fso As New FileSystemObject
Public objApp As Outlook.Application
Public oItem As Outlook.MailItem
Sub importOutlookTable()

 Set oItem = GetCurrentItem()

    oItem.display

Dim oHTML As MSHTML.HTMLDocument: Set oHTML = New MSHTML.HTMLDocument
Dim oElColl As MSHTML.IHTMLElementCollection
With oHTML
    .body.innerHTML = oItem.HTMLBody
    Set oElColl = .getElementsByTagName("table")
End With

'import in Excel
Dim x As Long, y As Long

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

Set objApp = Nothing
Set oItem = Nothing
Set oHTML = Nothing
Set oElColl = Nothing
End Sub

Function GetCurrentItem() As Object

    Set objApp = CreateObject("Outlook.Application")
    On Error Resume Next
    Select Case TypeName(objApp.ActiveWindow)
        Case "Explorer"
            Set GetCurrentItem = objApp.ActiveExplorer.Selection.Item(1)
        Case "Inspector"
            Set GetCurrentItem = objApp.ActiveInspector.CurrentItem
    End Select

    Set objApp = Nothing
End Function

html code on my outlook:

</head>

<body lang="ZH-TW" link="#0563C1" style='text-justify-trim:punctuation' vlink=
"#954F72">
    <div class="WordSection1">
        <table border="0" cellpadding="0" cellspacing="0" class=
        "MsoNormalTable" style=
        'width:203.0pt;margin-left:.1pt;border-collapse:collapse' width="271">
            <tr style='height:15.75pt'>
                <td nowrap rowspan="2" style=
                'width:60.0pt;border:solid windowtext 1.0pt;border-bottom:solid black 1.0pt;padding:0cm 1.4pt 0cm 1.4pt;height:15.75pt'
                width="80">
                    <p class="MsoNormal" style=
                    'font-weight: bold; text-align: center'><span lang="EN-US"
                    style='font-family:"Arial",sans-serif'>header 1</span></p>
                </td>

                <td nowrap style=
                'width:83.0pt;border-top:solid windowtext 1.0pt;border-left:none;border-bottom:none;border-right:solid windowtext 1.0pt;padding:0cm 1.4pt 0cm 1.4pt;height:15.75pt'
                width="111">
                    <p class="MsoNormal" style=
                    'font-weight: bold; text-align: center'><span lang="EN-US"
                    style='font-family:"Arial",sans-serif'>header 2</span></p>
                </td>

                <td nowrap rowspan="2" style=
                'width:60.0pt;border-top:solid windowtext 1.0pt;border-left:none;border-bottom:solid black 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 1.4pt 0cm 1.4pt;height:15.75pt'
                width="80">
                    <p class="MsoNormal" style=
                    'font-weight: bold; text-align: center'><span lang="EN-US"
                    style='font-family:"Arial",sans-serif'>header 4</span></p>
                </td>

                <td height="21" style='height:15.75pt;border:none' width="0">
                </td>
            </tr>

            <tr style='height:16.5pt'>
                <td nowrap style=
                'width:83.0pt;border-top:none;border-left:none;border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 1.4pt 0cm 1.4pt;height:16.5pt'
                width="111">
                    <p class="MsoNormal" style=
                    'font-weight: bold; text-align: center'><span lang="EN-US"
                    style='font-family:"Arial",sans-serif'>header 3</span></p>
                </td>

                <td height="22" style='height:16.5pt;border:none' width="0">
                </td>
            </tr>

            <tr style='height:18.0pt'>
                <td nowrap rowspan="3" style=
                'width:60.0pt;border-top:none;border-left:solid windowtext 1.0pt;border-bottom:solid black 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 1.4pt 0cm 1.4pt;height:18.0pt'
                width="80">
                    <p class="MsoNormal" style='text-align: center'><span lang=
                    "EN-US" style=
                    'font-family:"Arial",sans-serif'>apple</span></p>
                </td>

                <td nowrap rowspan="2" style=
                'width:83.0pt;border:none;border-right:solid windowtext 1.0pt;padding:0cm 1.4pt 0cm 1.4pt;height:18.0pt'
                width="111">
                    <p class="MsoNormal" style='text-align: center'><span lang=
                    "EN-US" style=
                    'font-family:"Arial",sans-serif'>red</span></p>
                </td>

                <td nowrap rowspan="3" style=
                'width:60.0pt;border-top:none;border-left:none;border-bottom:solid black 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 1.4pt 0cm 1.4pt;height:18.0pt'
                width="80">
                    <p class="MsoNormal" style='text-align: center'><span lang=
                    "EN-US" style='font-family:"Arial",sans-serif'>4</span></p>
                </td>

                <td height="24" style='height:18.0pt;border:none' width="0">
                </td>
            </tr>

            <tr style='height:18.0pt'>
                <td height="24" style='height:18.0pt;border:none' width="0">
                </td>
            </tr>

            <tr style='height:15.75pt'>
                <td nowrap style=
                'width:83.0pt;border-top:none;border-left:none;border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 1.4pt 0cm 1.4pt;height:15.75pt'
                width="111">
                    <p class="MsoNormal" style='text-align: center'><span lang=
                    "EN-US" style=
                    'font-family:"Arial",sans-serif'>fruit</span></p>
                </td>

                <td height="21" style='height:15.75pt;border:none' width="0">
                </td>
            </tr>

            <tr style='height:18.0pt'>
                <td nowrap rowspan="3" style=
                'width:60.0pt;border-top:none;border-left:solid windowtext 1.0pt;border-bottom:solid black 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 1.4pt 0cm 1.4pt;height:18.0pt'
                width="80">
                    <p class="MsoNormal" style='text-align: center'><span lang=
                    "EN-US" style=
                    'font-family:"Arial",sans-serif'>banana</span></p>
                </td>

                <td nowrap rowspan="2" style=
                'width:83.0pt;border:none;border-right:solid windowtext 1.0pt;padding:0cm 1.4pt 0cm 1.4pt;height:18.0pt'
                width="111">
                    <p class="MsoNormal" style='text-align: center'><span lang=
                    "EN-US" style=
                    'font-family:"Arial",sans-serif'>yellow</span></p>
                </td>

                <td nowrap rowspan="3" style=
                'width:60.0pt;border-top:none;border-left:none;border-bottom:solid black 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 1.4pt 0cm 1.4pt;height:18.0pt'
                width="80">
                    <p class="MsoNormal" style='text-align: center'><span lang=
                    "EN-US" style='font-family:"Arial",sans-serif'>3</span></p>
                </td>

                <td height="24" style='height:18.0pt;border:none' width="0">
                </td>
            </tr>

            <tr style='height:18.0pt'>
                <td height="24" style='height:18.0pt;border:none' width="0">
                </td>
            </tr>

            <tr style='height:15.75pt'>
                <td nowrap style=
                'width:83.0pt;border-top:none;border-left:none;border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 1.4pt 0cm 1.4pt;height:15.75pt'
                width="111">
                    <p class="MsoNormal" style='text-align: center'><span lang=
                    "EN-US" style=
                    'font-family:"Arial",sans-serif'>fruit</span></p>
                </td>

                <td height="21" style='height:15.75pt;border:none' width="0">
                </td>
            </tr>

            <tr style='height:18.0pt'>
                <td nowrap rowspan="3" style=
                'width:60.0pt;border-top:none;border-left:solid windowtext 1.0pt;border-bottom:solid black 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 1.4pt 0cm 1.4pt;height:18.0pt'
                width="80">
                    <p class="MsoNormal" style='text-align: center'><span lang=
                    "EN-US" style=
                    'font-family:"Arial",sans-serif'>cat</span></p>
                </td>

                <td nowrap rowspan="2" style=
                'width:83.0pt;border:none;border-right:solid windowtext 1.0pt;padding:0cm 1.4pt 0cm 1.4pt;height:18.0pt'
                width="111">
                    <p class="MsoNormal" style='text-align: center'><span lang=
                    "EN-US" style=
                    'font-family:"Arial",sans-serif'>cute</span></p>
                </td>

                <td nowrap rowspan="3" style=
                'width:60.0pt;border-top:none;border-left:none;border-bottom:solid black 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 1.4pt 0cm 1.4pt;height:18.0pt'
                width="80">
                    <p class="MsoNormal" style='text-align: center'><span lang=
                    "EN-US" style='font-family:"Arial",sans-serif'>1</span></p>
                </td>

                <td height="24" style='height:18.0pt;border:none' width="0">
                </td>
            </tr>

            <tr style='height:18.0pt'>
                <td height="24" style='height:18.0pt;border:none' width="0">
                </td>
            </tr>

            <tr style='height:15.75pt'>
                <td nowrap style=
                'width:83.0pt;border-top:none;border-left:none;border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 1.4pt 0cm 1.4pt;height:15.75pt'
                valign="bottom" width="111">
                    <p class="MsoNormal" style='text-align: center'><span lang=
                    "EN-US" style=
                    'font-family:"Arial",sans-serif'>animal</span></p>
                </td>

                <td height="21" style='height:15.75pt;border:none' width="0">
                </td>
            </tr>
        </table>

        <p class="MsoNormal"><span lang="EN-US">&nbsp;</span></p>

        <p class="MsoNormal"><span lang="EN-US">&nbsp;</span></p>

        <p class="MsoNormal"><span lang="EN-US">&nbsp;</span></p>

        <p class="MsoNormal"><span lang="EN-US">&nbsp;</span></p>

        <p class="MsoNormal"><span lang="EN-US">&nbsp;</span></p>

        <p class="MsoNormal"><span lang="EN-US">&nbsp;</span></p>
    </div>
</body>
</html>

回答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.

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


来源:https://stackoverflow.com/questions/30037004/copy-a-specific-format-table-from-outlook-to-email

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!