Results 1 to 12 of 12
  1. #1
    4 Star Lounger
    Join Date
    Feb 2006
    Posts
    504
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Auto Save Attachment (2003)

    I find the following codes to auto save the email attachment to local disk, but for some reason, the codes doesn't work. Please Help.

    ' How to use:
    ' From Outlook, open the VBEditor (Alt+F11)
    ' Add a reference to the "Microsoft Excel <your version number> Object Library fron Tools>References
    ' Paste the code into the ThisOutlookSession module
    ' Create an Outlook folder named "Temp" in your Personal folders (or amend the code: Set TargetFolderItems to eqaul an existing folder)
    ' Create a directory "C:Temp" (or amend the constant: FILE_PATH to eqaul an existing folder)
    ' Save the project
    ' Restart Outlook (or run the routine "Application_Startup")

    ' Testing the vba script
    ' Move a mail item with some attachments into you target folder.
    ' The attachments will be saved in your specified directory
    ' Any Excel files will be printed




    '################################################# ##############################
    '### Module level Declarations
    'expose the items in the target folder to events
    Option Explicit
    Dim WithEvents TargetFolderItems As Items
    'set the string constant for the path to save attachments
    Const FILE_PATH As String = "C:Temp"

    '################################################# ##############################
    '### this is the Application_Startup event code in the ThisOutlookSession module
    Private Sub Application_Startup()
    'some startup code to set our "event-sensitive" items collection
    Dim ns As Outlook.NameSpace
    '
    Set ns = Application.GetNamespace("MAPI")
    Set TargetFolderItems = ns.Folders.Item( _
    "Personal Folders").Folders.Item("Temp").Items

    End Sub

    '################################################# ##############################
    '### this is the ItemAdd event code
    Sub TargetFolderItems_ItemAdd(ByVal Item As Object)
    'when a new item is added to our "watched folder" we can process it
    Dim olAtt As Attachment
    Dim i As Integer

    If Item.Attachments.Count > 0 Then
    For i = 1 To Item.Attachments.Count
    Set olAtt = Item.Attachments(i)
    'save the attachment
    olAtt.SaveAsFile FILE_PATH & olAtt.FileName
    End If
    Next
    End If

    Set olAtt = Nothing

    End Sub

    '################################################# ##############################
    '### this is the Application_Quit event code in the ThisOutlookSession module
    Private Sub Application_Quit()

    Dim ns As Outlook.NameSpace
    Set TargetFolderItems = Nothing
    Set ns = Nothing

    End Sub

    THanks

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Auto Save Attachment (2003)

    Where did you put the code?

  3. #3
    4 Star Lounger
    Join Date
    Feb 2006
    Posts
    504
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Auto Save Attachment (2003)

    Paste the code into the ThisOutlookSession module

  4. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Auto Save Attachment (2003)

    Have you closed all instances of Outlook, then started Outlook again after pasting the code there?

  5. #5
    4 Star Lounger
    Join Date
    Feb 2006
    Posts
    504
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Auto Save Attachment (2003)

    Yes, I did. I followed the steps below:

    From Outlook, open the VBEditor (Alt+F11)
    ' Add a reference to the "Microsoft Excel <your version number> Object Library fron Tools>References
    ' Paste the code into the ThisOutlookSession module
    ' Create an Outlook folder named "Temp" in your Personal folders (or amend the code: Set TargetFolderItems to eqaul an existing folder)
    ' Create a directory "C:Temp" (or amend the constant: FILE_PATH to eqaul an existing folder)
    ' Save the project
    ' Restart Outlook (or run the routine "Application_Startup")

  6. #6
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Auto Save Attachment (2003)

    There is one error in the code: in the part

    If Item.Attachments.Count > 0 Then
    For i = 1 To Item.Attachments.Count
    Set olAtt = Item.Attachments(i)
    'save the attachment
    olAtt.SaveAsFile FILE_PATH & olAtt.FileName
    End If
    Next
    End If

    the first End If is superfluous, you should remove it:

    If Item.Attachments.Count > 0 Then
    For i = 1 To Item.Attachments.Count
    Set olAtt = Item.Attachments(i)
    'save the attachment
    olAtt.SaveAsFile FILE_PATH & olAtt.FileName
    Next
    End If

    With that change, the code works correctly when I try it.

  7. #7
    4 Star Lounger
    Join Date
    Feb 2006
    Posts
    504
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Auto Save Attachment (2003)

    Hi Hans,

    I remove the first End If and still doesn't work for me. I have a Test personal folder create and move one email have excel attachement to the Test folder. But the excel attachment is not in C:Temp folder.

    Please advise.

    Thanks

  8. #8
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Auto Save Attachment (2003)

    You could test which of your routines is running or not running by placing message boxes in them or writing to the Immediate Window. For example:

    <code>Private Sub Application_Startup()
    'some startup code to set our "event-sensitive" items collection
    MsgBox "Sub Application_Startup() running" 'or
    Debug.print "Sub Application_Startup() running"

    Dim ns As Outlook.NameSpace
    Set ns = Application.GetNamespace("MAPI")
    Set TargetFolderItems = ns.Folders.Item( _
    "Personal Folders").Folders.Item("Temp").Items
    End Sub </code>


    (Note: I wrote that off the top of my head; some debugging might be required)

    Also, do you get a macro warning when you start Outlook? If not, your macro security level maybe be too high or too low. I suggest Medium.

  9. #9
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Auto Save Attachment (2003)

    Try the following:
    - Open the ThisOutlookSession module.
    - Select File | Export File...
    - Save ThisOutlookSession.cls in a convenient location, for example on your desktop.
    - Create a zip file from ThisOutlookSession.cls.
    - Attach the zip file to a reply.
    That would enable Loungers to look at your ThisOutlookSession module.

  10. #10
    4 Star Lounger
    Join Date
    Feb 2006
    Posts
    504
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Auto Save Attachment (2003)

    Please see the attached zip.

    Thank you so much.
    Attached Files Attached Files

  11. #11
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Auto Save Attachment (2003)

    I imported the class module from your zip file and copied the code into ThisOutlookSession.
    I then ran the Application_Startup manually (instead of restarting Outlook).
    I move an e-mail with attachments to the Temp folder, and lo and behold, the attachments were saved in C:Temp.
    So the code is correct.
    If it doesn't work for you, check the macro security level, as suggested by jscher2000 - if it is set to High, the code may not run at all.
    If that doesn't help either, add a MsgBox statement to each of the subs in the code. This should give you an indication whether the code runs.

  12. #12
    4 Star Lounger
    Join Date
    Feb 2006
    Posts
    504
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Auto Save Attachment (2003)

    Thanks, Hans. Now it works. I just find out the personal folder name is not the same as in the codes.

    Thanks again.

Posting Permissions

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