XL5: Sending Mail from MS Excel Using VBAMAPI.DLL (Part 1)

ID: Q125854

The information in this article applies to:

SUMMARY

In Microsoft Excel, you can use the SendMail method in a Visual Basic macro for basic electronic mail (E-mail) functionality. However, for more complete control of mail, including message content, subject, return receipt, and recipients, you must use the VBAMAPI.DLL file, which currently ships with Microsoft Project version 4.0 for Windows (this file is located on Setup Disk 2).

The following file(s) are available for download from the Microsoft Software Library:

 ~ VBAMAPI.EXE (size: 69050 bytes) 

For more information about downloading files from the Microsoft Software Library, please see the following article in the Microsoft Knowledge Base:

   ARTICLE-ID: Q119591
   TITLE     : How to Obtain Microsoft Support Files from Online Services

Note that if you use VBAMAPI.DLL with Microsoft Excel, use the built-in MailLogon and MailLogoff methods.

Once you've downloaded the file, copy it to your Windows SYSTEM directory.

The information in the "More Information" section of this article contains Declare functions and data type definitions necessary to use VBAMAPI.DLL.

For more information about using MAPI with Microsoft Excel for Windows, please see the following articles:

   ARTICLE-ID:Q125853
   TITLE     :XL: Sending Mail from MS Excel using VBAMAPI.DLL (Part 2)

   ARTICLE-ID: Q123185
   TITLE     : GP Fault Using MAPI.DLL Calls in Visual Basic Procedure

MORE INFORMATION

Microsoft provides examples of Visual Basic for Applications procedures for illustration only, without warranty either expressed or implied, including, but not limited to the implied warranties of merchantability and/or fitness for a particular purpose. The Visual Basic procedures in this article are provided 'as is' and Microsoft does not guarantee that they can be used in all situations. While Microsoft support professionals can help explain the functionality of a particular macro, they will not modify these examples to provide added functionality, nor will they help you construct macros to meet your specific needs. If you have limited programming experience, you may want to consult one of the Microsoft Solution Providers. Solution Providers offer a wide range of fee-based services, including creating custom macros. For more information about Microsoft Solution Providers, call Microsoft Customer Information Service at (800) 426-9400.

'***************************
'   FUNCTION Declarations
'***************************
'For best results please enter each of the DeclareFunction
'on a separate line

Declare Function MAPILogon Lib "MAPI.DLL" (ByVal UIParam&, ByVal
User$, ByVal Password$, ByVal Flags&, ByVal Reserved&, Session&) As Long

Declare Function MAPILogoff Lib "MAPI.DLL" (ByVal Session&, ByVal
UIParam&, ByVal Flags&, ByVal Reserved&) As Long
Declare Function MAPIDetails Lib "MAPI.DLL" (ByVal Session&, ByVal
UIParam&, Recipient As MapiRecip, ByVal Flags&, ByVal Reserved&) As Long

Declare Function MAPIResolveName Lib "VBAMAPI.DLL" Alias
"BMAPIResolveName" (ByVal Session&, ByVal UIParam&, ByVal UserName$, ByVal Flags&, ByVal Reserved&, Recipient As MapiRecip) As Long
Declare Function MAPISendDocuments Lib "MAPI.DLL" (ByVal UIParam&,
ByVal DelimStr$, ByVal FilePaths$, ByVal FileNames$, ByVal Reserved&) As Long

Declare Function MAPIFindNext Lib "VBAMAPI.DLL" Alias "BMAPIFindNext"
(ByVal Session&, ByVal UIParam&, MsgType$, SeedMsgID$, ByVal Flag&, ByVal Reserved&, MsgID$) As Long
Declare Function MAPIDeleteMail Lib "MAPI.DLL" (ByVal Session&, ByVal

UIParam&, ByVal MsgID$, ByVal Flags&, ByVal Reserved&) As Long
Declare Function MAPIAddress Lib "VBAMAPI.DLL" Alias "BMAPIAddress"
(ByVal Session&, ByVal UIParam&, Caption$, ByVal EditFields&, Label$, RecipCount&, Recipients() As MapiRecip, ByVal Flags&, ByVal Reserved&) As Long

Declare Function MAPISaveMail Lib "VBAMAPI.DLL" Alias "BMAPISaveMail"
(ByVal Session&, ByVal UIParam&, Message As MapiMessage, Recipient As MapiRecip, File As MapiFile, ByVal Flags&, ByVal Reserved&, MsgID$) As Long

