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.
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
|
Email This Post
|
Subscribe to Posts Feed
|
Subscribe to Comments
Filed Under: Outlook, VBA, automation
Tags: attachments, native, Outlook, VBA, Windows Script Host










5 Responses to “Open Any Email Attachment From Outlook”:
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
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
OK JP, thank you very much.
Eduardo
By EDUARDO on Aug 28, 2008
Thanks a lot
I give the code to our secretary & she was impressed
By Mohamed Abdel Nabyu on Oct 15, 2008
Glad to hear it Mohamed! I mean, I hope it was useful.
–JP
By JP on Oct 17, 2008