Add Multiple Attachments To An Outlook Email (1 Viewer)

TimTDP

Registered User.
Local time
Today, 03:54
Joined
Oct 24, 2008
Messages
210
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
  
    '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
                    Set objOutlookAttach = .Attachments.Add(rstAttachments!EMailAttachment)
                End If
                .MoveNext
            Loop
        End With
    End If
    
    .Save
    .Send
End With

Set olMail = Nothing
Set objOutlookAttach = 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 "Set objOutlookAttach = .Attachments.Add(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
 

pbaldy

Wino Moderator
Staff member
Local time
Yesterday, 17:54
Joined
Aug 30, 2003
Messages
36,126
Well, your With block is referring to the recordset, so inappropriate for the attachment. Plus you have the structure wrong, I believe. Try this before the loop:

Set objOutlookAttach = olMail.Attachments

and this inside the loop:

objOutlookAttach.Add rstAttachments!EMailAttachment
 

TimTDP

Registered User.
Local time
Today, 03:54
Joined
Oct 24, 2008
Messages
210
Hi Paul

Can you explain further? I have tried your suggestion without success

Thank you
 

pbaldy

Wino Moderator
Staff member
Local time
Yesterday, 17:54
Joined
Aug 30, 2003
Messages
36,126
What does your code look like now?
 

TimTDP

Registered User.
Local time
Today, 03:54
Joined
Oct 24, 2008
Messages
210
My code looks like this:
Code:
Dim olApp As Outlook.Application
Dim olMail As Outlook.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

    '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
            Do Until .EOF
                If DoesFileExist(rstAttachments!EMailAttachment) Then
                    olMail.Attachments.Add (rstAttachments!EMailAttachment)
                End If
                .MoveNext
            Loop
        End With
    End If

    .Save
    .Send
End With

Set olMail = Nothing
Set objOutlookAttach = Nothing
Set olApp = Nothing
Set rstAttachments = Nothing
Set db = Nothing

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

IT WORKS!!!!!

As you suggested, replaced:
Set objOutlookAttach = .Attachments.Add(rstAttachments!EMailAttachment)
with
olMail.Attachments.Add (rstAttachments!EMailAttachment)

Thanks very much
 

pbaldy

Wino Moderator
Staff member
Local time
Yesterday, 17:54
Joined
Aug 30, 2003
Messages
36,126
Glad you got it working.
 

konecny.tomas

New member
Local time
Yesterday, 17:54
Joined
Oct 12, 2012
Messages
9
Hi.

My intention is to attach pdf file saved in folder "W:\POST\Invoice\" based on the Invoice number generated from the db. Pdf file name starts with the invoice number and continues with various text of various lenght.

With the code below I get the runtime error: "file not found".

Any suggestions?

Thank you.

Code:
Dim db As DAO.Database
Dim rst As DAO.Recordset
Set db = CurrentDb
Dim rstattach As DAO.Recordset
Dim Path As String
Dim File As String

Path = "W:\POST\Invoice\"

Set rstattach = db.OpenRecordset(strSql, dbOpenDynaset)


Do While Not rstattach.EOF
strRST1 = strRST1 & Path & rstattach.Fields("[Invoice]")
rstattach.MoveNext
Loop

File = Dir(strRST1 & "*")



With olNewEmail 'Attach template
.to = strContactEmail
.display
.Subject = strEmailSubject

.htmlbody = strEmailText & olNewEmail.htmlbody & strline


.attachments.Add File


End With
Code:
 

pbaldy

Wino Moderator
Staff member
Local time
Yesterday, 17:54
Joined
Aug 30, 2003
Messages
36,126
I don't see strSql being set anywhere.
 

konecny.tomas

New member
Local time
Yesterday, 17:54
Joined
Oct 12, 2012
Messages
9
Sql below.

Code:
strSql = "SELECT TBL_DodacieListy.Faktura" & _
" FROM TBL_DodacieListy" & _
" WHERE TBL_DodacieListy.ID_kup = " & Forms!F_Faktura!Fkup.Value & " AND TBL_DodacieListy.FakturaOdoslana = TRUE" & _
" AND Day(TBL_DodacieListy.FakturaOdoslanaDna) = " & Forms!F_Faktura!Den.Value & " AND Month(TBL_DodacieListy.FakturaOdoslanaDna) = " & Forms!F_Faktura!Mesiac.Value & " AND Year(TBL_DodacieListy.FakturaOdoslanaDna) = " & Forms!F_Faktura!Rok.Value
 

