Opening Outlook Attachments

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.

outlook mail toolbar

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:

Public Sub OpenAttachment()
 
    ‘ 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
Posted in Uncategorized

10 thoughts on “Opening Outlook Attachments

  1. I’m confused. What’s wrong with double clicking the attachment icon?
    Or should this be titled, “Opening Outlook Attachments with the Keyboard”?

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

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

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

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


Posting code? Use <pre> tags for VBA and <code> tags for inline.

Leave a Reply

Your email address will not be published.