VBA应用—批量下载邮件附件

使用方法

outlook2019 + win10

启用宏

outlook > 文件 > 选项 > 自定义功能区 > 勾选 开发工具
在这里插入图片描述

outlook > 文件 > 选项 > 信任中心设置 > 宏设置 > 启用所有宏
在这里插入图片描述

宏编写

outlook > 开发工具 > Visual Basic > 双击 ThisOutlookSession
在这里插入图片描述

插入 > 过程
在这里插入图片描述

然后粘贴下文的代码段,F5运行或者Ctrl+S保存。注意,path必须要在你的电脑中存在,我默认设置的是E盘。path = “E:\宏下载”

在这里插入图片描述

邮件中点击下载

打开outlook > 开发工具 > 宏 > 批量下载附件(或者其它你自定的名称),这样就会将所选中的所有邮件的附件下载到path中了。

在这里插入图片描述
在这里插入图片描述

代码段如下:

Public Sub 批量下载附件()
Dim msg As MailItem
Dim exp As Explorer
Dim att As Attachment
Dim mailIndex As Integer
Dim path As String
Dim folder As String
Set exp = Application.ActiveExplorer
'保存附件到哪个路径,可以自己修改,末尾必须是斜杠
path = "E:\宏下载\"
mailIndex = 0
For Each msg In exp.Selection
If msg.Attachments.Count > 0 Then
mailIndex = mailIndex + 1
For Each att In msg.Attachments
'所有附件保存到path中,文件命名为:mailatt<编号>_附件原始文件名
att.SaveAsFile path + CStr(mailIndex) + "_" + att.FileName
Next
End If
Next
End Sub

版权声明:

本文为CSDN博主「游无方」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_40021197/article/details/109163091

