2

Following site. Created this code into a modul:

Public Sub saveAttachtoDisk(itm As Outlook.MailItem)    
Dim objAtt As Outlook.Attachment

For Each objAtt In itm.Attachments
    objAtt.SaveAsFile "C:\Data\" & objAtt.DisplayName
    Set objAtt = Nothing
Next
End Sub

But nothing happens when I set up a rule which calls the script everytime a mail comes in.

InDubio
  • 67
  • 1
  • 9

1 Answers1

0

what your doing looks correct..

Are you sure there are attachments attached to your email ?

and if so have you tried debugging to make sure this sub is being called ?

if it is then have you checked its actually fetching the attachments from the email ?

or is it returning an empty collection of attachments ?

also you can simplify your for each like so -

Public Sub saveAttachtoDisk(itm As Outlook.MailItem)
        For Each objAtt As Outlook.Attachment In itm.Attachments
            objAtt.SaveAsFile("C:\Data\" & objAtt.DisplayName)
        Next
    End Sub
scriptss
  • 1
  • 1
  • yes, there are attachments. how can i debug it since it has an argument in the function? – InDubio Nov 29 '16 at 12:19
  • run the program in debug and put a breakpoint on the for each, hover over itm.Attachments and see if there any attachments in that collection, if there aren't then there's an issue fetching the attachments, if there are then there's more than likely an issue with saving them – scriptss Nov 29 '16 at 12:52
  • A method of debugging is [here](https://stackoverflow.com/questions/5029141/debugging-an-outlook-2007-script-fired-by-a-rule). – Kalin Aug 30 '19 at 18:02