2

I am not familiar with vba enough to modify this for my needs.

I need to download the attachments from a specific folder.

I found this example, but I am not sure how to get the folder where these emails are sent to.

I have a rule that when these emails come in, it places them into a different folder.

This where I want to run the macro so it only strips the attachments from these emails and places them on the local computer folder.

What parts do I need to change to get this to work for my needs?

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
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
OneFineDay
  • 9,004
  • 3
  • 26
  • 37
  • What do you know about the folder? Its entry id? Folder path? – Dmitry Streblechenko Mar 14 '16 at 17:59
  • I'm just not sure how to write the folder path in vba. It just another folder in the email next to the inbox. – OneFineDay Mar 14 '16 at 18:07
  • Link-only usually can become invalid if the linked page changes - I have add context to your question so your fellow users will have some idea what your asking, also with @DmitryStreblechenko great answer, I have also add second answer. :-) – 0m3r Mar 14 '16 at 22:22

2 Answers2

3

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")
Dmitry Streblechenko
  • 62,942
  • 4
  • 53
  • 78
2

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
0m3r
  • 12,286
  • 15
  • 35
  • 71