Click to See Complete Forum and Search --> : e-mail attachments


tchyper
May 4th, 2001, 04:18 PM
does anyone know, how to open an attachment? (from an e-mail)
thanks!

chris75
May 9th, 2001, 10:44 AM
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

Cimperiali
July 3rd, 2002, 10:52 AM
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?
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