?
Solved

Problems sending email with attachments from MAPI

Posted on 2003-03-17
8
Medium Priority
?
351 Views
Last Modified: 2010-05-01
I am using the code below to log on and then send email with attachments. The problem I am having is that this code works fine on my Windows2000 laptop which I use for development, but, as soon as I try this on one of the network machines (running Windows 98 or NT) it seems to work but never brings the mail message on screen, further investigation found that during the compose message routine at the point of sending an error occurs and in the immediate window I am told that the attachment is not found, even though the attachment name is listed with the debug.print statement within the compose message procedure where I am adding an attachment. This all works perfectly on my development laptop that is not connected to the network. The one thing I have noticed is that on the development laptop when a session is opened it is Outlook Express that is opened, Outlook Express is not available on the network machines. Everything up to the send method appears to work fine.: -

At the form level: -

'***************************************************************************************
'* Add the attachment
'***************************************************************************************
Private Sub cmdAddAttachment_Click()
On Error GoTo ErrAddAttachment
Static i As Integer                       ' Hold the array element and retain its position
   
   If bSent = True Then                   ' Mail already sent
      i = 0                               ' Reset the static variable to zero
      bSent = False                       ' Set the flag that determines if the email has been sent
      ReDim sAttachmentName(iNumAttachments + 1) ' Re-dimension the array
   Else
      iNumAttachments = i                    ' Set the module level variable to the static amount
      ReDim Preserve sAttachmentName(iNumAttachments + 1) ' Re-dimension the array whilst preserving what it already contains
   End If
   
   sAttachmentName(i) = sFullFileName
   Debug.Print sAttachmentName(i)
   i = i + 1                              ' Increment the array element
   
   ' Enable the send button
   cmdSend.Enabled = True
   
   Exit Sub
   
ErrAddAttachment:
   MsgBox "Error No: " & Err.Number & vbCrLf & Err.Description & vbCrLf & "in the Add Attachment Click Event", vbCritical, ERRTITLE
   
End Sub

'***************************************************************************************
'* Send the selected photograph(s) to an Outlook recipient
'***************************************************************************************
Private Sub cmdSend_Click()
On Error GoTo ErrSend
   
   If fLogOn = True Then         ' Use the logon function
      If iNumAttachments = 0 Then sAttachmentName(0) = sFullFileName
      Call ComposeMessage("frmAdministrationIMP", iNumAttachments)
   End If
   
   ' If a session exists then log off the session, so that a new session can be commenced
   If (mapSess.SessionID) Then
      LogOff (Me.Name)
   End If
   
   Exit Sub
   
ErrSend:
   MsgBox "Error No: " & Err.Number & vbCrLf & Err.Description & vbCrLf & "in the Send Click Event", vbCritical, ERRTITLE
   
End Sub

Module Level: -
'***************************************************************************************
'* Check if a mail session is open and if not open one
'***************************************************************************************
Public Function fLogOn() As Boolean
On Error GoTo errLog
   
   If fLogOn = True Then fLogOn = False
   ' Check if a session is already opened and if so exit
   If frmAdministrationIMP.mapSess.NewSession Then
      MsgBox "Session already established"
      Exit Function
   End If
   
   With frmAdministrationIMP.mapSess
      .DownLoadMail = False             ' Prevent mail from downloading
      .LogonUI = True                   ' Use the underlying email system's logon UI.
      .SignOn                           ' Signon
      fLogOn = True                     ' If successful return True
      .NewSession = True                ' Set NewSession to True
      bNewSession = .NewSession         ' Set variable flag to true
      frmAdministrationIMP.mapMess.SessionID = .SessionID
   End With
   
   Exit Function
   
errLog:
   If Err.Number = 32003 Then
      MsgBox "The mail session was cancelled.", vbOKOnly + vbCritical, "Mail Session Cancelled"
      fLogOn = False
   Else
      MsgBox "Error No: " & Err.Number & vbCrLf & Err.Description & vbCrLf & "in the fLogOn Function", vbCritical, ERRTITLE
   End If
   
End Function

