VBA自动下载邮件附件脚本
时间: 2023-11-15 18:12:21 浏览: 161
以下是一个VBA脚本示例,用于自动下载Outlook邮件的附件:
```
Sub DownloadAttachments()
Dim objOL As Outlook.Application
Dim objMsg As Outlook.MailItem
Dim objAttachments As Outlook.Attachments
Dim objAttachment As Outlook.Attachment
Dim objFSO As Object
Dim strFolderpath As String
Dim strFilePath As String
Dim i As Integer
'Select the folder where the emails are located
Set objOL = CreateObject("Outlook.Application")
Set objMsg = objOL.ActiveExplorer.Selection.Item(1)
Set objAttachments = objMsg.Attachments
'Set the folder where you want to save the attachments
strFolderpath = "C:\Email Attachments\"
'Create the folder if it doesn't exist
Set objFSO = CreateObject("Scripting.FileSystemObject")
If Not objFSO.FolderExists(strFolderpath) Then
objFSO.CreateFolder (strFolderpath)
End If
'Loop through each attachment and save it to the folder
For i = 1 To objAttachments.Count
strFilePath = strFolderpath & objAttachments.Item(i).FileName
objAttachments.Item(i).SaveAsFile strFilePath
Next i
'Release objects from memory
Set objAttachment = Nothing
Set objAttachments = Nothing
Set objMsg = Nothing
Set objOL = Nothing
End Sub
```
使用此脚本,您需要打开Outlook并选择包含要下载附件的电子邮件。然后运行脚本,它将循环遍历每个邮件的附件并将其保存到您指定的文件夹中。请确保更改“strFolderpath”变量以指向您想要保存附件的文件夹路径。
阅读全文