How to save the attachment of an outlook from VBA?

Bafio

New Member
Joined
Dec 12, 2016
Messages
4
Hello to everyone, once again I'm here looking for a solution to my problem.

I sistematically receive an e-mail with an excel attachment, and I have to open it and copy a range in the excel file, and paste in another file (already in my hard disk) in which I compare the values.

Is there someone who could help me in find a way to save/open (doesn't matter which one) the file through VBA?

Thanks for help.
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
Code:
Sub SaveDownAttachment()
    Dim myOlApp As Outlook.Application
    Dim myInspector As Outlook.Inspector
    Dim myItem As Outlook.MailItem
    Dim myAttachments As Outlook.Attachments

    Dim Folder As Outlook.MAPIFolder
    Dim sFolders As Outlook.MAPIFolder
    Dim iRow As Long, oRow As Integer
    Dim MailBoxName As String, Pst_Folder_Name  As String
    Dim myname As String
    Dim Email As String
    
   myname = Application.UserName
    Email = ' This should equal your outlook mail address
 
    
    MailBoxName = Email

    Pst_Folder_Name = "Inbox"
   
    For Each Folder In Outlook.Session.Folders(MailBoxName).Folders
        If VBA.UCase(Folder.Name) = VBA.UCase(Pst_Folder_Name) Then GoTo Label_Folder_Found
        For Each sFolders In Folder.Folders
            If VBA.UCase(sFolders.Name) = VBA.UCase(Pst_Folder_Name) Then
                Set Folder = sFolders
                GoTo Label_Folder_Found
            End If
        Next sFolders
    Next Folder
Label_Folder_Found:
     If Folder.Name = "" Then
        MsgBox "Invalid Data in Input"
        GoTo End_Lbl1:
    End If
    On Error Resume Next
    For iRow = Folder.Items.Count To 1 Step -1
   
        If Folder.Items.Item(iRow).Subject = "Subject" Then
        Set myOlApp = CreateObject("Outlook.Application")
        Set myItem = Folder.Items.Item(iRow)
        Set myAttachments = myItem.Attachments
        myAttachments.Item(1).SaveAsFile "" 'Path File
        Exit Sub
        End If
    Next iRow
exitsub:
    
    Set Folder = Nothing
    Set sFolders = Nothing
    
End_Lbl1:
End Sub

I use the above a lot. Note the changes that need to be made. This is for excel vba. It can also be done through outlook.
 
Upvote 0

Forum statistics

Threads
1,214,976
Messages
6,122,539
Members
449,088
Latest member
RandomExceller01

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top