Open Any Email Attachment From Outlook


May 29, 2008 – 1:21 am by JP

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.

VBA:
  1. Sub OpenAttachmentInNativeApp()
  2. ' based on code posted by Sue Mosher
  3. ' http://tinyurl.com/684zg4
  4.  
  5. Dim myShell As Object
  6. Dim MyItem As Outlook.MailItem
  7. Dim myAttachments As Outlook.Attachments
  8. Dim i As Long
  9. Dim Att As String
  10.  
  11. On Error Resume Next
  12. Select Case TypeName(Application.ActiveWindow)
  13.     Case "Explorer"
  14.         Set MyItem = ActiveExplorer.Selection.Item(1)
  15.     Case "Inspector"
  16.         Set MyItem = ActiveInspector.CurrentItem
  17.     Case Else
  18. End Select
  19. On Error GoTo 0
  20.    
  21. If MyItem Is Nothing Then
  22.     GoTo ExitProc
  23. End If
  24.  
  25. Set myAttachments = MyItem.Attachments
  26.  
  27. If myAttachments.Count> 0 Then
  28.     For i = 1 To myAttachments.Count
  29.         Att = myAttachments.Item(i).DisplayName
  30.         ' delete just in case it exists from before
  31.         On Error Resume Next
  32.         Kill "C:\" & Att
  33.         On Error GoTo 0
  34.        
  35.         myAttachments.Item(i).SaveAsFile "C:\" & Att
  36.     Next i
  37. End If
  38.  
  39. ' Windows Script Host Object
  40. Set myShell = CreateObject("WScript.Shell")
  41. myShell.Run "C:\" & Att
  42.  
  43. ExitProc:
  44. Set myAttachments = Nothing
  45. Set MyItem = Nothing
  46. Set myShell = Nothing
  47. End Sub

And here's a link to the newsgroup thread:

Open an attachment in a new window using its native application

Enjoy,
JP


Share and Enjoy:
  • StumbleUpon
  • Technorati
  • Digg
  • Google
  • del.icio.us
  • MisterWong

Print This Post Print This Post  |  Email This Post Email This Post  |  Permalink  |  Subscribe to this feed Subscribe now!

Filed Under: Outlook, VBA, automation
Tags: , , , ,

This post has 765 views since May 29, 2008 – 1:21 am.
  1. 5 Responses to “Open Any Email Attachment From Outlook”

  2. 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???

    By EDUARDO on Aug 26, 2008

  3. 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

    By JP on Aug 27, 2008

  4. OK JP, thank you very much.

    Eduardo

    By EDUARDO on Aug 28, 2008

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

    By Mohamed Abdel Nabyu on Oct 15, 2008

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

    --JP

    By JP on Oct 17, 2008

Post a Comment

To post VBA code in your comment, use [VBA] tags, like this: [VBA]Code goes here[/VBA].





Subscribe without commenting

Keep Reading:

Browse Posts:


« XML Parsing Function || Highlight And Move Multiple Emails »