Search code examples
vbaemailoutlookemail-attachments

Outlook rule with vba-script don't start automatically


I use the following VBA Script in Outlook 2013 to automatically save xlsx attachments:

Public Sub SaveAttachmentsToDisk(MItem As Outlook.MailItem)
    Dim oAttachment As Outlook.Attachment
    Dim sSaveFolder As String
    
    sSaveFolder = "C:\EXAMPLEPATH\"
    For Each oAttachment In MItem.Attachments
        If oAttachment.DisplayName Like "*.xlsx" Then
            oAttachment.SaveAsFile sSaveFolder & oAttachment.DisplayName
        End If
    Next
End Sub

I setup a rule to start the script. When I run the rule manually it works fine. But it won't work automatically and I don't understand why.
Any ideas what the problem is?

EDIT: I found the problem. We are using "Kerio Connect". That is the reason why the rules not working. Thanks you all!


Solution

  • First of all, I'd suggest assigning a very simple sub to the rule to make sure it is triggered by the rule:

    Public Sub SaveAttachmentsToDisk(MItem As Outlook.MailItem)
        MsgBox MItem.Subject
    End Sub
    

    After you know the code is invoked by Outlook and your rule is triggered correctly you may start looking at the code more closely.

    You need to make sure the path exists on the disk and the file name (see the DisplayName property value) doesn't contain any forbidden symbols for file names.

    Consider setting a breakpoint for debugging the code when the rule triggers in Outlook.