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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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

Enjoy,
JP



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 261 views since May 29, 2008 – 1:21 am.
  1. 3 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

Post a Comment

Browse Posts:


« XML Parsing Function | Highlight And Move Multiple Emails »