Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Attach multiple files to email using Outlook 1

Status
Not open for further replies.

DevelopV

Technical User
Mar 16, 2012
113
ZA
I need to attach files to an email, using Outlook as the email client.
The number of attachments is random.

In Access I write the full path of the file to be attached to the field "EMailAttachment" in table "tblTempSendEMailAttachments" at run time.

I have the following code:
Code:
Dim olApp As Outlook.Application
Dim olMail As MailItem
Dim objOutlookAttach As Outlook.Attachment

Set olApp = New Outlook.Application
Set olMail = olApp.CreateItem(olMailItem)

With olMail
    .BodyFormat = olFormatHTML
    .To = Left(pubMessageRecipientsToAddress, Len(pubMessageRecipientsToAddress) - 1)
    .CC = ""
    .Subject = pubMessageSubject
    .Body = pubMessageBody
End With

    'Add Attachments
    Dim db As DAO.Database
    Dim rstAttachments As DAO.Recordset
        
    Set db = CurrentDb()
    Set rstAttachments = db.OpenRecordset("Select EMailAttachment from tblTempSendEMailAttachments")
    
    If rstAttachments.RecordCount > 0 Then
        With rstAttachments
            .MoveLast
            .MoveFirst
            
            Do Until .EOF
                If DoesFileExist(rstAttachments!EMailAttachment) Then
                    With olMail

                    [COLOR=red]Set objOutlookAttach = .Attachments.Add(rstAttachments!EMailAttachment)[/color]
                    End With
                 End If
                .MoveNext
            Loop
        End With
    End If

With olMail
    .Save
    .Send
End With

Set olMail = Nothing
Set objOutlookAttach = Nothing
Set olApp = Nothing

MsgBox "Mail Sent!", vbOKOnly, "Mail Sent"

The code to create the email works. However when the code: Set objOutlookAttach = .Attachments.Add(rstAttachments!EMailAttachment) runs I get an error 325 -Operation is not supported for this type of object.

What am I doing wrong, and how do I correct it?

Many thanks in advance
 

You don't need objOutlookAttach at all. You don't even need the 'With/End With' if you use
Code:
If DoesFileExist(rstAttachments!EMailAttachment) Then
   olMail.Attachments.Add(rstAttachments!EMailAttachment)
End If

Also, since you are not using rstAttachments.RecordCount you do not need to 'load' the records first so the .movefirst and .movelast statements are not necessary, either. You can just use the 'Do until .EOF' and '.movenext' to cycle through the records.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top