将附件从 Outlook 导出到 Excel 单元格

Export Attachment from outlook to excel cells

提问人:sephiroth 提问时间:4/2/2023 更新时间:4/2/2023 访问量:129

问:

我有一个问题,将附件从Outlook导出到Excel单元格。附件不是文件名,而是文件本身。例如,如果是 PDF 文件,它会将 PDF 文件提取到单元格中,而不是 PDF 中的文件名或详细信息。我知道如何将附件保存到文件夹中,但不知道在单元格上。代码如下:

Sub GetOutlookDetails()

Dim olApp As Outlook.Application
Dim olNS As Outlook.Namespace
Dim olFldr As Outlook.MAPIFolder
Dim olItem As Object
Dim olMailItem As Outlook.MailItem
Dim ws As Worksheet
Dim iRow As Long
Set ws = ThisWorkbook.Worksheets("Sheet1")
Dim lastrow As Long

Set olApp = New Outlook.Application
Set olNS = olApp.GetNamespace("MAPI")

'Set Location Mailbox
Set olFldr = olNS.Folders("Cash Allocations UKI")
Set olFldr = olFldr.Folders("Inbox")
Set olFldr = olFldr.Folders("GB - United Kingdom")

iRow = 5

Application.ScreenUpdating = False

'Find Unread email only in Mailbox
For Each olItem In olFldr.Items

If olItem.UnRead = True Then
    If olItem.Class = olMail Then
    Set olMailItem = olItem
        With olMailItem
            ws.Cells(iRow, "A") = .SenderEmailAddress
            ws.Cells(iRow, "B") = .Subject
            ws.Cells(iRow, "C") = .Body
            iRow = iRow + 1
        End With
    
    End If
    End If

Next olItem
Application.ScreenUpdating = False

'Remove Wrap Text
Columns("C:C").Select
With Selection
    .WrapText = True
    .Orientation = 0
    .AddIndent = False
    .IndentLevel = 0
    .ShrinkToFit = False
    .ReadingOrder = xlContext
    .MergeCells = False
End With
With Selection
    .WrapText = False
    .Orientation = 0
    .AddIndent = False
    .IndentLevel = 0
    .ShrinkToFit = False
    .ReadingOrder = xlContext
    .MergeCells = False
End With
Range("A5").Select

'To put "."
lastrow = ThisWorkbook.Worksheets("Sheet1").Range("C" & Rows.Count).End(xlUp).Row
Range("D5:D" & lastrow) = "."
End Sub

这个想法是将每封电子邮件中收到的附件嵌入到 E 列

ws.Cells(iRow, "E") = .Attachments 'Stuck here
Excel VBA 电子邮件 Outlook 附件

评论


答:

2赞 Dmitry Streblechenko 4/2/2023 #1

您需要将附件另存为文件(其中 object 来自集合),然后使用 将其作为对象插入。有关详细信息,请参阅 https://www.howtoexcel.org/embed-pdf/Attachment.SaveAsFileAttachmentMailItem.AttachmentsWorksheet.OLEObjects.Add

评论

0赞 sephiroth 4/2/2023
您能否分享有关如何执行此操作的代码?我一直在寻找,但找不到任何可以让它工作的东西
0赞 Dmitry Streblechenko 4/2/2023
查看我提供的链接中的源代码