Search code examples
vbams-accessoutlook

Save Selected Outlook Email Message as .msg File via MS Access VBA


I managed to get this code to select and save an Outlook email to folder on my PC via MS Access.
https://social.msdn.microsoft.com/Forums/en-US/edf1d135-ffd3-4b3a-8318-db7f86047872/save-a-selected-outlookmail-as-msgfile-with-vba-in-access?forum=outlookdev

Option Compare Database
Option Explicit
Private Const SavePath As String = "D:\New folder\"
Sub SaveSelectMail()
    Dim olApp As Outlook.Application
    Dim olExplorer As Outlook.Explorer
    Dim olMail As Outlook.MailItem
    
    Set olApp = New Outlook.Application
    Set olExplorer = olApp.ActiveExplorer
    Set olMail = olExplorer.Selection(1)
    
    olMail.SaveAs SavePath, OlSaveAsType.olMSG
    
    Set olMail = Nothing
    Set olExplorer = Nothing
    Set olApp = Nothing
End Sub

I created a form in Access with a command button with the following code (on click event):

Private Sub Command6_Click()
Call SaveSelectMail
End Sub

When I click on the button the following error pops up (Outlook is open):

Run-time error '-2147352567 (80020009)': Cannot write to D:\New Folder. Right-click the folder that contains the file you want to write to, and then click properties on the shortcut menu to check your permission for the folder.

I checked folder properties but have no idea what should I do.

I tried to set the path to a folder on my portable HD. The same error pops.


Solution

  • Will assume you have an actual existing folder path in place of "D:\New Folder\".

    Must include name for saved file in the Path argument. SavePath & "somefilename.msg".

    If you want the filename to be dynamic, concatenate variables, options:

    1. user input in a form textbox: SavePath & Me.tbxFile & ".msg"

    2. constructed from attributes of the email