【发布时间】:2020-06-30 07:55:58
【问题描述】:
在 Outlook 中使用 VBA 并且在定位时正在努力处理分级文件夹,因为它似乎只适用于一个分级的“子级别”。我目前在我的 Outlook 中可能有一个 5 层文件夹组织,每天我都会收到许多带有需要归档附件的电子邮件。 到目前为止,我正在使用我的第一个文件夹来提取附件并将它们归档到我创建的指定文件夹中,但由于子文件夹位于第 4 层,因此它不起作用。
Sub GetAttachments()
On Error GoTo GetAttachments_err
' Declare variables
Dim ns As NameSpace
Dim Inbox As MAPIFolder
Dim SubFolder As MAPIFolder
Dim Item As Object
Dim Atmt As Attachment
Dim FileName As String
Dim i As Integer
Set ns = GetNamespace("MAPI")
Set Inbox = ns.GetDefaultFolder(olFolderInbox)
Set SubFolder = Inbox.Folders("DZ1")
i = 0
' Check Inbox for messages and exit of none found
If SubFolder.Items.Count = 0 Then
MsgBox "There are no messages in the Sales Reports folder." _
, vbInformation, "Nothing Found"
Exit Sub
End If
' Check each message for attachments
If SubFolder.Items.Count > 0 Then
For Each Item In SubFolder.Items
' Save any attachments found
For Each Atmt In Item.Attachments
FileName = "File path" & Atmt.FileName
Atmt.SaveAsFile FileName
i = i + 1
Next Atmt
Next Item
End If
' Clear memory
GetAttachments_exit:
Set Atmt = Nothing
Set Item = Nothing
Set ns = Nothing
Exit Sub
' Handle errors
GetAttachments_err:
MsgBox "An unexpected error has occurred." _
& vbCrLf & "Please note and report the following information." _
& vbCrLf & "Macro Name: GetAttachments" _
& vbCrLf & "Error Number: " & Err.Number _
& vbCrLf & "Error Description: " & Err.Description _
, vbCritical, "Error!"
Resume GetAttachments_exit
End Sub
我能得到一些帮助吗?
干杯
【问题讨论】:
标签: vba outlook outlook-addin