'---------------------------------------------------------------------------------------
' This code was originally written by Alex Dybenko
' It is not to be altered or distributed, except as part of an application.
' You are free to use it in any application, provided the copyright notice is left unchanged.
 
' Copyright 1998-2008 Alex Dybenko. All Rights Reserved.
' http://AccessBlog.net
' http://www.PointLtd.com
'---------------------------------------------------------------------------------------
Option Compare Database
Option Explicit
 
Private Declare Function GetModuleHandle Lib "kernel32" Alias "GetModuleHandleA" (ByVal lpModuleName As StringAs Long
Private Declare Function LoadLibrary Lib "kernel32" Alias "LoadLibraryA" (ByVal lpLibFileName As StringAs Long
Private Declare Function GetProcAddress Lib "kernel32" (ByVal hModule As LongByVal lpProcName As StringAs Long
Private Declare Function FreeLibrary Lib "kernel32" (ByVal hLibModule As LongAs Long
 
 
Private Declare Function MAPISendMail Lib "MAPI32.DLL" Alias "BMAPISendMail" _
(ByVal Session As LongByVal UIParam As Long, Message As MAPIMessage, _
Recipient() As MapiRecip, File() As MapiFile, ByVal Flags As Long, _
ByVal Reserved As LongAs Long

Public Function RunMAPISendMail()
    'A sample procedure show how to load dll from user specified path
    Dim hModule As Long
 
    Dim lpProc As Long
    Dim FreeLib As Boolean
    Dim ret As Long
    Dim strDllPath As String
    'A path to dll
    strDllPath = "C:\Program Files\Outlook Express\"
 
   ' check first to see if the module is already
   ' mapped into this process.
    hModule = GetModuleHandle(strDllPath & "MAPI32.DLL")
   If hModule = 0 Then
      ' need to load module into this process.
      hModule = LoadLibrary(strDllPath & "MAPI32.DLL")
      FreeLib = True
   End If
 
   ' if the module is mapped, check procedure
   ' address to verify it's exported.
   If hModule Then
      lpProc = GetProcAddress(hModule, "BMAPISendMail")
      If lpProc <> 0 Then
        returnVal = MAPISendMail()
      End If
      Exported = (lpProc <> 0)
   End If
   If FreeLib Then Call FreeLibrary(hModule)
End Function