0

I have an email account that contains two aliases, pod@ and invoices@. There is a rule to move mail received by pod@ to POD folder (Outlook folder, not HDD).

I have run a script VBA code like: Save attachments to a folder and rename them

I need to set the same rule for invoices@ so the attachments are saved to another Network Drive.

If add another script of the same but with different location, I can't choose any of the scripts when setting up the Outlook rule, they become invisible.

I would like to have scripts running in different rules saving attachments to different network folders.

OR

One rule with script that will determine which alias mail was sent to, move mail to specific Outlook folder then save attachments to different network folders.

Also I found a problem running the current script. When trying to save attachments, to the network folder and the network folder is not accessible, the rule crashes.

I don't know VBA or any other languages.

EDITED:

As per answer below:

Public Sub saveAttachtoDisk_invoices (itm As Outlook.MailItem) 
    Dim objAtt As Outlook.Attachment 
    Dim saveFolder As String Dim dateFormat
    dateFormat = Format(Now, "yyyy-mm-dd H-mm")  saveFolder = "C:\Temp\"
    on error goto ErrorHandler
    For Each objAtt In itm.Attachments    
        objAtt.SaveAsFile saveFolder & "\" & dateFormat & objAtt.DisplayName
    Next
    on error goto 0

    exit sub

ErrorHandler:
    debug.print itm.subject
    debug.print err.number & ": " & err.description & " on save line in saveAttachtoDisk_invoices"
    resume next
End Sub

I added a message so the end user will know if an error occurs and a file was not save. I have added this line to the ErrorHandler

MsgBox objAtt.DisplayName & ": " & Err.Description

that will display the File name and a description that the path does not exist.

Community
  • 1
  • 1
Kos
  • 1
  • 1
  • 4
  • 1
    Can you post your code? Without seeing the code, it's hard to say what you need to do. Sounds like you might be able to just add an `IF` statement to check condition and then set a string variable for the output folder. See IF-THEN-ELSE for a brief overview. Also, MSDN – CBRF23 Nov 20 '14 at 12:42
  • Don't know why my html markups not working. http://www.techonthenet.com/excel/formulas/if_then.php and http://msdn.microsoft.com/en-us/library/752y8abs.aspx – CBRF23 Nov 20 '14 at 12:44

1 Answers1

0

Use different script names. You can keep the rule from crashing by bypassing errors due to network unavailability or while attempting to save unsavable attachments.

Public Sub saveAttachtoDisk_invoices (itm As Outlook.MailItem) 
    Dim objAtt As Outlook.Attachment 
    Dim saveFolder As String Dim dateFormat
    dateFormat = Format(Now, "yyyy-mm-dd H-mm")  saveFolder = "C:\Temp\"
    on error goto ErrorHandler
    For Each objAtt In itm.Attachments    
        objAtt.SaveAsFile saveFolder & "\" & dateFormat & objAtt.DisplayName
    Next
    on error goto 0

exit sub

ErrorHandler:
debug.print itm.subject
debug.print err.number & ": " & err.description & " on save line in saveAttachtoDisk_invoices"
 resume next
End Sub
niton
  • 8,771
  • 21
  • 32
  • 52
  • Thank you niton! that what I mean... I will try this one, but looking at it I presume it's should be working fine. – Kos Dec 01 '14 at 14:41
  • hi Niton, I can confirm it's working absolutely fine! – Kos Dec 02 '14 at 17:41