VBA in MS Outlook to save messages in a newly created folder

albertan

Board Regular
Joined
Nov 16, 2014
Messages
68
Office Version
  1. 365
I'm trying to assign a VBA script in MS Outlook so that I can get all emails saved in a newly created folder (named by date, files also named by date).

I create a folder and assign name to it:

strNewFolderName = Format(dtDate , "yyyymmdd", vbUseSystemDayOfWeek, _
vbUseSystem)

sName = Item.Subject
ReplaceCharsForFileName sName, "_"
MkDir ("C:\Users\Albertan\My Documents\Messages" & strNewFolderName)

Then I'm trying to save the email in this newly created folder:

sPath = "C:\Users\Albertan\My Documents\Messages"&StrNewFolderName
Debug.Print sPath & sName
Item.SaveAs sPath & sName, olMSG

But it is not saving there. Something wrong with my SPath. I think it is not recognizing the folder name (variable that I use).

Does anybody have any suggestion?

Thank you
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Try a slash after messages ...\messages\ if you want the folders to be called \messages\yyyy etc and also add another to the end of the spath string between spath and sname in the saveas line
 
Last edited:
Upvote 0
Try a slash after messages ...\messages\ if you want the folders to be called \messages\yyyy etc and also add another to the end of the spath string between spath and sname in the saveas line


Sorry still having issue: the folders are being created but messages not beings saved in those folders. If I defined spath why should I put a slash there, not sure:



Public Sub SaveMsgv1(Item As Outlook.MailItem)
Dim sPath As String
Dim dtDate As Date
Dim sName As String
Dim enviro As String
Dim strNewFolderName As String

dtDate = Item.ReceivedTime
strNewFolderName = Format(dtDate, "yyyymmdd", vbUseSystemDayOfWeek, _
vbUseSystem)

sName = Item.Subject
ReplaceCharsForFileName sName, "_"
MkDir ("C:\Users\Albertan\My Documents\Messages" & strNewFolderName)

sName = Format(dtDate, "yyyymmdd", vbUseSystemDayOfWeek, _
vbUseSystem) & ".msg"


' use My Documents in older Windows.
sPath = "C:\Users\Albertan\My Documents\Messages" & strNewFolderName
Debug.Print sPath & sName
Item.SaveAs sPath "& sName, olMSG"
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,219
Members
452,619
Latest member
Shiv1198

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