'***************************************************************************************
'* Compose the message
'***************************************************************************************
Public Sub ComposeMessage(sForm As String, iNumAttachments As Integer)
On Error GoTo ErrCompose
Dim i As Integer
Dim iAttachments As Integer
Dim sRecipient As String            ' Name of the mail recipient
Dim sTitle As String                ' The title of the email
Dim sInput As String                ' Used to hold the input entered by the user  into
   
   iAttachments = iNumAttachments
     
   ' Get the user to enter the recipients name and title of the email
   sRecipient = InputBox("Enter the name of the mail recipient.", "Recipient Name")
   If StrPtr(sRecipient) <> 0 Then
      sRecipient = "Enter/Select Recipient"  ' Set a feault value otherwise an error occurs
   Else
      Exit Sub
   End If
     
   sTitle = InputBox("Enter the title of the mail.", "Mail Title", "Photographs Attached")
   
   Select Case sForm
      Case "frmAdministrationIMP"
         With frmAdministrationIMP
            ' Set up the message
            .mapMess.Compose
            .mapMess.RecipAddress = sRecipient
            .mapMess.MsgSubject = sTitle
            .mapMess.MsgNoteText = "Please find attached the photographs you requested"
            ' Add all the attachments
            While sAttachmentName(i) <> ""
               .mapMess.AttachmentIndex = .mapMess.AttachmentIndex + 1
               .mapMess.AttachmentPathName = .mapMess.AttachmentPathName & sAttachmentName(i)
                Debug.Print sAttachmentName(i)
               i = i + 1
            Wend
            .mapSess.NewSession = False
            .mapMess.Send True
         End With
      Case "frmSearchResultsIMP"

   End Select
   
   bSent = True
   Exit Sub

   
ErrCompose:
   Debug.Print Err.Description
   Resume Next



Help with this is much appreciated.

LiMa
0
Comment
Question by:LiMa
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
  • 2
8 Comments
 
LVL 4

Expert Comment

by:barnesd1
ID: 8151388
Are you sure the object library you are using (.olb file) works with the outlook version in the office?  Try referencing a version in "c:\program files\microsoft office\office", eg msoutl8.olb for Outlook 97.
0
 

Author Comment

by:LiMa
ID: 8151605
No, I didn't think you needed the outlook object library when using the MAPI controls. But, just in case I have tried adding this in, and creating a new setup (during the package and deployment wizard I am informed there are no reference files for the the outlook object lib). Unfortunately this still does not make any differance. I can get the above code to work perfectly with a single attachment by modifying part of the code in the compose message  procedure to: -

With frmAdministrationIMP
   .mapMess.Compose
   .mapMess.MsgSubject = sTitle
   .mapMess.MsgNoteText = "Please find attached the photographs you requested"
   .mapMess.AttachmentPathName = sAttachmentName(i)
   .mapSess.NewSession = False
   .mapMess.Send True
End With

This works fine but if I use the other code to add more attachments, I get the attachmenmt not found error. Even though I can use a debug.print statement and the correct attachment name is displayed.

LiMa
0
 
LVL 4

Expert Comment

by:barnesd1
ID: 8151690
Public Function AddAttachment(strName As String, atAttachmentType As Variant, strSource As String, strReadFromFile As String)


Dim MapiAttachment As Object
Set MapiAttachment = MAPIMessage.Attachments.Add

With MapiAttachment
    .Name = strName
    .Type = mapiFileData
    .Source = strSource
    .ReadFromFile FileName:=strReadFromFile
End With
   
End Function

Use example
oMail.AddAttachment "Word", 0, sFilename, sFilename
This works for me!
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 2

Expert Comment

by:CD-Softy
ID: 8155196
0
 

Author Comment

by:LiMa
ID: 8158205
I can't seem to get anything to work from either of the last two comments. It has to be something to do with trying to slot this code into my project.

Does anyone have an example of this running? Nothing elaborate just something that demonstrates how to add multiple attachments to the email. This is driving me insane I seem to be repeating the same process over and over again when trying to get this to work.

Help before I am driven totally insane.

LiMa
0
 

Author Comment

by:LiMa
ID: 8158237
Forgot to add my email address (just in case any of you experts do have a small demo): - lima_b1@excite.com

Thanx

LiMa
0
 
LVL 2

Accepted Solution

by:
CD-Softy earned 720 total points
ID: 8160992
for a MAPI code generator, see
http://www.vbtoys.com/downloads.asp and download Visual MAPI 99 - good source code generator for VB.
0
 

Author Comment

by:LiMa
ID: 8179768
That's excellent thanks a lot. Thanks also to barnesd1 for your comments.

I think the main problem I had was that the MAPI messaging doesn't seem to be very well documented in the way that it should be used. The MAPI code generator helped me make sense of it all (well to some degree).

Thanx again.

LiMa
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses
Course of the Month10 days, 16 hours left to enroll

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question