欢迎留言探讨!

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
### 回答1: 可以使用 VBA 编写代码来自动下载 Outlook 邮件附件。以下是一个示例代码: Sub DownloadAttachments() Dim objOL As Outlook.Application Dim objNS As Outlook.NameSpace Dim objFolder As Outlook.MAPIFolder Dim objItem As Object Dim objAttachment As Outlook.Attachment Dim strFolderPath As String Dim strFileName As String ' Set the folder path where you want to save the attachments strFolderPath = "C:\Attachments\" ' Create the Outlook application object Set objOL = CreateObject("Outlook.Application") ' Get the MAPI namespace Set objNS = objOL.GetNamespace("MAPI") ' Get the Inbox folder Set objFolder = objNS.GetDefaultFolder(olFolderInbox) ' Loop through each item in the Inbox folder For Each objItem In objFolder.Items ' Check if the item is a mail item If TypeOf objItem Is MailItem Then ' Loop through each attachment in the mail item For Each objAttachment In objItem.Attachments ' Save the attachment to the specified folder strFileName = strFolderPath & objAttachment.FileName objAttachment.SaveAsFile strFileName Next objAttachment End If Next objItem ' Clean up Set objAttachment = Nothing Set objItem = Nothing Set objFolder = Nothing Set objNS = Nothing Set objOL = Nothing MsgBox "Attachments downloaded successfully!", vbInformation End Sub 请注意,此代码仅适用于 Outlook 客户端,而不适用于 Outlook Web App。 ### 回答2: VBA是Visual Basic for Applications的缩写,是一种用于编写宏的编程语言,可扩展Microsoft Office应用程序的功能。下面是如何使用VBA自动下载Outlook邮件附件的步骤: 1. 打开Outlook应用程序,并进入“开发者”选项卡。如果未看到“开发者”选项卡,请在“文件”选项卡上选择“选项”,然后在“自定义功能区”中启用“开发者”选项卡。 2. 单击“Visual Basic”按钮,打开Visual Basic编辑器。 3. 在Visual Basic编辑器中,创建一个新的模块。右键点击项目名字,选择“插入”,再选择“模块”。 4. 在新模块中,编写以下代码来自动下载Outlook邮件附件: ```VBA Sub DownloadAttachments() Dim outlookApp As Outlook.Application Dim outlookNamespace As Namespace Dim outlookFolder As MAPIFolder Dim outlookItem As MailItem Dim outlookAttachment As Attachment Dim saveFolder As String ' 设置附件保存路径 saveFolder = "C:\Attachments\" ' 初始化Outlook应用程序和名称空间 Set outlookApp = New Outlook.Application Set outlookNamespace = outlookApp.GetNamespace("MAPI") ' 设置欲遍历的文件夹(可以是收件箱、发件箱等) Set outlookFolder = outlookNamespace.GetDefaultFolder(olFolderInbox) ' 遍历文件夹中每个邮件 For Each outlookItem In outlookFolder.Items ' 检查邮件是否有附件 If outlookItem.Attachments.Count > 0 Then ' 遍历每个附件 For Each outlookAttachment In outlookItem.Attachments ' 保存附件到指定路径 outlookAttachment.SaveAsFile saveFolder & outlookAttachment.DisplayName Next outlookAttachment End If Next outlookItem ' 释放对象 Set outlookApp = Nothing Set outlookNamespace = Nothing Set outlookFolder = Nothing Set outlookItem = Nothing Set outlookAttachment = Nothing End Sub ``` 5. 在代码中,将`saveFolder`变量的值替换为你想要保存附件的文件夹路径。 6. 单击运行按钮或按下`F5`键来运行代码。 7. 运行完代码后,Outlook邮件中的附件将会自动下载到指定的文件夹路径下。 以上就是使用VBA自动下载Outlook邮件附件的步骤和相关代码。请注意,该代码将会下载指定文件夹中所有邮件的附件,如果只需要下载特定邮件或特定文件夹中的附件,需要进行进一步的代码修改。 ### 回答3: VBA是Visual Basic for Applications的简称,是一种用于自动化办公任务的编程语言。要实现VBA自动下载Outlook邮件附件,可以按照以下步骤进行操作: 1. 打开Outlook应用程序并进入“开发人员”选项卡。如果未显示该选项卡,可以打开Outlook设置,并启用开发人员模式。 2. 在“开发人员”选项卡中,点击“Visual Basic”按钮,打开VBA编辑器。 3. 在VBA编辑器中,选择“工具”菜单,然后选择“引用”。 4. 在“引用”对话框中,勾选“Microsoft Outlook Object Library”选项,并点击“确定”按钮。 5. 在VBA编辑器的模块窗口中,插入一个新的模块。 6. 在新的模块中,编写VBA代码来下载Outlook邮件附件。以下是一个基本的示例代码: ``` Sub DownloadAttachments() Dim OutlookApp As Outlook.Application Dim OutlookNamespace As Namespace Dim Inbox As MAPIFolder Dim Item As Object Dim Attachment As Attachment Dim SaveFolderPath As String ' 设置保存附件的文件夹路径 SaveFolderPath = "C:\Attachments" ' 创建Outlook应用程序并登录邮箱账号 Set OutlookApp = New Outlook.Application Set OutlookNamespace = OutlookApp.GetNamespace("MAPI") Set Inbox = OutlookNamespace.GetDefaultFolder(olFolderInbox) ' 遍历收件箱中的所有邮件 For Each Item In Inbox.Items ' 判断是否有附件 If Item.Attachments.Count > 0 Then ' 遍历邮件中的所有附件 For Each Attachment In Item.Attachments ' 保存每个附件到指定的文件夹 Attachment.SaveAsFile SaveFolderPath & "\" & Attachment.Filename Next Attachment End If Next Item ' 释放内存 Set Inbox = Nothing Set OutlookNamespace = Nothing Set OutlookApp = Nothing MsgBox "附件已成功下载到指定文件夹。" End Sub ``` 7. 在代码中,可以根据实际需要修改保存附件的文件夹路径。 8. 运行该VBA代码,即可自动下载Outlook邮件中的附件到指定的文件夹中。 以上是一个简单的示例代码,你可以根据实际需求进行修改和扩展。希望对你有所帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值