Download attachments from specific folder in Outlook

耗尽温柔 提交于 2019-12-02 02:38:13

To open a folder on the same level as your Inbox, open Inbox, then go one level up to its parent, then retrieve your folder by name:

set MyFolder = Application.Session.GetDefaultFolder(olFolderInbox).Parent.Folders.Item("My Folder Name")

Code goes under ThisOutlookSession Update folder Name "Temp"

Private WithEvents Items As Outlook.Items

Private Sub Application_Startup()
    Dim olNameSpace As Outlook.NameSpace
    Dim olFolder  As Outlook.MAPIFolder

    Set olNameSpace = Application.GetNamespace("MAPI")
    Set olFolder = olNameSpace.GetDefaultFolder(olFolderInbox).Folders("TEMP")
    Set Items = olFolder.Items
End Sub

Private Sub Items_ItemAdd(ByVal Item As Object)
    If TypeOf Item Is Outlook.MailItem Then
        SaveAttachments Item
    End If
End Sub

'// http://www.slipstick.com/developer/save-attachments-to-the-hard-drive/
Public Sub SaveAttachments(Item As Outlook.MailItem)

    If Item.Attachments.Count > 0 Then

        Dim objAttachments As Outlook.Attachments
        Dim lngCount As Long
        Dim strFile As String
        Dim sFileType As String
        Dim i As Long

        Set objAttachments = Item.Attachments
            lngCount = objAttachments.Count
        For i = lngCount To 1 Step -1

            ' Get the file name.
            strFile = objAttachments.Item(i).FileName

            ' Get the path to your My Documents folder
            strFolderpath = CreateObject("WScript.Shell").SpecialFolders(16)
            strFolderpath = strFolderpath & "\Attachments\"

            ' Combine with the path to the folder.
            strFile = strFolderpath & strFile

            ' Save the attachment as a file.
            objAttachments.Item(i).SaveAsFile strFile

        Next i
    End If

End Sub
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!