How can I save an attachment from an RSS Feed folder

Rob4077 used Ask the Experts™
I have subscribed to an RSS Feed of MP3 files that I save in a sub folder of my RSS Feeds folder called "Italian Mags MP3".  Each email contains an MP3 file and the first line in each email contans a code that identifies the file in the format  "a MP3 yyyy-mmn" where
    a is a two or 3 character code,
    MP3 is a constant  
   yyyy-mm is the year and month of the file and
   n is a further identifier of the file

What I would like is do develop an Outlook Rule or a VBA macro that runs as soon as the RSS Feed is updated that will save the attachment in a folder on my PC based on the month (mm) content of the first line of the email. Specifically the path would be C:\Users\Rob\Music\Italian\mm where mm is the month corresponding to the mm in the date component of the code.

If necessary I can easily write a VBA function that will extract the mm component I am looking for, but what I don't know is the best and easiest way to accomplish the rest, or if it is even possible.

I guess I need something that will
   Check RSS Feeds as soon as they hit this folder
   Run my function to determine the mm content
  Save the attachment in the appropriate folder

Can anyone suggest how I do this, of if I am asking too much of Outlook
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®


Actually I just found an easier way to identify the month. The month can simply be identified as the month of the date when the file was sent
Hi Rob, I tested this in Outlook 2010, just place the code below into ThisOutlookSession, add a reference to Microsoft Scripting Runtime, save and restart Outlook. I modelled it as close as I can to the information you provided but there will likely be some errors before we get to the final product. I don't have the RSS feed, but essentially this code establishes a watch on the folder, and when an email is added to the folder, it triggers the macro. It uses a combination of MID & INSTR to find the relevant month, or you can substitute with Month(Item.ReceivedTime) if you prefer.
Option Explicit
' Adapted from Daniel Dusek

Private Const ITALIAN_MP3_FOLDER As String = "Italian Mags MP3"
Private WithEvents m_outlookFolderItems As Outlook.Items

Private Sub Application_Startup()
End Sub

Private Sub m_outlookFolderItems_ItemAdd(ByVal Item As Object)
' You have new mail in folder ITALIAN_MP3_FOLDER
' Add reference to Microsoft Scripting Runtime
Dim att As Attachment, attFolder As String
' Identifies folder and creates new one if required
attFolder = "C:\Users\Rob\Music\Italian\" & Mid(Item.Subject, InStr(1, Item.Subject, "-") + 1, 2) & "\"
CheckFolder attFolder
If Item.Attachments.Count > 0 Then
  For Each att In Item.Attachments
    att.SaveAsFile attFolder & att.DisplayName
    'MsgBox "RSS MP3 saved to : " & attFolder & att.DisplayName
  Next att
End If

End Sub

Private Sub Initialize_Handler()
    Dim outlookFolder As Outlook.MAPIFolder
    Dim defaultRSSFolder As Outlook.MAPIFolder
    Dim outlookNameSpace As Outlook.NameSpace

    Set outlookNameSpace = Outlook.GetNamespace("MAPI")
    Set defaultRSSFolder = outlookNameSpace.GetDefaultFolder(olFolderRssFeeds)
    Set outlookFolder = defaultRSSFolder.Folders(ITALIAN_MP3_FOLDER)
    Set m_outlookFolderItems = outlookFolder.Items
End Sub

Private Sub CheckFolder(folder_name As String)
'Adapted from
Dim FSO As FileSystemObject, newfolder
Set FSO = New FileSystemObject
If Not FSO.FolderExists(folder_name) Then
   newfolder = FSO.CreateFolder(folder_name)
End If
End Sub

Open in new window

It has been adapted from a variety of sources on the web, referenced in the code if you want to check further there are some brilliant people out there who can make Outlook jump through hoops.

Let me know how you go


Hi Terry,
That is absolutely amazing!!!! I changed the code to take the date from the alternative you proveded, namely Month(Item.ReceivedTime), as it proved more reliable, but apart from that it worked first time. Thank you so much.

I am familiar with basic VBA code but what I don't understand is how the watch works. How did you know what code to use to watch for the arrival of an email into the feed (Sub m_outlookFolderItems_ItemAdd(ByVal Item As Object))? There must be somewhere that shows/lists the varous watches you can add to code.

Anyway I am very grateful that you put this together for me.
Ha I'm amazed it worked first time, it is a credit to how well you have detailed your question.  I'm not all that familiar with VB so I learned quite a bit myself in the process, and it is only the tip of a very large iceberg I expect. When an item is moved to a folder it triggers a number of "events" (one being folder_ItemAdd) which is "handled" by Outlook. The WithEvents & Initialize_Handler code above allows the event to be detected by the programmer as well. There are lots of events, I just had a look in VBE at View > Object Browser. I'm going to hunt down some articles to help me transition a bit further along with VB, it is really powerful, and as you can see it works happily with VBA.



Thanks Terry, appreciate help very much, and the explanation of where to find more information. Like you I will have to hunt down some more information.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial