Solved

How can I save an attachment from an RSS Feed folder

Posted on 2012-12-29
5
375 Views
Last Modified: 2012-12-30
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
0
Comment
Question by:Rob4077
  • 3
  • 2
5 Comments
 

Author Comment

by:Rob4077
Comment Utility
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
0
 
LVL 16

Accepted Solution

by:
terencino earned 500 total points
Comment Utility
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 http://stackoverflow.com/questions/12457471/how-do-i-set-a-macro-to-launch-every-time-an-email-arrives-in-a-certain-subfolde

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

Private Sub Application_Startup()
    Initialize_Handler
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 http://www.devguru.com/technologies/vbscript/quickref/filesystemobject_folderexists.html
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
...Terry
0
 

Author Comment

by:Rob4077
Comment Utility
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.
0
 
LVL 16

Expert Comment

by:terencino
Comment Utility
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.

Cheers
...Terry
0
 

Author Comment

by:Rob4077
Comment Utility
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.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Learn more about how the humble email signature can be used as more than just an electronic business card. When used correctly, a signature can easily be tailored for different purposes by different departments within an organization.
Create high volume marketing opportunities using email signatures with these top 10 DOs and DON'Ts of email signature marketing.
This Experts Exchange video Micro Tutorial shows how to tell Microsoft Office that a word is NOT spelled correctly. Microsoft Office has a built-in, main dictionary that is shared by Office apps, including Excel, Outlook, PowerPoint, and Word. When …
To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now