PDA

View Full Version : Access and Outlook Express



AlanG
02-27-2005, 06:48 AM
Hi

I can send emails with or without attachments from Access using MS Outlook no problem, but I have one user who flatly refuses to install Outlook on his machine (don't ask why - it's beyond me!!!).

Consequently I've been trying to be able to use Outlook Express instead (which he's quite happy to use!!!!) and found the following freeware code (credit to the author Lyle Fairfield) which uses the msoe.dll

I've modified the code to suit my needs, but despite several (un)happy hours of trying, can't work out what I'm missing to be able to add an attachment to the email.

I'd be very grateful if anyone can add the arguments needed to pass to the .dll to enable adding an attachment.

Thanks............

Alan



Option Compare Database
Option Explicit

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


Type MAPIFileTag
Reserved As Long
TagLength As Long
Tag() As Byte
EncodingLength As Long
Encoding() As Byte
End Type


Type MAPIFile
Reserved As Long
Flags As Long
Position As Long
PathName As String
FileName As String
FileType As Long
End Type


Type MAPIMessage
Reserved As Long
Subject As String
NoteText As String
MessageType As String
DateReceived As String
ConversationID As String
Originator As Long
Flags As Long
RecipCount As Long
Recipients As Long
Files As Long
FileCount As Long
End Type


Declare Function MAPISendMail _
Lib "c:\program files\outlook express\msoe.dll" ( _
ByVal Session As Long, _
ByVal UIParam As Long, _
message As MAPIMessage, _
ByVal Flags As Long, _
ByVal Reserved As Long) As Long


Sub SendMailWithOE(ByVal strSubject As String, ByVal strMessage As String, _
ByRef aRecips As Variant)
Dim recips() As MAPIRecip
Dim message As MAPIMessage
Dim z As Long
ReDim recips(LBound(aRecips) To UBound(aRecips))
For z = LBound(aRecips) To UBound(aRecips)
With recips(z)
.RecipClass = 1
If InStr(aRecips(z), "@") <> 0 Then
.Address = StrConv(aRecips(z), vbFromUnicode)
Else
.Name = StrConv(aRecips(z), vbFromUnicode)
End If
End With
Next z
With message
.NoteText = strMessage
.Subject = strSubject
.RecipCount = UBound(recips) - LBound(aRecips) + 1
.Recipients = VarPtr(recips(LBound(recips)))
End With

MAPISendMail 0, 0, message, 0, 0

End Sub

Sub TestSendMailwithOE(strSubject As String, strBody As String)

Dim aRecips(0 To 0) As String

aRecips(0) = "someone@somewhere.com"

SendMailWithOE strSubject, strBody, aRecips

End Sub

Private Sub cmdEmail_Click()

Call TestSendMailwithOE("test", "testing message")

End Sub

downwitch
02-28-2005, 05:11 PM
I've wrestled with this one for a long time--thanks for posting that code, it finally got me to an answer.

That code, according to the mvps newsgroup, is buggy--indeed, it crashed Access very nicely for me. The same guy who wrote it posted back to the newsgroup with a bugless version; the thread is here (http://groups-beta.google.com/group/comp.databases.ms-access/browse_thread/thread/c227e7315bab6b49/2a6b1597ff0c71c6), the updated code is here (http://ffdba.com/downloads/Send_Mail_With_Outlook_Express.dat). Don't know why it never occurred to me to search for an answer to that question. More tips and versions on Tony Toe's excellent site (http://www.granite.ab.ca/access/email/mapi.htm).

By the way, I would strongly recommend that you check out Michael Kaplan's SimpleMapi objects (http://www.trigeminal.com/lang/1033/codes.asp?ItemID=24), which can be dropped right in to VB and VBA, and work pretty nicely (though I have had problems on OE-and-Outlook machines with it). Lots of great stuff there, including an example whose api call looks very much like the OE one, that will show you that by substituting &H8 for the second to last 0 in the call you get to see the message on the screen.

AlanG
03-01-2005, 01:31 PM
Hi

Thanks for your very full and complete answer. It's solved a headache for me and works a treat..............

Again, many thanks :)

Alan