I don’t know why there’s not an easier way to open attachments from an email I’m viewing, but there’s not. I made stole some VBA code to do the job.
First, I borrowed JP’s code to Open Any Email Attachment From Outlook. Then I changed a few things. I only wanted to open the first attachment, not all attachments. In the vast majority of cases, there’s only one attachment. If there is more, I’ll just use the mouse. I still have a mouse connected to my computer, despite rumors to the contrary.
Next, I changed the code to save the temporary in the Temp directory rather than the root directory. There’s no real difference, it’s just a personal preference. I used the Environ$ function to find the Windows Temporary Folder.
Finally, I added a toolbar tool to a new mail message. I can now use Alt+M to open the first attachment to any open mail item.
My first attempt was a great success. I opened a pdf file beautifully. My second attempt produced an error. I was trying to open an xls file with spaces in the file name. I tried all manner of quotes and brackets, but couldn’t get around the error. I ended up converting the filename to the DOS 8.3 name, otherwise known as the short file name, using API code from FreeVBCode.com. It’s been working well since.
Here’s the code:
‘ based on code posted by Sue Mosher
‘ http://tinyurl.com/684zg4
Dim oShell As Object
Dim miItem As MailItem
Dim sFileName As String
Dim sPath As String
sPath = VBA.Environ$(“Tmp”) & “”
On Error Resume Next
Select Case TypeName(Application.ActiveWindow)
Case “Explorer”
Set miItem = ActiveExplorer.Selection.Item(1)
Case “Inspector”
Set miItem = ActiveInspector.CurrentItem
Case Else
End Select
On Error GoTo 0
If Not miItem Is Nothing Then
If miItem.Attachments.Count > 0 Then
sFileName = miItem.Attachments.Item(1).DisplayName
‘ delete just in case it exists from before
On Error Resume Next
Kill sPath & sFileName
On Error GoTo 0
miItem.Attachments.Item(1).SaveAsFile sPath & sFileName
End If
‘convert sfilename to dos 8.3
sFileName = GetShortFileName(sPath & sFileName)
‘ Windows Script Host Object
Set oShell = CreateObject(“WScript.Shell”)
oShell.Run sFileName
End If
Set miItem = Nothing
Set oShell = Nothing
End Sub
I’m confused. What’s wrong with double clicking the attachment icon?
Or should this be titled, “Opening Outlook Attachments with the Keyboard”?
Everything I post should end with “with the keyboard”, so I just started leaving it off.
I tried this out and I receive a compile error when it runs, “Sub or Function not Defined”. The debugger is referencing “GetShortFilename” as the error.
It’s missing the “GetShortFilename” API Function, I would have run it with ShellExecute so you don’t need this Function.
Zac: Follow the FreeVBCode link to get the API function.
Slightly off topic but bitter personal experience demands I post this. If you are handling multiple attachments on a single Outlook email process them in reverse order. I got caught by a ‘known’ bug when I tried to process from first to last.
Thanks as usual to a complete stranger somewhere on the web for the insight, and his name was Ken Slovak (Outlook MVP) http://www.slovaktech.com.
Ugh, good point gruf999. I read my email in plain text. When someone sends an embedded picture, it shows as an attachment – the first attachment. If I’m only opening one, I should be opening the last one. Off to change my code.
Thanks Dick, This is just what I have looking for. Its works great. There’s just one problem. On using the CTRL + SHIFT + F to access the Advanced Find dialog box, I got this Macro inserted in the Menu Bar. However the Macro does not run in this Advanced Find Dialog box menu bar.
Any work round this as it would greatly aid me in my daily work.
Neverthless Thanks so much for this piece of Code.
[…] 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. […]
i used it for my plugin for “extract attachments from outlook“