Consulting

Page 1 of 2 1 2 LastLast
Results 1 to 20 of 22

Thread: Solved: Problems with automatic save attachment

  1. #1

    Question Solved: Problems with automatic save attachment

    Hi,
    I am using Oulook 2003 and found and implemented the code by Killian to automatically save attachments in mails moved to a specific folder. This works fine as long as I manually move the mail in outlook. As soon as I install a rule to do this for me, the save of the attachment is no longer executed. Any idea what is going wrong ?
    In the description it is mentioned this should work with a rule also...

    This is the code I use :

    '########################################################################## #####
    '### 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("ELSO").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

    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

    '########################################################################## #####
    '### print routine
    Sub PrintAtt(fFullPath As String)

    Dim xlApp As Excel.Application
    Dim wb As Excel.Workbook

    'in the background, create an instance of xl then open, print, quit
    Set xlApp = New Excel.Application
    Set wb = xlApp.Workbooks.Open(fFullPath)
    wb.PrintOut
    xlApp.Quit

    'tidy up
    Set wb = Nothing
    Set xlApp = Nothing

    End Sub



    I do not refer to the print routine for the moment.
    The rule is quite simple and moves incoming mail from a certain address to the ELSO folder.

    Another thing I tried out is to have this work under the Inbox instead of Personal Folders as I normally do not work with PF, but the Set TargetFolderItems does not seem to work on Inbox ? I receive run-time error '-1663827697(9cd7010f) : An object could not be found' when starting Outlook.


    Thanks in advance,
    Ingrid
    Last edited by ingridbl9; 05-05-2006 at 04:32 AM.

  2. #2
    VBAX Master Killian's Avatar
    Joined
    Nov 2004
    Location
    London
    Posts
    1,132
    Location
    Hi Ingrid,

    This should work fine with a rule, since it's using the Items_Add event - it shouldn't matter how the Item is added. Provided the Application_Startup code has run to set the folder items' event...

    Regarding the Inbox: it is one of the fixed folders in Outlook (like the calendar, notes, task etc) so you need to use the GetDefaultFolder method[VBA]Set TargetFolderItems = ns.GetDefaultFolder(olFolderInbox).Items[/VBA]
    K :-)

  3. #3
    Hi Killian,

    Thx, the suggestion for the Inbox works perfect !
    But with the rule it still isn't functioning
    I am still a novice in Outlook VBA, so what do you mean when you say the Application_Startup Code must have run ?
    Your help is much appreciated ...
    Ingrid

  4. #4
    Hi Killian,

    Following your reply I did some debugging and the Application_Startup event code is processed allright, but the ItemAdd event is not triggered when I run my rule (which I already guessed). Are you sure this works at your end ? Is there any reason my Outlook is reacting differently ?

    In addition, if I get the same document name, the Save As doesn't seem to overwrite the older version, which it should be doing for my purpose. Anything I can do about that ?

    Thanks again - I am becoming a bit frustrated
    Ingrid
    Last edited by ingridbl9; 05-08-2006 at 05:06 AM.

  5. #5
    VBAX Master Killian's Avatar
    Joined
    Nov 2004
    Location
    London
    Posts
    1,132
    Location
    hmm... curious...
    to be sure, I just tested it using the code posted in post#1:

    Set the watched folder as "Temp" in "Personal Folders"
    Set up a rule to move mail to Personal Folders>Temp
    Ran the App-Startup routine
    Sent some mail

    Result: the rule moved the mail, the MailAdd event fired, attachments were saved.
    I also checked what happens with a file of the same name - the existing (old) file is overwritten

    So I'm a bit confused... I'll have a think about it.
    K :-)

  6. #6
    puzzling indeed - I am correct in setting up the code under ThisOUtlookSession? or is this where I am going wrong ?

    For the moment I am moving the mails manually as this triggers the event, but this is not as it should work as this doesn't give an immediate result on mail arrival.
    Anything you can come up with I am willing to try out... learning all the time !

    already big thanks
    Ingrid

  7. #7
    VBAX Master Killian's Avatar
    Joined
    Nov 2004
    Location
    London
    Posts
    1,132
    Location
    Quote Originally Posted by ingridbl9
    puzzling indeed - I am correct in setting up the code under ThisOUtlookSession? or is this where I am going wrong ?
    this is quite correct, so no problem there.

    I have come across issues with the ItemAdd event failing to fire - these are usually related to processing large numbers of mailitems (MS KB article).
    But I'm guessing you tested it with a single file to start with...
    K :-)

  8. #8
    yes, of course - tried it out with anything from 1 up to 10 messages.
    As I am probably stuck here, (have been looking around on microsoft sites but only reported problem is when too many items are moved), can't I work around this and forget about the rule altogether ?

    Can't I add the check of the inbox in my vba code ? It only needs to check whether an incoming message has an attachment and the subject does not contain RE: or FW: (as in reply or forward) and then execute the save attachment. The mailbox concerned is only used for sending the documents that need to be saved.

    Or am I asking too much now ?

    Appreciate the help - again -
    Ingrid

  9. #9
    VBAX Master Killian's Avatar
    Joined
    Nov 2004
    Location
    London
    Posts
    1,132
    Location
    The fact that this code works fine for me but not for you (we're both on OL2003) makes me think there's something else going on here but while I was struggling to work out what that is, I found a much better way of doing the whole thing...

    In the rules wizard, as well as moving your mail item, you can then choose to run a script. What I didn't realise, was that if you include a mailitem object as an argument to that routine, it will indeed refer to the mailitem the rule is applied to! Perfect!

    So, set the rule for incoming mail to move the mail and run a script.
    This is the script to run:[VBA]Public Sub RuleScriptSaveAtt(Item As MailItem)

    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)
    olAtt.SaveAsFile FILE_PATH & olAtt.FileName
    Next
    End If
    Set olAtt = Nothing

    End Sub[/VBA]
    K :-)

  10. #10

    Thumbs up

    YESSS
    Works perfect - I had seen the script option, but there again my knowledge ran short... I am just a mainframe programmer doing some Microsoft stuff on the side...

    Anyway thanks very much for the prompt responses and the solution, I'll certainly keep my eye on your forum, maybe I can help someone else out sometime.

    Ingrid

  11. #11
    VBAX Master Killian's Avatar
    Joined
    Nov 2004
    Location
    London
    Posts
    1,132
    Location
    Glad its working - we got there in the end

    I've marked the thread as solved...
    K :-)

  12. #12
    VBAX Regular
    Joined
    Jan 2008
    Posts
    10
    Location
    Quote Originally Posted by Killian
    The fact that this code works fine for me but not for you (we're both on OL2003) makes me think there's something else going on here but while I was struggling to work out what that is, I found a much better way of doing the whole thing...

    In the rules wizard, as well as moving your mail item, you can then choose to run a script. What I didn't realise, was that if you include a mailitem object as an argument to that routine, it will indeed refer to the mailitem the rule is applied to! Perfect!

    So, set the rule for incoming mail to move the mail and run a script.
    This is the script to run:[vba]Public Sub RuleScriptSaveAtt(Item As MailItem)

    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)
    olAtt.SaveAsFile FILE_PATH & olAtt.FileName
    Next
    End If
    Set olAtt = Nothing

    End Sub[/vba]
    How would I use this to save the attachment to a specific folder on my network drive? The location I would like to save to is "L:\M5" and the file already exists so it needs to be overwritten every time the rule is run.

    Also, is it ok if I chose to select more conditions in the set up rules wizard?

    Thanks for the help as I am new to VBA!

  13. #13
    VBAX Regular
    Joined
    Jan 2008
    Posts
    10
    Location
    anyone?

  14. #14
    VBAX Newbie
    Joined
    Sep 2008
    Posts
    1
    Location

    Help needed

    Quote Originally Posted by JAG836
    anyone?
    Thanks for the update on the email attachment save in local disk's folder.

    i have copied the given code in the ThisOutlookSession module.
    how do i run it.
    i have closed my Outlook and restarted it, but there i could not find any attachment from temp folder in outlook copied to c:\temp.

    How do i do it. please guide.

    Thanks
    Vish

  15. #15
    Quote Originally Posted by vish
    Thanks for the update on the email attachment save in local disk's folder.

    i have copied the given code in the ThisOutlookSession module.
    how do i run it.
    i have closed my Outlook and restarted it, but there i could not find any attachment from temp folder in outlook copied to c:\temp.

    How do i do it. please guide.

    Thanks
    Vish

  16. #16
    Hi,

    You have to run the script in a rule that is triggered on new arrival of mail or on a specific subject in new mail.

    Good luck,
    Ingrid

  17. #17

    Outlook 2007 saving attachments to disk

    Trying to make the script below work for me, and it fails, with
    Dim WithEvents TargetFolderItems aAs Items

    not sure why??

  18. #18
    VBAX Newbie
    Joined
    May 2009
    Posts
    2
    Location
    Can I set this up to watch two personal folders and save the attachments from each personal folder to different folders on my hard drive? I had the original code by Killian working great, but can't seem to get this to work on different folders.
    Thanks in advance.

  19. #19
    I haven't tried that... but I am having trouble with my Outlook runs not running automatically...?!?

  20. #20
    VBAX Newbie
    Joined
    May 2009
    Posts
    2
    Location
    I played around and fixed my own question. I created and defined other arguments TargetFolderItems1 and FILE_PATH1, then duplicated the TargetFolderItems_ItemAdd routine and changed to changed it to save to FILE_PATH1. Everything seems to be working now.

    Thanks to everyone for all of these posts. Now if someone could write some VBA to do the rest of my work for me too, I would appreciate it!

Posting Permissions

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