Results 1 to 6 of 6

Thread: [RESOLVED] Saving attachments from emails

  1. #1

    Thread Starter
    Member
    Join Date
    Sep 2006
    Posts
    38

    Resolved [RESOLVED] Saving attachments from emails

    Hi all,

    I need to save attachments that arrive from a particular email address.
    I have made a rule that will move the email to a folder and now i need a macro that will save the attachment in the mail to a folder on the hard disk.

    I have found this code but i'm getting an error message when i'm runing it.


    Code:
    Sub SaveAttachmentsToFolder()
    
    On Error GoTo SaveAttachmentsToFolder_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
        Dim varResponse As VbMsgBoxResult
        Set ns = GetNamespace("MAPI")
        Set Inbox = ns.GetDefaultFolder(olFolderInbox)
        Set SubFolder = Inbox.Folders("test") ' Enter correct subfolder name.
        i = 0
    ' Check subfolder 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
        For Each Item In SubFolder.Items
            For Each Atmt In Item.Attachments
    ' Check filename of each attachment and save if it has "xls" extension
                If Right(Atmt.FileName, 3) = "txt" Then
                ' This path must exist! Change folder name as necessary.
                    FileName = "C:\test\" & _
                        Format(Item.CreationTime, "yyyymmdd_hhnnss_") & Atmt.FileName
                    Atmt.SaveAsFile FileName
                    i = i + 1
                End If
            Next Atmt
        Next Item
    ' Show summary message
        If i > 0 Then
            varResponse = MsgBox("I found " & i & " attached files." _
            & vbCrLf & "I have saved them into the C:\test." _
            & vbCrLf & vbCrLf & "Would you like to view the files now?" _
            , vbQuestion + vbYesNo, "Finished!")
    ' Open Windows Explorer to display saved files if user chooses
            If varResponse = vbYes Then
                Shell "Explorer.exe /e,C:\test", vbNormalFocus
            End If
        Else
            MsgBox "I didn't find any attached files in your mail.", vbInformation, "Finished!"
        End If
    ' Clear memory
    SaveAttachmentsToFolder_exit:
        Set Atmt = Nothing
        Set Item = Nothing
        Set ns = Nothing
        Exit Sub
    ' Handle Errors
    SaveAttachmentsToFolder_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 SaveAttachmentsToFolder_exit
    
    
    End Sub

    i recieve this error message:



    I'm using outlook 2003

    Any help will be appreciated.

  2. #2
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Saving attachments from emails

    comment this line

    'On Error GoTo SaveAttachmentsToFolder_err

    now tell me where exactly are you getting the error?
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  3. #3

    Thread Starter
    Member
    Join Date
    Sep 2006
    Posts
    38

    Re: Saving attachments from emails

    I recieve this error once i try to run the macro (F5).

  4. #4
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Saving attachments from emails

    yes, like i said comment that line and then run the macro and then tell me where exactly are you getting the error?
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  5. #5

    Thread Starter
    Member
    Join Date
    Sep 2006
    Posts
    38

    Re: Saving attachments from emails

    :-)

    Sorry for misunderstand.

    I'll do it tomorrow since i have already left work.

    Thanks for now.

  6. #6

    Thread Starter
    Member
    Join Date
    Sep 2006
    Posts
    38

    Re: Saving attachments from emails

    Ok,

    I have comment the line and it runs perfectly :-)

    But now, i need it to be automatically and not only when i run the macro. Any idea how ti accomplish that?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width