Full Version: Add Multiple Attachments To An Outlook Email
UtterAccess Discussion Forums > Microsoft® Access > Access Modules
TimTDP
I have the following code to send emails from Access:
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 rstAttachements 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
                     .Attachment.Add (rstAttachments!EMailAttachment)
                   End With
               End If
               .MoveNext
           Loop
       End With
   End If
      
   With olMail
       .Save
       .Send
   End With
  
   Set olMail = Nothing
   Set olApp = Nothing
  
   MsgBox "Mail Sent!", vbOKOnly, "Mail Sent"


However the code under "Add Attachments" does not work. I get an error in line ".Attachment (rstAttachments!EMailAttachment)"

I write the url of every file to be attached to the email as records in a table called "tblTempSendEMailAttachments".
I want to loop through the table as attach the files to the email

Thanks in advance
arnelgp
If your field EMailAttachment is of Attachment type, you need to open it as recordsource.
TimTDP
It is not of attachment type
It is text, 255 long
Bob G
i dont think it should be using olmail i think it should be olappattach.attachments.add

Bob G
here is a snippet of what i use to send attachments successfully

CODE
Set objOutlook = GetObject(, "Outlook.Application")


   Set objOutlook = CreateObject("Outlook.Application")
   Set objOutlookMsg = objOutlook.CreateItem(olMailItem)
   With objOutlookMsg
   Set objOutlookRecip = .Recipients.Add(somebody@somewhere.com)
                      
          objOutlookRecip.type = olTo
      .Subject = "here you go"
      .Body = "This information is being automatically generated " & vbCrLf _
        & "If you have any issues please contact the sender"
    
      .Importance = olImportanceHigh  'High importance
        Set objOutlookAttach = .Attachments.Add(strpath & "somefilexls")
TimTDP
What do you dimension objOutlookAttach as?
Bob G
i know it looks and sounds wierd. but I dont. i use exactly what i have shown (with real data anyway)
Bob G
i think it is because it is part of the object objoutlook I dont dim the objoutlookrecip either. and I use Option Explicit on the module so if it was "wrong' it wouldnt let me
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.