send multiple attachements in email (1 Viewer)

kobiashi

Registered User.
Local time
Today, 09:10
Joined
May 11, 2018
Messages
258
hi

im trying to create a sub to be able to email multiple attachments in the same email, i cant use send object as it will only allow one attachment, i want to be able to send a report and a query as an excel format, how can i do this without having to save it on the computer first?

the code below wants to save it to the desktop first

Code:
Private Sub EmailReport_Click()
Dim oApp As New Outlook.Application
Dim oEmail As Outlook.MailItem
Dim fileName As string, todayDate As String    

'Export report in same folder as db with date stamp
todayDate = Format(Date, "MMDDYYYY")

fileName1 = Application.CurrentProject.Path & "\ReportName_" & todayDate & ".pdf"
DoCmd.OutputTo acReport, "Report_ENG_MCU_InServiceIssues", acFormatPDF, fileName, False

fileName2 = Application.CurrentProject.Path & "\QueryName_" & todayDate & ".pdf"
DoCmd.OutputTo acQuery, "Query_ENG_MCU_ForRS", acFormatPDF, fileName, False

'Email the results of the report generated
Set oEmail = oApp.CreateItem(olMailItem)
With oEmail
    .Display
    .Recipients.Add ""
    .Subject = "In Service Issues"
    .Body = "Hi All" & CHR$(13) & CHR&(13) & "Here is the in service issues for today"
    .Attachments.Add fileName1
    .Attachments.Add fileName2
    '.Send        
End With

MsgBox "Email successfully sent!", vbInformation, "EMAIL STATUS"
 

Ranman256

Well-known member
Local time
Today, 04:10
Joined
Apr 9, 2015
Messages
4,337
put the all the files into a collection, then cycle thru them to attach...

Code:
Public Sub testEmail()
Dim vTo, vSubj, vBody
Dim colFiles As New Collection

vTo = "WileE@acme.com"
vSubj = "test multi attachs"
vBody = "Dear Wiley"

colFiles.Add "c:\folder\file1.txt"
colFiles.Add "c:\folder2\file2.xls"
'etc

Call Send1Email(vTo, vSubj, vBody, colFiles)

Set colFiles = Nothing
End Sub

send the email the email...
Code:
Public Function Send1Email(ByVal pvTo, ByVal pvSubj, ByVal pvBody, Optional pcolFiles As Collection) As Boolean
Dim oApp As Outlook.Application
Dim oMail As Outlook.MailItem
dim vFile

On Error GoTo ErrMail

Set oApp = CreateObject("Outlook.Application")
Set oMail = oApp.CreateItem(olMailItem)

With oMail
    .To = pvTo
    .Subject = pvSubj
    .Body = pvBody

    If Not IsEmpty(pcolFiles) Then
        For Each vFile In pcolFiles
             .Attachments.Add vFile, olByValue, 1
        Next
    End If
    
   .Send
End With
Send1Email = True

endit:
Set oMail = Nothing
Set oApp = Nothing
Exit Function

ErrMail:
MsgBox Err.Description, vbCritical, Err
Resume endit
Resume Next
End Function
 

kobiashi

Registered User.
Local time
Today, 09:10
Joined
May 11, 2018
Messages
258
does that use the data from the database, or is that files from the computer, i noticed the file path for the files are computer file paths?
 

Cronk

Registered User.
Local time
Today, 18:10
Joined
Jul 4, 2013
Messages
2,772
The attachment has to be a saved file. The full path and name of that file can be hard coded, saved in an access table or generated in vba.


I avoid hard coding. For attachments which might be used multiple times, I store the file path/name in a table and let the user change the name and/or location.


In the processing of payments, if receipt document is to be emailed, I generate and save the document, then change the name to customize it with say, the receipt number or account number or name of the payer. The file name might or might not be stored in a table depending on user requirements.
 

Ranman256

Well-known member
Local time
Today, 04:10
Joined
Apr 9, 2015
Messages
4,337
you can change it for whatever....
files in folders, datasets, etc.
 

Users who are viewing this thread

Top Bottom