[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

Outlook XP Custom Rule - Extract Email Attachment From An Email

Posted on 2006-04-19
16
Medium Priority
?
1,016 Views
Last Modified: 2007-12-19
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.
0
Comment
Question by:xmlnewbie
  • 8
  • 6
14 Comments
 
LVL 76

Expert Comment

by:David Lee
ID: 16507103
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
 

Author Comment

by:xmlnewbie
ID: 16509420
Exactly! Basically, I want to extract the email attachments from these forwarded emails and put them in my Inbox.
0
 
LVL 76

Expert Comment

by:David Lee
ID: 16509731
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
Easily manage email signatures in Office 365

Managing email signatures in Office 365 can be a challenging task if you don't have the right tool. CodeTwo Email Signatures for Office 365 will help you implement a unified email signature look, no matter what email client is used by users. Test it for free!

 

Author Comment

by:xmlnewbie
ID: 16510776
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
 

Author Comment

by:xmlnewbie
ID: 16512197
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
 

Author Comment

by:xmlnewbie
ID: 16512233
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
 

Author Comment

by:xmlnewbie
ID: 16514038
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
 
LVL 76

Expert Comment

by:David Lee
ID: 16514998
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
 

Author Comment

by:xmlnewbie
ID: 16518625
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
 
LVL 76

Expert Comment

by:David Lee
ID: 16519602
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
 

Author Comment

by:xmlnewbie
ID: 16520305
Once saved to the disk, can it be placed in the Inbox?
0
 
LVL 76

Accepted Solution

by:
David Lee earned 400 total points
ID: 16520692
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
 
LVL 76

Expert Comment

by:David Lee
ID: 16570335
Any update, xmlnewbie?
0
 

Author Comment

by:xmlnewbie
ID: 16610838
I'm still working the issue. I'll post my solution here if I ever figure this out.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will help to fix the below errors for MS Exchange Server 2016 I. Certificate error "name on the security certificate is invalid or does not match the name of the site" II. Out of Office not working III. Make Internal URLs and Externa…
A few solutions to a problem some of us have been having when trying to add Hostgator email accounts to Outlook 2016 (will probably work with Outlook 2013 as well).
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
The video provides a quick and easy steps to migrate MBOX file to well known Outlook PST and Office 365. Besides this, it also supports and migrates more than 20 email clients of MBOX which include AppleMail, Opera, Thunderbird and SeaMonkey effortl…
Suggested Courses

590 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