Save An Attachment To A Folder

  • I found the code to use for saving an attachment in an email to an external folder. (could not post to that threat - http://www.ozgrid.com/forum/showthread.php?t=59353 - so had to create a new one) However I am getting an error. The line of code is:


    Code
    Set myFolder = myNameSpace.GetDefaultFolder(olFolderInbox)
        Set myFolder = myFolder.Folders("Daily Reports")


    The second line is the one with the error. It is a runtime error indicating an object can not be found. The folder is actually a sub folder of a sub folder or a Folder called My Folders. The "My Folders" folder is not a sub folder of the main inbox but a sibling (for lack of better words)


    I tried to change the first line to:

    Code
    Set myFolder = myNameSpace.GetDefaultFolder(olFolderMyFolders)


    but then received the error on that line. If it helps, here is the set up of my outlook if it helps:


    Mailbox
    -Inbox
    -My Folders
    -- Inbound Items
    --- Reports
    ---- Daily Reports


    The email with the attachments are in the "Daily Reports" sub folder


    Any Help wouold be appreciated.

  • Re: Save An Attachment To A Folder


    You need to reference each of the folders as you navigate across and then down.


    [vba]
    Set myFolder = myFolder.Parent.Folders("My Folders")
    Set myFolder = myFolder.Folders("Inbound Items")
    Set myFolder = myFolder.Folders("Reports")
    Set myFolder = myFolder.Folders("Daily Reports")
    [/vba]

    [h4]Cheers
    Andy
    [/h4]

  • Re: Save An Attachment To A Folder


    Thanks ANdy, that worked.


    Now I have one other issue I need assistance with.


    Ihave the following line:

    Code
    myAttachment.SaveAsFile "H:\Kana_Reports\Daily\Daily_Received\" & myAttachment.FileName & _Format(Now(), "mm_dd_yyyy")


    while this does save the attachment and does add the date to the end, it is adding it after the file type. i.e


    file1.xls10_05_2007 or file2.csv10_05_2007


    How do I make it add it prior to the file type (and add an "_" )? i.e


    file1_10_05_2007.xls or file2_10_05_2007_.csv



    One other question: At present I have it saving the files in a predefinded folder. How would I have it crete a folder based on the date and then save the files to that folder? I.e if today is 10/5/2007 it creates the folder 100507 and then saves the files into that folder.

  • Re: Save An Attachment To A Folder


    you need to remove the file extension from the value returned by myAttachment.FileName


    use either the function Replace, if you know the exact extension,
    or Instr and Left if not.

    [h4]Cheers
    Andy
    [/h4]

  • Re: Save An Attachment To A Folder


    It may be easier to add the date up front.....


    One other question: At present I have it saving the files in a predefinded folder. How would I have it crete a folder based on the date and then save the files to that folder? I.e if today is 10/5/2007 it creates the folder 100507 and then saves the files into that folder.

  • Re: Save An Attachment To A Folder


    OK, I found the code to rete the folder and have it working. Now I just need to know how to have my code automatically run all the time (be "listening" ) so that when the email is received and moved into the specified folder, the attachment will automatically get saved.

  • Re: Save An Attachment To A Folder


    This works in Outlook 2000:



    That code goes in the ThisOutlookSession module. Whenever an email is received from Mavyak, the code specified will run.

  • Re: Save An Attachment To A Folder


    Quote from Mavyak

    This works in Outlook 2000:



    That code goes in the ThisOutlookSession module. Whenever an email is received from Mavyak, the code specified will run.


    Looks like some of the variables are the same as mine. Here is my code, how do I modify it to run continusly:


  • Re: Save An Attachment To A Folder



    This code goes in the ThisOutlookSession module. It should monitor your Daily Reports folder. Whenever new mail is delivered there and the sender's name contains "MAVYAK", it will save any and all attachments to the appropriate (and dynamically created if necessary) folder. Note that I stripped off the trailing slash on your folder name so that the Dir() function would work. I added the slash to the line that saves the attachments. Change the line for your path to suit as well as the testing set-up (e.g. change testing for sender name of "MAVYAK" to what ever criteria will drive the saving of the attachements).


    The "WithEvents" keyword is the driving force behind this process. This is an event procedure that happens when an event, in this case "ItemAdd", happens to a specified object, in this case the "Daily Reports" folder. When an item/email gets added to the folder, the code runs, saving the attachments.

  • Re: Save An Attachment To A Folder


    [QUOTE=Mavyak]


    This code goes in the ThisOutlookSession module....QUOTE]


    OK I put the code in the This OutlookSesson module. It initiall errored on

    Code
    Dim objNS As NameSpace
        Set objNS = Application.GetNamespace("MAPI")


    I changed to

    Code
    Dim myNameSpace As Outlook.NameSpace
        Dim myFolder As Outlook.MAPIFolder


    No loger get the error. However, nothing happens (Yes I did close and restart outlook)....

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!