CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    May 2001
    Posts
    9

    e-mail attachments

    does anyone know, how to open an attachment? (from an e-mail)
    thanks!


  2. #2
    Join Date
    May 2001
    Posts
    5

    Re: e-mail attachments

    Assuming that you are using VB with the Outlook Object Model, the only methods I know of are DELETE and SAVEASFILE for attachments:
    ie.:
    Outlook.olMailItem.Attachments(0).SaveAsFile folder&"\"&...Attachments(0).FileName

    You could save it and then find a way in VB to open the file as the next line of your program

    Hope this is of some use



  3. #3
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    Red face If someone could improve...

    I am looking for a way to read attachments. Found this solution,
    but it leaves you with temporary files...

    Anyone could improve or suggest a better way?
    Code:
    Option Explicit
    'put a reference to
    'Microsoft Outlook (highest number) Object Library
    '(this example has been developed with 9.0 (=outlook 2000))
    Const SEE_MASK_INVOKEIDLIST = &HC
    Const SEE_MASK_NOCLOSEPROCESS = &H40
    Const SEE_MASK_FLAG_NO_UI = &H400
    Private Type SHELLEXECUTEINFO
        cbSize As Long
        fMask As Long
        hwnd As Long
        lpVerb As String
        lpFile As String
        lpParameters As String
        lpDirectory As String
        nShow As Long
        hInstApp As Long
        lpIDList As Long
        lpClass As String
        hkeyClass As Long
        dwHotKey As Long
        hIcon As Long
        hProcess As Long
    End Type
    
    Private Declare Function ShellExecuteEx Lib "shell32.dll" (SEI As SHELLEXECUTEINFO) As Long
    Private Declare Function GetTempPath Lib "kernel32" Alias "GetTempPathA" (ByVal nBufferLength As Long, ByVal lpBuffer As String) As Long
    '
    '
    Private Sub Command1_Click()
    Dim OutApp As Outlook.Application
    Dim OutNS As Outlook.NameSpace
    Dim OutFolder As Outlook.MAPIFolder
    Dim outMail As Outlook.MailItem
    Dim outVariant As Variant
    Dim FileName As String
    
    On Error GoTo ErrClick
    'instancing outlook
    Set OutApp = New Outlook.Application
    'instancing namespace
    Set OutNS = OutApp.GetNamespace("MAPI")
    'goto inbox
    Set OutFolder = OutNS.GetDefaultFolder(olFolderInbox)
    'serach for first nmail with  attachment
    For Each outVariant In OutFolder.Items
        'if it is a mailitem
        If TypeOf outVariant Is Outlook.MailItem Then
            
            Set outMail = outVariant
            'count attachments
            If outMail.Attachments.Count > 0 Then
                'found a mail with one or more attachments
                'save first attachment
                '(nb: index starts from 1!)
                FileName = outMail.Attachments.Item(1).FileName
                Dim strTemp As String
                
                'save on disK
                
                strTemp = String(100, Chr$(0))
                'Get the temporary path
                GetTempPath 100, strTemp
                'strip the rest of the buffer
                strTemp = Left$(strTemp, InStr(strTemp, Chr$(0)) - 1)
                'add a dummy letter in name,
                'as the tryue name may be in use by outlook
                'in the temp dir
                If Dir(strTemp & "a" & FileName) = "" Then
                    outMail.Attachments.Item(1).SaveAsFile strTemp & "a" & FileName
                    Call OpenAttachment(strTemp & "a" & FileName)
                    
                End If
                
                'was just an experiment. Close all, and exit searching mails
                'with attachments
                outMail.Close olDiscard
                Exit For
            End If
        End If
    Next outVariant
    'Cleaning:
    
    ExitClick:
    Set outMail = Nothing
    Set outVariant = Nothing
    Set OutFolder = Nothing
    Set OutNS = Nothing
    If Not OutApp Is Nothing Then
        OutApp.Quit
    End If
    Set OutApp = Nothing
    Exit Sub
    
    ErrClick:
        MsgBox Err.Description
        Resume ExitClick
        
    End Sub
    '
    '
    Private Sub OpenAttachment(FileName As String)
    Dim SEI As SHELLEXECUTEINFO
    
        Dim r As Long
        With SEI
            'Set the structure's size
            .cbSize = Len(SEI)
            'Seet the mask
            '.fMask = SEE_MASK_NOCLOSEPROCESS Or _
            ' SEE_MASK_INVOKEIDLIST Or SEE_MASK_FLAG_NO_UI
            'Set the owner window
            .hwnd = Me.hwnd ' OwnerhWnd
            'open
            .lpVerb = "Open"
            'Set the filename
            .lpFile = FileName
            .lpParameters = vbNullChar
            .lpDirectory = vbNullChar
            .nShow = 0
            .hInstApp = 0
            .lpIDList = 0
        End With
        'open associated program for that attachment
        r = ShellExecuteEx(SEI)
    End Sub
    Last edited by Cimperiali; July 3rd, 2002 at 12:28 PM.
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured