?
Solved

MAPI and attachments

Posted on 2002-06-11
8
Medium Priority
?
514 Views
Last Modified: 2007-11-27
Hi,

I've written a VB6 program that creates a compressed and encrypted ascii file that needs to be e-mailed out.  The program is running on a PC with Windows NT Workstation and Outlook98.

Basically, on the first attempt to send the email (with the option to view msg first) I get an error message saying "Could not open one or more attachments".  The email is displayed, but has no attachment.  Strangely enough, if you manually attach the file and send it, when the e-mail arrives at its destination there are two copies of the file attached.

If I then restart the program and attempt to generate another e-mail it all works fine, no error messages or anything.

Any help would be gratefully received!

Thanks,

Julian
0
Comment
Question by:JulianDavies
[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
  • 3
8 Comments
 
LVL 6

Expert Comment

by:simonbennett
ID: 7070125
Can we see the code?

Thanks
0
 

Author Comment

by:JulianDavies
ID: 7070240
This is the routine I'm using....

Private Sub GenerateEmail(Optional strSingleFile As String)

Dim strFile As String
Dim strText As String

' Generate a new e-mail
    mapiMail.Compose

' Get the address
    If mblnRSM = True Then
        ' Use the address from the relevant tab
        mapiMail.RecipAddress = mstrEmailRcpt
        'mapiMail.RecipDisplayName = mstrEmailRcpt
        strFile = strSingleFile
    Else
        ' Use the RSM's own address
        mapiMail.RecipAddress = mobjRSMs(mintSelectedRSM).Email
        'mapiMail.RecipDisplayName = mobjRSMs(mintSelectedRSM).Name
        strFile = mobjRSMs(mintSelectedRSM).ClientFile
    End If

' Add subject line
    mapiMail.MsgSubject = mobjActions(mintSelectedAction).Subject

' Add body text
    strText = " " & vbCrLf & vbCrLf & mobjActions(mintSelectedAction).Subject & vbCrLf & vbCrLf
    If txtEmailText.Text <> "" Then
         strText = strText & "Information: " & txtEmailText.Text
    End If
    mapiMail.MsgNoteText = strText
   
' Add file attachment
    mapiMail.AttachmentIndex = mapiMail.AttachmentCount
    mapiMail.AttachmentType = 0
    mapiMail.AttachmentPathName = strFile
    mapiMail.AttachmentName = ""
    mapiMail.AttachmentPosition = 0    
    mapiMail.send True
   
End Sub
0
 
LVL 18

Expert Comment

by:mdougan
ID: 7070298
You might need to reset the AttachmentCount if you are re-using this same mapiMail object.  I've normally created a New mapiMail object, composed it, attached files and sent it out, and then set the mapiMail object = Nothing.  This ensures that the attachmentcount etc always gets set back to the default values for each new composition.

Also, I remember something funny about setting the AttachmentIndex also increments the AttachmentCount, so, put a breakpoint on that line and verify that the count is what you think it should be and that the attachmentindex is what you think it should be each time.........
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:JulianDavies
ID: 7070496
Will try it, but not sure if that will help... it's only on first use that the problem occurs!

Setting the object to nothing could be a problem as it's a control on the form, rather than just an object instantiated in code.
0
 
LVL 18

Expert Comment

by:mdougan
ID: 7070538
You have a point.  I've been working mostly with object variables.  Keep in mind that you don't need to have a physical control on the form.  You can keep the Project Component checked (which references the object library) and then just declare a variable as:

Dim x as MAPI.Session

Set x = New MAPI.Session

(you'd have to double check the actual class names, but the auto-complete should give you the right objects to work with).

Then, you could work with the object as I described.

It's likely that the AttachmentIndex needs to be a 1 based index (so, the first attachment has an index of 1), and the first time through, your AttachmentCount = 0.  So, it has a problem.  However, setting the AttachmentIndex (maybe even when setting it incorrectly) may be bumping up the attachmentcount to 1, which will then succeed the next time.  However, if the attachmentcount keeps incrementing you'll start getting weird results, like the double-attachments.  So, you either need to remove all attachments (maybe by setting the attachmentcount = 0 each time through) or, set the mapimail object = nothing
0
 

Author Comment

by:JulianDavies
ID: 7077595
Sorry for the delay, but I've now tried your suggestions and the incidences of the problem went down, but didn't totally go away.  Interestingly (?) I've done trials with Outlook Express instead of Outlook and the problem doesn't occur!  Not sure if that is significant or not, but I'd still like to get it working with Outlook98.
0
 
LVL 18

Accepted Solution

by:
mdougan earned 900 total points
ID: 7078189
I experimented with attachments using the mapi objects that I'd suggested, and came up with this code that seems to work pretty reliably.  It probably wouldn't take much effort to change this sub to mirror the functionality of your existing sub.  I'd suggest you try it, keeping your original code there to fall back on if it doesn't work for you.  I believe that it's using the CDO library, though because I'm using CreateObject, I don't have to reference it in the application.  You may need to do something else with the session object's logon, but it should be similar to whatever you're doing now:

Public Sub EventNotice()
Dim objSession As Object
Dim objMessage As Object
Dim objRecipient As Object
Dim objAttachment As Object
    On Error GoTo ErrorRtn

    'Create the Session Object
    Set objSession = CreateObject("mapi.session")
   
    objSession.Logon profileName:="Outlook"
                       
    'Add a new message object to the OutBox
    Set objMessage = objSession.Outbox.Messages.Add
    objMessage.Subject = "Test with Attachments"
    objMessage.Text = "Test with Attachment Bodytext"
   
    Set objRecipient = objMessage.Recipients.Add
    objRecipient.Address = "SMTP:mike.dougan@csfb.com"
    objRecipient.Type = 1
    objRecipient.Resolve
   
    Set objAttachment = objMessage.Attachments.Add
    objAttachment.Type = 1
    objAttachment.Position = 0
    objAttachment.Name = "c:\test1.jpg"
    objAttachment.ReadFromFile "c:\test1.jpg"
   
    Set objAttachment = objMessage.Attachments.Add
    objAttachment.Type = 1
    objAttachment.Position = 1
    objAttachment.Name = "c:\test2.jpg"
    objAttachment.ReadFromFile "c:\test2.jpg"
           
    objMessage.Update
    objMessage.send showDialog:=False
    objSession.Logoff
     
ExitRtn:
    Set objSession = Nothing
    Set objMessage = Nothing
    Set objRecipient = Nothing
    Set objAttachment = Nothing
   
    Exit Sub
ErrorRtn:
    MsgBox Err.Description
    GoTo ExitRtn

End Sub
0
 

Author Comment

by:JulianDavies
ID: 7086347
Thanks for your help.... that's working fine.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying 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

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month8 days, 22 hours left to enroll

765 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