Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Calling MapiSendMail in VBA 1

Status
Not open for further replies.

snowmore

MIS
May 23, 2003
2
0
0
US
Within Microsoft Access, I am trying to call the default e-mail client, create a message and attach a file. The user will interactively address the message, then send it.

I have code that works for Outlook, but I need to make it generic to work with any e-mail client.

I believe the solution is going to involve MapiSendMail. I have found some examples of C code, but haven't found VBA code.

Anyone got this?
 
And you can't translate C to VBA ?

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
This one is from a VB6 project and I guess it will not need much if any translation.

Function DoMAPIemail(Subject$, MessageText$, AttPaths$(), Optional Recipient$ = "") As Boolean
'requires a Project reference into MSMAPI32.OCX

Dim i%
Dim OrigDir$, Msg$

Dim MAPISess As MAPISession, MAPIMess As MAPIMessages
Set MAPISess = New MSMAPI.MAPISession
Set MAPIMess = New MSMAPI.MAPIMessages

OrigDir$ = CurDir$
With MAPISess
' XP may need to have a profile set so...
'.LogonUI = False causes problem under XP
.LogonUI = True
.DownLoadMail = False
On Error Resume Next
.SignOn 'changes CurDir$ to MAPI
If Err Then
MsgBox "Error " & Err & " " & Err.Description & " during MAPI Session SignOn"
GoTo exitMapiEmail
End If
On Error GoTo 0
End With

With MAPIMess
.SessionID = MAPISess.SessionID
.Compose
If Len(Recipient$) Then
.RecipAddress = Recipient$
.RecipDisplayName = Recipient$
End If
.MsgSubject = Subject$
.MsgNoteText = MessageText$
For i = 1 To UBound(AttPaths$)
If Len(AttPaths$(i)) Then
.AttachmentIndex = i - 1
.AttachmentPathName = AttPaths$(i)
.AttachmentPosition = i + 1
End If
Next
'by now the mapi controls have set curdir to that of MAPI
ChDrive OrigDir$
ChDir OrigDir$
On Error Resume Next
.Send True 'CurDir$ changes to MAPI again
'Error 32001 returned if user cancels send from Outlook
If Err.Number > 0 And Err.Number <> 32001 Then
Msg$ = "Error " & Err.Number & " " & Err.Description & ", sending email" & vbCrLf
If Err = 32003 Then
Msg$ = Msg$ & vbCrLf & "Please start your email program (e.g. Outlook) manually and try again"
End If
MsgBox Msg$, vbCritical
GoTo exitMapiEmail
End If
On Error GoTo 0
End With

DoEvents

MAPISess.SignOff
DoMAPIemail = True

exitMapiEmail:
Set MAPIMess = Nothing
Set MAPISess = Nothing
On Error GoTo 0
ChDrive OrigDir$
ChDir OrigDir$

End Function
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top