vba自动下载outlook邮件附件
时间: 2023-05-16 18:05:23 浏览: 45
可以使用以下代码实现:
Sub DownloadAttachments()
Dim olApp As Outlook.Application
Dim olNs As Outlook.Namespace
Dim olFolder As Outlook.MAPIFolder
Dim olMail As Outlook.MailItem
Dim olAttachment As Outlook.Attachment
Dim strFolderPath As String
'Set the folder path where you want to save the attachments
strFolderPath = "C:\Attachments\"
'Create the Outlook application object
Set olApp = New Outlook.Application
'Get the MAPI namespace
Set olNs = olApp.GetNamespace("MAPI")
'Get the Inbox folder
Set olFolder = olNs.GetDefaultFolder(olFolderInbox)
'Loop through each mail item in the Inbox folder
For Each olMail In olFolder.Items
'Check if the mail item has any attachments
If olMail.Attachments.Count > 0 Then
'Loop through each attachment in the mail item
For Each olAttachment In olMail.Attachments
'Save the attachment to the specified folder
olAttachment.SaveAsFile strFolderPath & olAttachment.FileName
Next olAttachment
End If
Next olMail
'Clean up
Set olAttachment = Nothing
Set olMail = Nothing
Set olFolder = Nothing
Set olNs = Nothing
Set olApp = Nothing
MsgBox "Attachments downloaded successfully!", vbInformation
End Sub