Declare Function MAPISendMail Lib "VBAMAPI.DLL" Alias "BMAPISendMail"
(ByVal Session&, ByVal UIParam&, Message As MapiMessage, Recipient As MapiRecip, File As MapiFile, ByVal Flags&, ByVal Reserved&) As Long

Declare Function MAPIReadMail Lib "VBAMAPI.DLL" Alias "BMAPIReadMail"
(ByVal Session&, ByVal UIParam&, ByVal MsgID$, ByVal Flags&, ByVal Reserved&, Message As MapiMessage, Originator As MapiRecip, Recips() As MapiRecip, Files() As MapiFile) As Long

Declare Function GetProfileInt Lib "KERNEL" (ByVal lpAppName As
String, ByVal lpKeyName As String, ByVal nDefault As Integer) As Integer

'***************************************************
'   MAPI Message holds information about a message
'***************************************************

Type MapiMessage
    Reserved As Long
    Subject As String
    NoteText As String
    MessageType As String
    DateReceived As String
    ConversationID As String
    Flags As Long
    RecipCount As Long
    FileCount As Long
End Type

'************************************************
'   MAPIRecip holds information about a message
'   originator or recipient
'************************************************

Type MapiRecip
    Reserved As Long
    RecipClass As Long
    Name As String
    Address As String
    EIDSize As Long
    EntryID As String
End Type

'******************************************************
'   MapiFile holds information about file attachments
'******************************************************
Type MapiFile
    Reserved As Long
    Flags As Long
    Position As Long
    PathName As String
    FileName As String
    FileType As String
End Type

'**************************
'   CONSTANT Declarations
'**************************
'
Global Const SUCCESS_SUCCESS = 0 Global Const MAPI_USER_ABORT = 1 Global Const MAPI_E_FAILURE = 2 Global Const MAPI_E_LOGIN_FAILURE = 3 Global Const MAPI_E_DISK_FULL = 4 Global Const MAPI_E_INSUFFICIENT_MEMORY = 5 Global Const MAPI_E_BLK_TOO_SMALL = 6 Global Const MAPI_E_TOO_MANY_SESSIONS = 8 Global Const MAPI_E_TOO_MANY_FILES = 9 Global Const MAPI_E_TOO_MANY_RECIPIENTS = 10 Global Const MAPI_E_ATTACHMENT_NOT_FOUND = 11 Global Const MAPI_E_ATTACHMENT_OPEN_FAILURE = 12 Global Const MAPI_E_ATTACHMENT_WRITE_FAILURE = 13 Global Const MAPI_E_UNKNOWN_RECIPIENT = 14 Global Const MAPI_E_BAD_RECIPTYPE = 15 Global Const MAPI_E_NO_MESSAGES = 16 Global Const MAPI_E_INVALID_MESSAGE = 17 Global Const MAPI_E_TEXT_TOO_LARGE = 18 Global Const MAPI_E_INVALID_SESSION = 19 Global Const MAPI_E_TYPE_NOT_SUPPORTED = 20 Global Const MAPI_E_AMBIGUOUS_RECIPIENT = 21 Global Const MAPI_E_MESSAGE_IN_USE = 22 Global Const MAPI_E_NETWORK_FAILURE = 23 Global Const MAPI_E_INVALID_EDITFIELDS = 24 Global Const MAPI_E_INVALID_RECIPS = 25 Global Const MAPI_E_NOT_SUPPORTED = 26 Global Const MAPI_ORIG = 0 Global Const MAPI_TO = 1 Global Const MAPI_CC = 2 Global Const MAPI_BCC = 3 Global Const MAPI_UNREAD = 1 Global Const MAPI_RECEIPT_REQUESTED = 2 Global Const MAPI_SENT = 4

'***********************
'   FLAG Declarations
'***********************
Global Const MAPI_LOGON_UI = &h1 Global Const MAPI_NEW_SESSION = &h2 Global Const MAPI_DIALOG = &h8 Global Const MAPI_UNREAD_ONLY = &h20 Global Const MAPI_ENVELOPE_ONLY = &h40 Global Const MAPI_PEEK = &h80 Global Const MAPI_GUARANTEE_FIFO = &h100 Global Const MAPI_BODY_AS_FILE = &h200 Global Const MAPI_AB_NOMODIFY = &h400 Global Const MAPI_SUPPRESS_ATTACH = &h800 Global Const MAPI_FORCE_DOWNLOAD = &h1000 Global Const MAPI_OLE = &h1 Global Const MAPI_OLE_STATIC = &h2

Additional query words: 5.00 5.00a 5.00c

Keywords          : kbinterop kbprg 
Version           : 5.00 5.00c
Platform          : WINDOWS

Last Reviewed: May 17, 1999