Outlook XP Custom Rule - Extract Email Attachment From An Email

At work, I have a rule in Outlook XP that forwards certain emails as an attachment to my personal email account at home. When my personal email account (accessed via Outlook XP, also) receives this forwarded email as an attachment, I want it to “extract” the attachment and place it in my Inbox. Afterwards, I want it to delete the forwarded email leaving me with just the attached email in my Inbox.

This result in the email sent to my work email account appearing in my personal email account’s Inbox as if the original email was sent directly to my personal email. No longer will I have to deal with subject lines that start off with “FW:.” Also, when I click Reply, I will be replying to the original email sender instead of my work email.

I seriously doubt there is a rule that can do this. So, I think there must be a VBA solution. I’m a VB.NET developer with very little experience in Outlook programming. Please explain your solution in simple terms.
xmlnewbieAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

David LeeCommented:
Greetings, xmlnewbie.

Let me make sure I understand the question.  You have a rule at work that forwards certain emails home as an attachment.  At home you'd like a process that stores the attached mail item in your mailbox so it appears that the message was received at your home account instead of at your wrok account.  Do I have it right?

Cheers!
0
xmlnewbieAuthor Commented:
Exactly! Basically, I want to extract the email attachments from these forwarded emails and put them in my Inbox.
0
David LeeCommented:
It's possible, but I'm not sure how practical this is.  When you send the item as an attachment Outlook converts it to a .msg file and attaches that file.  The only solution I can think of is to open it onscreen and save that item.  The code below does that.  For this to work the attached item has to be the open item.  So, you'd open the message, then open the attachment, then run this macro to save the item to the inbox.  I tested this on my comptuer using Outlook 2003 and it worked properly.  Do you need instructions on how to add this code to Outlook?

Sub SaveItemToInbox()
    Dim olkMessage As Outlook.MailItem
    Set olkMessage = Application.ActiveInspector.CurrentItem
    olkMessage.Save
    Set olkMessage = Nothing
End Sub
0
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

xmlnewbieAuthor Commented:
Can this execute on the NewMail event, instead? Also, I only want this executing for emails that contain an email attachment only. If I get an email with, say, an image as an attachment, I don't want this to do anything.

Also, yes, how do I put this code in Outlook? I'm using XP. Do I put it in "ThisOutlookSession?" So far, this is what I have:

Dim WithEvents myOlApp As Outlook.Application

Private Sub Application_Startup()
   MsgBox "Application_Startup was triggered!"
End Sub

Private Sub Application_NewMail()
     MsgBox "Application_NewMail was triggered!"
End Sub

I assume I can put the logic I need in Application_NewMail. Am I right?

0
xmlnewbieAuthor Commented:
I've been trying to work the problem using VB.NET (it's easier for me since I don't really know anything about Outlook programming). Anyway, here's what I have so far. If I can cast the attachment to a MailItem somehow, I think this problem would be solved.

        Dim MyOutlook As New Outlook.Application
        Dim Ns As Outlook.NameSpace = MyOutlook.GetNamespace("MAPI")
        Dim Inbox As Outlook.MAPIFolder = Ns.GetDefaultFolder(OlDefaultFolders.olFolderInbox)
        Dim MoveMail As Outlook.MailItem = Nothing

        Dim InboxItems As Outlook.Items = Inbox.Items

        ' Precondition 1: Email is an unread email
        InboxItems = InboxItems.Restrict("[Unread] = true")

        Try
            For Each Mail As Outlook.MailItem In InboxItems

                ' Precondition 2: Email contains one attachment
                If Mail.Attachments.Count = 1 Then
                    ' Precondition 3: Attachment is an email
                    Dim AtmtName As New IO.FileInfo(Mail.Attachments(1).FileName)
                    If AtmtName.Extension.ToLower = ".msg" Then
                        ' This cast fails
                        MoveMail = DirectCast(Mail.Attachments(1), Outlook.MailItem)
                        MoveMail.Move(Inbox)
                    End If
                End If
            Next Mail
        Catch ex As System.Exception
            Throw
        End Try

If I can get this to work in VB.NET, how hard is it to incorporate it in Outlook? What would be the best approach?
0
xmlnewbieAuthor Commented:
Also, if I am going to keep this in VB.NET, how do I respond to the NewMail event -- and is that even a good idea? I read somewhere that the NewMail event is only triggered once even when mulitple new emails arrive. I suppose that doesn't matter since I explicitly check for unread emails only.
0
xmlnewbieAuthor Commented:
I suppose I can build a COM Add-In as shown on http://support.microsoft.com/default.aspx?scid=kb;en-us;302896. However, I would rather not have a button clicked just to trigger my routine. I still think responding to the NewMail event would be best. Suggestions?
0
David LeeCommented:
Sorry, I can't answer any questions about doing this with VB.NET as I haven't taken the time to learn it yet.  The NewMail event is never good to use, other than for a broad notification mecahnism for mail arrival.  Instead, use the ItemAdd event of the folder you want to monitor.  I'm not seeing a way to do this though without clicking a button.  Here's why.  The message you want comes in as an attachment.  Outlook cannot open attachments directly.  Instead, it saves the attachment to disk and shells to the OS causing it to open the file in whatever application is associated with the attachment's file type.  In this case the file type is .msg, which is associated with Outlook.  Once the item is open in an Outlook Inspector window it's easy to get the item.  Outlook doesn't have a programming command for launching an attachment.  Saving the attachment to disk is no problem, but Outlook's object model doesn't include any means of opening or reading one.  That'll require a 3rd-party control.  Here's a link to one that'll do this: http://www.priasoft.com/exchangemigration/products_msg_main.asp  Or perhaps .NET includes functionality that I'm not familiar with that makes this possible without using a 3rd-party control.
0
xmlnewbieAuthor Commented:
I don't want to open the attachment. I want to take the attachment, which is an email (.msg), and drop it in the Inbox. I am able to do this manually through Outlook, so I am guessing there is a way to do this programatically also.
0
David LeeCommented:
I know what the attachment is.  There are lots of things that you can do manually that Microsoft did not make available to programmers via Outlook's object model.  The only methods for an attachment object are Delete and SaveAsFile.  I've tried creating an item directly from an attachment, but Outlook generates an error.  I don't see a way to do it without saving the attachment to disk and opening it from there.  But I have to allow for the possibility that there's a undocumented or poorly documented way of doing this that I have yet to discover.
0
xmlnewbieAuthor Commented:
Once saved to the disk, can it be placed in the Inbox?
0
David LeeCommented:
Not without opening it in a window.  I thought about using the CreateItemFromTemplate method, but that's intended to create a new item that you're going to send from an existing message template.  It's not possible to simply create a blank message and set all the properties based on the properties of the attached message since many of a message's properties are read-only.  It's also impossible to use the Move method since Outlook doesn't see the attachment as an Outlook object (i.e. a message).  That leaves the Copy method.  For Copy to work the item has to appear as an Outlook message.  The only way i can see to do that is to open the item in a window.  Outlook doesn't even provide a means of reading a .msg file back into Outlook, other than the aforementioned CreateItemFromTemplate method.  Doing that requires making a call to the OS via a shell command to open the file using the associated program.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
David LeeCommented:
Any update, xmlnewbie?
0
xmlnewbieAuthor Commented:
I'm still working the issue. I'll post my solution here if I ever figure this out.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Outlook

From novice to tech pro — start learning today.