Sending email using MAPI kills Access

  • Thread starter Thread starter GJH
  • Start date Start date
G

GJH

I've looked around on how to send emails with multiple attachements from
access. Came across a great set of VB code that sends emails really well,
but when I try to send Attachements all I get is the error message "
Msaccess has caused an error in MSOE.dll. Msaccess will now close." :-( and
not the most helpful message.

I am guessing that the problem is with the MAPIfile.FileType. Some internet
sources say one thing, other say something different, and everything that I
have tried doesn't work, so perhaps there are two problems here ..

Anyhow, it is past midnite here I will check back tomorrow - any thoughts,
hints, farts, or other bright ideas welcome. Not farts - just checking if
you were paying attection:-)

/Gordon - Full Code below

Option Compare Database
Option Explicit

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

'Type MAPIFileTag ' is this really needed??
' 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 MAPIFileTag
FileType As Long ' internet says this should be a string, a long, or
a structure MAPIFileTag
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 TestSendMailWithAttachmentsUsingOE()

Dim MessageOnly As Boolean ' debuging variable. Works when true, bombs
when false!
MessageOnly = True

Dim Recipient(0 To 0) As String
Recipient(0) = "No>[email protected]<Spam" '<Big Grin>

Dim Attachements(0 To 0) As String
Attachements(0) = "C:\autoexec.bak"

Dim Subject As String
Subject = "Email with Attachment"

Dim MessageBody As String
MessageBody = "But I keep getting errors when I try :-("

' Process the email recipients this will go to
Dim Mrecips(0 To 0) As MAPIRecip
With Mrecips(0)
.RecipClass = 1
.Address = StrConv(Recipient(0), vbFromUnicode)
End With

' process the file list for attachments
Dim Mfiles(0 To 0) As MAPIFile
If Not MessageOnly Then
With Mfiles(0)
.Flags = 0
.Reserved = 0
.Position = -1
.PathName = StrConv(Attachements(0), vbFromUnicode)
.FileName = ""
.FileType = 0

End With
End If

Dim MMessage As MAPIMessage
With MMessage
.NoteText = MessageBody
.Subject = Subject
.RecipCount = 1
.Recipients = VarPtr(Mrecips(0))
If Not MessageOnly Then .FileCount = 1
If Not MessageOnly Then .Files = VarPtr(Mfiles(0))
End With

MAPISendMail 0, 0, MMessage, 0, 0

End Sub
 
I use Outlook for my mail. This is the code I use for
sending multiple attachments. I like this because I can
get all my email information and atachments from an open form

Jim


Function SendEMail()
Dim strTo As String, strSubject As String, _
varBody As Variant, strCC As String, _
strBCC As String, strAttachment As String,
strAttachment1 As String

strTo = "email address"
strSubject = "put subject here"
varBody = "put message for bdy here"
' Add more strattachments if needed and modify IF statement
' below
strAttachment = "attachment1"
strAttachment1 = "attachment2"
'Start Outlook
Dim olApp As Outlook.Application
Set olApp = CreateObject("Outlook.Application")

'Logon
Dim olNs As Outlook.NameSpace
Set olNs = olApp.GetNamespace("MAPI")
olNs.Logon

'Send a message
Dim olMail As Outlook.MailItem
Set olMail = olApp.CreateItem(olMailItem)
'Fill Out and Send Message
olMail.To = strTo
olMail.CC = strCC
olMail.BCC = strBCC
olMail.Subject = strSubject
olMail.Body = varBody
' Modify these statements if more attachmewnts are needed
If Len(strAttachment) <> 0 Then
olMail.Attachments.Add (strAttachment)
If Len(strAttachment1) <> 0 Then
olMail.Attachments.Add (strAttachment1)
End If
End If
olMail.Send

Set olNs = Nothing
Set olMail = Nothing
Set olApp = Nothing

End Function



-----Original Message-----
I've looked around on how to send emails with multiple attachements from
access. Came across a great set of VB code that sends emails really well,
but when I try to send Attachements all I get is the error message "
Msaccess has caused an error in MSOE.dll. Msaccess will now close." :-( and
not the most helpful message.

I am guessing that the problem is with the
MAPIfile.FileType. Some internet
 
Great! . . . err . . . but I get an undefined user type here Dim olApp As
Outlook.Application
What should I be including?

/Gordon
 
I found these instructions:

1) In the module, click on Tools then References
2) Scroll down the list and place a checkmark next to Outlook x.0 Object
Model (for Outlook 98 it is version 8.0, Outlook 2000 is version 9.0,
Outlook 2002/XP is version 10.0)
3) If it wasn't already checked near the top, scroll down a little bit more
and check Microsoft DAO 3.x Object Library
4) Scroll down a little bit more and check Microsoft Scripting Runtime
5) Click OK to close that window.

But at step 2 above I do not have OutLook x.0 Object Model - <Frustration!>

/Gordon
 
Gordon
I am running W/98 and Outlook 2000. I do have the
Microsoft Outlook 9.0 Library checked in my references.
When I installed Office 2000 I check everything for reasons
like this. Perhaps if you reinstall Office and check all
options you might have the additional references available.

Jim
 
Great! It is all working good now! [Wait for it ..... Wait for it .....]
Except [I knew it!] for one interesting thing ...

When Outlook is open (i.e. has an open visible window) it works really well.
When outlook is not open it hangs (Win XP) until the Outlook process is
killed.
I ran it with lots of break points, and it did not hang! (True).

Eventually found out that if I put a 5 second delay before the following
line it all worked well.
Set olMail = Nothing
A one second delay was not enough.

So I am running it with a 5 second delay.

QUESTION: Is this a sleeping dog? [A sleeping dog may wake up and bite you!
So kill it while it sleeps.] How can I make sure it will not need 6 seconds
later?

Cheers Gordon.
Oh BTW the issue with the missing Outlook x.0 Object was a PBKAC, since it
is really called Microsoft Outlook x.0 Object [yes really!:-(]

 
Back
Top