konecny.tomas

New member
Local time
Yesterday, 17:54
Joined
Oct 12, 2012
Messages
9
Paul, thank you very much, I know this technique from your previous posts (very helpful), but I applied it incorrectly.

Now (with corrections) it attaches only one attachment, but I want to attach all of files based on the recordset.

With
Code:
strRST1 = strRST1 & Path & rstattach.Fields("[Faktura]")
I get error: "Bad file name".

Below upgrated procedure, attaching only one file:
Code:
Dim db As DAO.Database
Set db = CurrentDb
Dim rstattach As DAO.Recordset
Dim Path As String
Dim File As String
Path = "W:\POSTA\E_faktury\NA_ODOSLANIE\"
Set rstattach = db.OpenRecordset(strSql, dbOpenDynaset)


strSql = "SELECT TBL_DodacieListy.Faktura" & _
" FROM TBL_DodacieListy" & _
" WHERE TBL_DodacieListy.ID_kup = " & Forms!F_Faktura!Fkup.Value & " AND TBL_DodacieListy.FakturaOdoslana = TRUE" & _
" AND Day(TBL_DodacieListy.FakturaOdoslanaDna) = " & Forms!F_Faktura!Den.Value & " AND Month(TBL_DodacieListy.FakturaOdoslanaDna) = " & Forms!F_Faktura!Mesiac.Value & " AND Year(TBL_DodacieListy.FakturaOdoslanaDna) = " & Forms!F_Faktura!Rok.Value



Do While Not rstattach.EOF
strRST1 = Path & rstattach.Fields("[Faktura]")
rstattach.MoveNext
Loop
     
File = Dir(strRST1 & "*")

Debug.Print strSql
Debug.Print strRST1
Debug.Print File

 
   With olNewEmail   'Attach template
      .to = strContactEmail
  '    .CC = strCc
      .display
      .Subject = strEmailSubject
      
      .htmlbody = strEmailText & olNewEmail.htmlbody & strline
      
      .attachments.Add Path & File
            
   End With

Thank you.
 

pbaldy

Wino Moderator
Staff member
Local time
Yesterday, 17:54
Joined
Aug 30, 2003
Messages
36,126
You loop through a recordset and change the variable with each pass. Then later, you use the variable. If you think about it logically, you're only going to get the last item in the loop attached. You want that loop down where the attachment is added, and you want that attachment line inside the loop, so it attaches each file.
 

pbaldy

Wino Moderator
Staff member
Local time
Yesterday, 17:54
Joined
Aug 30, 2003
Messages
36,126
Hard to tell what you're doing wrong without seeing the code. ;)
 

pbaldy

Wino Moderator
Staff member
Local time
Yesterday, 17:54
Joined
Aug 30, 2003
Messages
36,126
Happy to help!
 

skankadon

New member
Local time
Yesterday, 17:54
Joined
Nov 20, 2013
Messages
4
help! code for multiple reports and files to email from a record
i have come to the limit of my abilities and i hope one of you guys/gals can help

i might be asking a bit much but i need the code i will pay or donate if neccesary

i have a database which has new customers added daily (currently we post all documentation)

when a new record is filled in the customer receives:

cover letter/invoice(report), book page(report) and a book (pdf, always the same) the reports are created from the record

rather than post all these documents i would like to email them to each customer in 1 email with the reports in pdf format.

Example:

CC (email feild from the record)

Subject (pre filled with "Stop Bullying Now! Books 4 Schools order")

Body (pre filled with "Dear ([Contact]if possible) Please find enclosed your documentation for your recent telephone order bla bla bla and our logo.jpg if possible)

then i would like to attach to the email the cover letter/invoice(report), book page(report) and the book (pdf) from the hard drive (r.eports changed to pdf)

if i can press a button on the form to send it that would be good.
if i could link it to a query to send a batch of them that would excellent.

The Database is Named: b4s.mdb
The table is called: Main Table
The table fields are Contact and email
The input form is called: Client
The letter/invoice report is called: b4sletinv
The Book page report: b4sbook
The pdf book is located at c:\Documents and Settings\Administrator\b4sfinal.pdf
The query for the batch is called: coverlet&proforma

your help is much a appreciated if you can help there is noway i can do this on my own.
 

Users who are viewing this thread

Top Bottom