Macro to save selected Outlook 2010 email to a folder as a .msg file

Biking Loki

Board Regular
Joined
Aug 25, 2005
Messages
167
Is there a way, macro or otherwise, to have a one-click solution that will save the selected email to a specific folder? This macro is part of a larger workflow with another program so to have this as a single click function would be very helpful.
Thanks in advance.
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
This one seems to work okay:

Code:
Option Explicit
Public Sub SaveMessageAsMsg()
Dim oMail As Outlook.MailItem
Dim objItem As Object
Dim sPath As String
Dim dtDate As Date
Dim sName As String
Dim enviro As String
enviro = CStr(Environ("USERPROFILE"))
For Each objItem In ActiveExplorer.Selection
Set oMail = objItem
sName = oMail.Subject
ReplaceCharsForFileName sName, "_"
dtDate = oMail.ReceivedTime
sName = Format(dtDate, "yyyymmdd", vbUseSystemDayOfWeek, _
vbUseSystem) & Format(dtDate, "-hhnnss", _
vbUseSystemDayOfWeek, vbUseSystem) & "-" & sName & ".msg"
sPath = enviro & "\Documents\"
Debug.Print sPath & sName
oMail.SaveAs sPath & sName, olMSG
Next
End Sub
Private Sub ReplaceCharsForFileName(sName As String, _
sChr As String _
)
sName = Replace(sName, "/", sChr)
sName = Replace(sName, "\", sChr)
sName = Replace(sName, ":", sChr)
sName = Replace(sName, "?", sChr)
sName = Replace(sName, Chr(34), sChr)
sName = Replace(sName, "<", sChr)
sName = Replace(sName, ">", sChr)
sName = Replace(sName, "|", sChr)
End Sub
 
Upvote 0
That works great. One question though. The macro as it is written deletes the message after saving, correct? Is there a way to either leave it an it current location or move it to an archive folder within outlook?

What I an trying to do is as follows. This macro saves the message to a folder that evernote (EN) monitors. whenever that folder has a file it, EN will import it. Even though the .msg is in EN, I'd like to have a copy in Out as an archive/backup.
 
Upvote 0
It's not deleting my email afterwards. I'll see if I can find the page where it came from.
 
Upvote 0
I'm not sure why I thought it was deleting them. I tried it just now and it worked fine. How can I change the folder location? I would like to put it in a folder within the documents folder.

I tried:
sPath = enviro & "\Documents\Test\"

and

sPath = enviro & "\Documents\Test"

Neither worked.

******
I figured it out:

sPath = enviro & "\Documents\" & "\Test\"
 
Last edited:
Upvote 0
Hi,

love this macro. I am wondering if you can add some lines so that it also shows who sent the email, I.e. The file name is to show date, time, subject, sender.
 
Upvote 0
If some one had a trouble with specific path, use as it below. I tried on even Server, its work perfect.

'sPath = "D:\TestOutlook\Test1\"
 
Upvote 0

Forum statistics

Threads
1,225,727
Messages
6,186,679
Members
453,368
Latest member
xxtanka

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top