none
Outlook, Get Attachments for specific email account. RRS feed

  • Question

  • I have this code that transfer attachments from my email and saves them to a folder.  It works! My problem is I have multiple email accounts in my Outlook, and I need a way to pic the specific accout to pull from.

    Sub GetAttachments()

    On Error GoTo AttError

    Dim ns As NameSpace

    Dim Inbox As MAPIFolder

    Dim Item As Object

    Dim Itmt As Attachment

    Dim FileName As String

    Dim i As Integer

    Set ns = GetNamespace("MAPI")

    Set Inbox = ns.GetDefaultFolder(olFolderInbox)

    i = 0

    '************************************* check for messages in Inbox************

    If Inbox.Items.Count = 0 Then

        MsgBox "there are no messages in the inbox.", vbInformation, _

                "Nothing Found"

        Exit Sub

        End If

    '*************************************Examine for Attachments*****************

    For Each Item In Inbox.Items

        For Each Atmt In Item.Attachments

        If Right(Atmt.FileName, 3) = "pdf" Then

            FileName = "C:\Email Attachments\" & Atmt.FileName

            Atmt.SaveAsFile FileName

            i = i + 1

        End If

        Next Atmt

    Next Item

    '*************************************Notifacations***************************

    If i > 0 Then

        MsgBox "I found and saved " & i & " attached files."

        Else: MsgBox "I didn't any attached files in your Inbox."

        End If

    '*************************************Clear Memory****************************

    AttExit:

        Set Atmt = Nothing

        Set Item = Nothing

        Set ns = Nothing

        Exit Sub

    '**************************************Error Handler**************************

    AttError:

        MsgBox "There was an error saving your attachments."

        Resume AttExit

       

    End Sub

    Thursday, July 5, 2012 7:34 AM

All replies

  • do you want some UI to choose account by user when macro runs or something other?
    Thursday, July 5, 2012 7:51 AM
  • No, I don't want a UI. I have an email designated for the files I pull.  I want the code to automatically pull from that email. let's call it zzz@somewhere.com

    Thursday, July 5, 2012 2:10 PM
  • change line

    Set Inbox = ns.GetDefaultFolder(olFolderInbox)

    to

    Set Inbox = ns.Folders(that email address).Store.GetDefaultFolder(olFolderInbox)

    Thursday, July 5, 2012 3:02 PM
  • Do all accounts deliver to the same folder (Inbox)? Or do you have separate folders/stores for each account?


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.3 is now available!

    Thursday, July 5, 2012 7:21 PM
  • I have separate folders.  It's an "office" folder open to the employees.
    Thursday, July 5, 2012 11:18 PM
  • Do you mean a delegate mailbox (not just a folder in the primary mailbox) shared among a few people?

    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.3 is now available!

    Friday, July 6, 2012 2:19 AM
  • It's a mailbox, not a folder.
    Sunday, July 8, 2012 10:32 AM
  • If I am understanding this correctly, you have a shared mailbox that is showing up in everyone's folder list in Outlook. In other words, if you click the Folder List icon on the navigation pane to see all folders, there is a top-level folder for that other mailbox.

    If that's the case, then Damian's suggestion above is accurate. You can reference the folder by name off the Namespace/Session object. The following KB article summarizes the various ways to reference folders:

    290804 Programming examples for referencing items and folders in Outlook 2002
    http://support.microsoft.com/kb/290804/EN-US

    The same concepts apply to later versions of Outlook.


    Bill Jacob - Microsoft Customer Service & Support - Developer Messaging

    Thursday, July 12, 2012 7:37 PM
    Moderator