点击工具-引用-Microsoft Outlook 16.0 Object Library进行outlook的引用
Sub DownloadAttachments()
Dim olApp As Outlook.Application
Dim olNamespace As Outlook.Namespace
Dim olFolder As Outlook.MAPIFolder
Dim olMail As Outlook.MailItem
Dim olAttachment As Outlook.Attachment
Dim saveFolder As String
saveFolder = ThisWorkbook.Path
Set olApp = New Outlook.Application
Set olNamespace = olApp.GetNamespace("MAPI")
Set olFolder = olNamespace.GetDefaultFolder(olFolderInbox)
i = 0
For Each olMail In olFolder.Items
If olMail.Attachments.Count > 0 Then
For Each olAttachment In olMail.Attachments
i = i + 1
saveFolder & "\" & i & "-" & olAttachment.Filename
& i) = olMail.SenderEmailAddress
& i) = olMail.Subject
_
Anchor:=Range("C" & i), _
Address:=saveFolder & "\" & i & "-" & olAttachment.Filename, _
TextToDisplay:=olAttachment.Filename
Next olAttachment
End If
Next olMail
End Sub
执行代码后,效果如下:
好了,本期教程就到这里啦,走过路过的点个关注分享一下吧,谢谢啦