Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Outlook VBA macro for saving emails copies in a local folder

Tags:

vba

outlook

Whenever sending an email, I would like a copy of that email to be saved in the local folder, together with all attachments.

I don't think this is possible with a custom rule in Outlook but perhaps it could be done with a VBA script?

I use Outlook and MS Exchange.

like image 919
Datageek Avatar asked Jul 30 '14 14:07

Datageek


1 Answers

Sure it can be done using the Application_ItemSend event procedure to call a custom procedure which will save your sent mails to a local folder.

This code goes in "ThisOutlookSession" module.

Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
    Call SaveACopy(Item)
End Sub

Sub SaveACopy(Item As Object)
    Const olMsg As Long = 3

    Dim m As MailItem
    Dim savePath As String

    If TypeName(Item) <> "MailItem" Then Exit Sub

    Set m = Item

    savePath = "c:\users\your_user_name\desktop\"  '## Modify as needed
    savePath = savePath & m.Subject & Format(Now(), "yyyy-mm-dd-hhNNss")
    savePath = savePath & ".msg"


    m.SaveAs savePath, olMsg


End Sub

You will need to ensure that the specified path is unique/etc., the above example is fairly crude. You also need strip out any illegal characters that can't be put in a file name (slash, pipes, etc.)...

As an alternative, I would suggest simply archiving your folder(s) periodically. You can configure Outlook to save a copy of sent mail to a "Sent" folder, and then you should be able to archive that folder; saving each item individually seems less-than-optimal.

like image 98
David Zemens Avatar answered Oct 21 '22 08:10

David Zemens