• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Attachments to a Folder Save

Hello James.

Done recently this exercise at work.
Working fine for me, let me know any changes required!

Code:
Sub Save_attatchments()

'Step 1:  Declare your variables
    Dim ns As Namespace
    Dim MyInbox As MAPIFolder
    Dim MItem As MailItem
    Dim Atmt As Attachment
    Dim FileName As String
'Step 2:  Set a reference to your inbox
    Set ns = GetNamespace("MAPI")
    Set MyInbox = ns.GetDefaultFolder(olFolderInbox)
   
   
'Step 3:  Check for messages in your inbox; exit if none
    If MyInbox.Items.Count = 0 Then
    MsgBox "No messages in folder."
    Exit Sub
    End If


'Step 4:  Create directory to hold attachments
    On Error Resume Next
    MkDir "C:\Moty\"
     

'Step 5:  Start to loop through each mail item
    For Each MItem In MyInbox.Items
   

'Step 6:  Save each attachement then go to the next attachment
    For Each Atmt In MItem.Attachments
    FileName = "C:\Moty\" & Atmt.FileName
    Atmt.SaveAsFile FileName
    Next Atmt
   
'Step 7:  Move to the next mail item
    Next MItem
   
'Step 8:  Memory cleanup
    Set ns = Nothing
    Set MyInbox = Nothing
   
   
End Sub
 
Will try at work.

Can me do me a favor..if i wanted to save certain attachments only as we receive 100+ attachment with the same subject.

So wanted to have two macro's.

Hope this make sense else please advise.
 
Code:
Sub Save_Particular_attatchment()

'Step 1:  Declare your variables
    Dim ns As Namespace
    Dim MyInbox As MAPIFolder
    Dim MItem As Object
    Dim Atmt As Attachment
    Dim FileName As String
    Dim i As Integer
   
'Step 2:  Set a reference to your inbox
    Set ns = GetNamespace("MAPI")
    Set MyInbox = ns.GetDefaultFolder(olFolderInbox)
   
'Step 3:  Check for messages in your inbox; exit if none
    If MyInbox.Items.Count = 0 Then
    MsgBox "No messages in folder."
    Exit Sub
    End If

'Step 4:  Create directory to hold attachments
    On Error Resume Next
    MkDir "C:\Monty\"
   
'Step 5:  Start to loop through each mail item
    For Each MItem In MyInbox.Items
   
'Step 6:  Check for the words Data Submission in Subject line
    If InStr(1, MItem.Subject, "Monty") < 1 Then  'Change "Monty" to your subject which you are looking for
    GoTo SkipIt
    End If

'Step 7:  Save each with a log number; go to the next attachment
    i = 0
    For Each Atmt In MItem.Attachments
    FileName = _
    "C:\Monty\Attachment-" & i & "-" & Atmt.FileName
    Atmt.SaveAsFile FileName
    i = i + 1
    Next Atmt
   
'Step 8:  Move to the next mail item
SkipIt:
    Next MItem
     
'Step 9:  Memory cleanup
    Set ns = Nothing
    Set MyInbox = Nothing
   
   
End Sub

Try this..
 
Thank you Champ. Can you adjust the code enabling saving attachments only from selected e-mails? That will be more helpful.
 
Back
Top