# Off Topic > Hello..Introduce yourself >  >  Saving Outlook attachments

## ronik

Hi All,

I am really strugling since last 3 months to get into solution of my requirement. What my requirement is 

I want to save outlook attachments depending on subject line. The attachment which i receive goes to a sub folder of outlook. Sub folder name is "MOTIT". How do i do this? at present i m using folder picker option form code. I have copied code online into excel and running the same. can someone please help me out for this one. what i need is just to modify code in Folder picker so that it dosent ask which folder to select form outlook. here is my code which i got online.
i m facing an error in line: 'Set outFolder = outNs.Folders("MOTIT").Folders("Inbox") 'CHANGE FOLDER AS NEEDED


Public Sub Extract_Outlook_Email_Attachments()

    Dim OutlookOpened As Boolean
    Dim outApp As Outlook.Application
    Dim outNs As Outlook.Namespace
    Dim outFolder As Outlook.MAPIFolder
    Dim outAttachment As Outlook.Attachment
    Dim outItem As Object
    Dim outMailItem As Outlook.MailItem
    Dim inputDate As String
    Dim subjectFilter As String
    Dim saveInFolder As String

    saveInFolder = "Z:\Rohan\BDN files"                                 'CHANGE FOLDER PATH AS NEEDED
    If Right(saveInFolder, 2) <> "\" Then saveInFolder = saveInFolder & "\"

    'inputDate = Format(InputBox("Type the start date"), "dd/mm/yyyy")
    'If inputDate = "" Then Exit Sub

    subjectFilter = "BDR_LINE"
    '& Format(inputDate, "dd/mm/yyyy")

    'Get or create Outlook object and make sure it exists before continuing

    OutlookOpened = False
    On Error Resume Next
    Set outApp = GetObject(, "Outlook.Application")
    If Err.Number <> 0 Then
        Set outApp = New Outlook.Application
        OutlookOpened = True
    End If
    On Error GoTo 0

    If outApp Is Nothing Then
        MsgBox "Cannot start Outlook.", vbExclamation
        Exit Sub
    End If

    Set outNs = outApp.GetNamespace("MAPI")
    'Set outFolder = outNs.Folders("MOTIT").Folders("Inbox") 'CHANGE FOLDER AS NEEDED
    Set outFolder = outNs.PickFolder                                   'OR USER SELECTS FOLDER

    If Not outFolder Is Nothing Then
        For Each outItem In outFolder.Items
            If outItem.Class = Outlook.OlObjectClass.olMail Then
                Set outMailItem = outItem
                If outMailItem.Subject = subjectFilter Then
                    Debug.Print outMailItem.Subject
                    For Each outAttachment In outMailItem.Attachments
                        outAttachment.SaveAsFile saveInFolder & outAttachment.Filename
                    Next
                End If
            End If
        Next
    End If

    If OutlookOpened Then outApp.Quit

    Set outApp = Nothing
end sub

----------


## arlu1201

Hello ronik, 

Welcome to Excelforum. Be a part of large Excel community. Enjoy Learning.

----------

