1

我希望将发件人和邮件正文粘贴到我的 Excel 文件中。

我已经达到了粘贴发件人的程度,但是当粘贴作为表格的正文时,它会破坏格式,因为它粘贴在一个单元格中。

如何将表格粘贴到发件人旁边的电子邮件中?

我认为粘贴表格的代码应该从注释开始:'在此处将表格粘贴到电子邮件正文中。

Private Sub CommandButton1_Click()

    Dim OutlookApp As Outlook.Application
    Dim OutlookNamespace As Namespace
    Dim Folder As MAPIFolder
    Dim OutlookMail As Variant
    Dim i As Integer

    Set OutlookApp = New Outlook.Application
    Set OutlookNamespace = OutlookApp.GetNamespace("MAPI")
    Set Folder = OutlookNamespace.GetDefaultFolder(olFolderInbox).Folders("ABV")

    i = 2

    With ThisWorkbook.Sheets("Sheet2")
        For Each OutlookMail In Folder.Items
            If OutlookMail.ReceivedTime >= .Range("C1") Then
                With .Cells(i, 1)
                    .Value = OutlookMail.SenderName
                    .Columns.AutoFit
                    .VerticalAlignment = xlTop
                End With
                With .Cells(i, 2)
                    'Paste the table inside the email body here
                End With
                i = i + 1
            End If
        Next OutlookMail
    End With

    Set Folder = Nothing
    Set OutlookNamespace = Nothing
    Set OutlookApp = Nothing

End Sub

此文件夹中的所有电子邮件都将包含一个表格,因此我想在每个发件人及其表格被粘贴时循环。

4

1 回答 1

0

您可以通过以下方式寻址和复制电子邮件正文中的表格:

OutlookMail.GetInspector.WordEditor.Range.Tables(1).Range.Copy

然后将其粘贴为例如值到您的Excel.Range

With ThisWorkbook.Sheets("Sheet2")
    .Range("A1").PasteSpecial xlPasteValues 

如果您想以不同的剪贴板支持的格式粘贴,请尝试使用 Excel 的宏记录器获得理想的格式。结果是这样的:

With ThisWorkbook.Sheets("Sheet2")
    .Range("A1").Select
    .PasteSpecial Format:="Text"   ' or "HTML" or ...
于 2019-05-29T13:50:40.010 回答