Consulting

Results 1 to 3 of 3

Thread: Solved: Open attachment in Outlook MailItem with VBA

  1. #1
    VBAX Regular
    Joined
    May 2005
    Location
    Tallinn
    Posts
    26
    Location

    Solved: Open attachment in Outlook MailItem with VBA

    Hello!
    Can somebody help me:

    I have active MailItem object and Attachments collection within it.
    I made loop:

    Dim myItem As Outlook.MailItem
    Dim myAttachments As Outlook.Attachments
    Dim myAttachment As Outlook.Attachment

    For Each myAttachment In myAttachments

    'Opens some userform with several myAttachment parameters

    'How can user open active attachment directly within form without saving it to drive with VBA?

    Next

  2. #2
    VBAX Master Killian's Avatar
    Joined
    Nov 2004
    Location
    London
    Posts
    1,132
    Location
    Well it's not possible to open a file without saving somewhere first. When you "open" an attachment directly from an email, Outlook will save the file to a temp directory first and open it from there.
    It's easy enough for you to do the same - I would recommend using the user's temp folder. You can get this path with Environ("TEMP")

    Then you'll need to open it. If you don't know which type of files you're dealing with, then you'll need to attempt to get the path of the executable that's associated with it in windows for each one.
    This can be done with the winAPI function "FindExecutable"

    All together, it goes something like this:[VBA]'general declarations
    Private Declare Function FindExecutable Lib "shell32.dll" _
    Alias "FindExecutableA" (ByVal lpFile As String, _
    ByVal lpDirectory As String, ByVal lpResult As String) As Long

    'in a procedure...
    Dim strTempFilePath As String
    Dim strExePath As String

    For Each myAttachment In myAttachments
    strTempFilePath = Environ("TEMP") & "\" & strTempFilePath & myAttachment.FileName
    myAttachment.SaveAsFile strTempFilePath
    strExePath = GetExePath(strTempFilePath)
    If strExePath <> "" Then
    Shell strExePath & " " & strTempFilePath, vbMaximizedFocus
    Else
    MsgBox "No file association found!"
    End If
    Next[/VBA]If you want to be diligent, you'll then go and delete all those files you saved. Difficult, since the user may have them open when you decide to do it. Perhaps you should do what everyone else seems to (including M$) and forget about it - at some stage the user's temp folder will get cleaned out... maybe...
    K :-)

  3. #3
    VBAX Regular
    Joined
    May 2005
    Location
    Tallinn
    Posts
    26
    Location
    Thank You very much!

    This was very helpful!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •