Open Any Email Attachment From Outlook

I was inspired to write some VBA code that lets you open any attachment in its native application by a recent newsgroup post from Outlook MVP Sue Mosher. The truth is I was bored, and it was easy to code because I simply reused code I had just written for the Save Incoming Attachments post. Hopefully the value of a stock code library will now be apparent to you.

This code uses the Windows Script Host Object Model to let Windows choose what program to use to open an attachment. Actually, it uses whatever file associations that were created when the program was installed. For example, .doc files usually open in Microsoft Word (if you have it installed).

So instead of instantiating the object model for every possible attachment type (very tedious), this method opens any attachment (as long as Windows knows what program to use). I haven't tested this with unknown attachments, so if anyone would like to try and let me know what happens (hopefully the file association dialog box appears), I'd be glad to hear it.

Sub OpenAttachmentInNativeApp()
' based on code posted by Sue Mosher
' http://tinyurl.com/684zg4

Dim myShell As Object
Dim MyItem As Outlook.MailItem
Dim myAttachments As Outlook.Attachments
Dim i As Long
Dim Att As String

On Error Resume Next
Select Case TypeName(Application.ActiveWindow)
    Case "Explorer"
        Set MyItem = ActiveExplorer.Selection.Item(1)
    Case "Inspector"
        Set MyItem = ActiveInspector.CurrentItem
    Case Else
End Select
On Error GoTo 0

If MyItem Is Nothing Then
    GoTo ExitProc
End If

Set myAttachments = MyItem.Attachments

If myAttachments.Count > 0 Then
    For i = 1 To myAttachments.Count
        Att = myAttachments.Item(i).DisplayName
        ' delete just in case it exists from before
        On Error Resume Next
        Kill "C:\" & Att
        On Error GoTo 0

        myAttachments.Item(i).SaveAsFile "C:\" & Att
    Next i
End If

' Windows Script Host Object
Set myShell = CreateObject("WScript.Shell")
myShell.Run "C:\" & Att

ExitProc:
Set myAttachments = Nothing
Set MyItem = Nothing
Set myShell = Nothing
End Sub

And here's a link to the newsgroup thread:

Open an attachment in a new window using its native application

Update 4/15/2009: Dick Kusleika from Daily Dose of Excel posted his version of the above code. Read his post titled Opening Outlook Attachments.

Enjoy,
JP

About JP

I'm just an average guy who writes VBA code for a living. This is my personal blog. Excel and Outlook are my thing, with a sprinkle of Access and Word here and there. Follow this space to learn more about VBA. Keep Reading »



Share This Article:

Share and bookmark this articledelicious buttonfacebook buttonlinkedin buttonstumbleupon buttontwitter button

comment bubble 19 Comment(s) on Open Any Email Attachment From Outlook:

  1. EDUARDO writes:

    Excuse me, but how can you open an email attachment without save it first in "C:\"???
    Or how can you open an attachment as same way as you open a file from the outlook preview pane???, what's the link code???

  2. As far as I know, you can't open an attachment without saving first; there's no Open Method on the Attachments Collection. If you use the code above, it works exactly the same as double clicking an attachment to an email from the preview pane (while leaving a copy of the file in the specified folder). However the code above works best when there's only one attachment to the email; it won't work properly if you have multiple attachments. It will save them all but only open the last one.

    HTH,
    JP

  3. EDUARDO writes:

    OK JP, thank you very much.

    Eduardo

  4. Mohamed Abdel Nabyu writes:

    Thanks a lot
    I give the code to our secretary & she was impressed

  5. Glad to hear it Mohamed! I mean, I hope it was useful.

    –JP

  6. RBliss writes:

    The following code will open the attachments. (I've only tested it with attachments of the same type. Not sure if it will work differently with mixed types.)

    Sub OpenAttachmentInNativeApp()
    ' based on code posted by Sue Mosher
    ' http://tinyurl.com/684zg4
    
    Dim myShell As Object
    Dim MyItem As Outlook.MailItem
    Dim myAttachments As Outlook.Attachments
    Dim i As Long
    Dim Att As String
    
    On Error Resume Next
    Select Case TypeName(Application.ActiveWindow)
        Case "Explorer"
            Set MyItem = ActiveExplorer.Selection.Item(1)
        Case "Inspector"
            Set MyItem = ActiveInspector.CurrentItem
        Case Else
    End Select
    On Error GoTo 0
    
    If MyItem Is Nothing Then
        GoTo ExitProc
    End If
    
    Set myAttachments = MyItem.Attachments
    ' Windows Script Host Object
    Set myShell = CreateObject("WScript.Shell")
    
    
    If myAttachments.Count > 0 Then
        For i = 1 To myAttachments.Count
            Att = myAttachments.Item(i).DisplayName
            ' delete just in case it exists from before
            On Error Resume Next
            Kill "C:\" & Att
            On Error GoTo 0
    
            myAttachments.Item(i).SaveAsFile "C:\" & Att
            myShell.Run "C:\" & Att
        Next i
    End If
    
    ExitProc:
    Set myAttachments = Nothing
    Set MyItem = Nothing
    Set myShell = Nothing
    End Sub
    
    • Correct, your code will open all the attachments to an email. It should work with all registered file types, opening each one in its native application. I think a new version of the code is in order, where you can specify the folder.

  7. This looks like something i need. I want to be able to send an email and then outlooks open the program for the attached file.

    The problem is, i don't know how to get the script from this site to outlook.

    plz help ;)

  8. Shazad Rojan writes:

    Hi,

    I tried to run the code as is in my outlook 2007 and got a run time error as follows:

    Run-time error '-2147024894 (80070002)';
    Method 'Run' of object 'IWshShell3' failed.

    Any help is appreciated as I don't know VBA

    i am trying to allow users to directly launch a .url attachment and not saving to the disk and then launch it. This is for our Accounting software (Navision).

    Thank you.

    • That means either:

      You don't have any program associated with the .url file extension, or
      You can only use the Run method with a file saved to disk.

  9. Hi JP

    We receive a large number of confirmation emails from brokers, in .txt format
    I have already used the code to save the attachments to a specified folder – works like a charm!
    My problem is that I need to open up these text files (which this code does) but also search for a keyword, say "Eureka", and if it finds it, save the file with "Eureka" as a prefix to the name, if not then save it as the filename of the attachment.
    How do I execute the find function using winshell on text files (.txt) and get the return value of true/false? Any help would be appreciated, however the working code would be legendary! :)

    Regards
    Wesley

  10. Dear JP, the script is really good but I have the same problem that Shazad Rojan.
    The script save the file but it got a run time error when running the following:
    myShell.Run "C:\" & Att

    Do you know why?
    I use Windows7 and Outlook 2003.

    Please JP, could you help me??… I'm desperate ;)

    • I can't tell, but Windows 7 probably has something to do with it. I'm not sure that's a problem that can be solved.

      You could try a few alternatives, for example the Shell command (which would probably cause the same error) or the FollowHyperlink function.

  11. JP -
    Trying to write code that will take newly recieved emails in a public folder with thouusands of emails and save the attachments to a specified folder. I need it to only take the new emails and only from specific senders. Can you help?

  12. Hi
    Can we have a VBA code that will open unread mail attachement.

    • Did you read the blog post? That's what the code in the post does.

      It sets a reference to the selected mail item, saves any attachment and then opens it.

This article is closed to any future comments.
Excel School