473,394 Members | 1,843 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Send email with attachments using VBA

22
I have been working in copy and paste for years and would love to automate the bi-monthly invoice summary e-mails that I send to my customers!

My Access 2013 database creates summaries for customers exported to an excel spreadsheet.
With the help of Ron De Bruin code, I was able to have excel split the worksheet into separate workbooks.

Back in Access, I have a parameter query that will pull a list of these customers and the path and file to their summary, like this
STID CustID ACCT APCEmail STMTAP STDATE STMTPATH
30 740 999999 hmc@123.com TRUE 05-Jan-19 C:\User\Summaries\EMAIL\999999.xlsx

I'm not sure where to begin!!
Do I start with DAO to set database and recordset, then loop through with MoveFirst & DoUntil, build the e-mail name, subject and message with string and SendObject?
Jan 5 '19 #1
2 4596
Nauticalgent
100 64KB
I do something similar to what it is you're trying to do. Here is the code I use. Hopefully you can adapt it to your needs.

Expand|Select|Wrap|Line Numbers
  1.  
  2. Private Sub SendEmailPass()
  3.  
  4.     Dim db As DAO.Database
  5.     Dim rs As DAO.Recordset
  6.     Dim rs1 As DAO.Recordset
  7.     Dim qdf As DAO.QueryDef
  8.     Dim strTo As String
  9.     Dim strCC As String
  10.     Dim strSubject As String
  11.     Dim strBody As String
  12.     Dim strMaintActy As String
  13.     Dim strShopCode As String
  14.     Dim strShipName As String
  15.     Dim intPassCode As Integer
  16.     Dim strPassCode As String
  17.     Dim intWC As Integer
  18.     Dim outApp As Outlook.Application
  19.     Dim outMail As Outlook.MailItem
  20.     Dim intPE As Integer
  21.     Dim intCSPE As Integer
  22.  
  23.     On Error Resume Next
  24.     Set outApp = GetObject(, "Outlook.Application")
  25.     On Error GoTo 0
  26.  
  27.     If outApp Is Nothing Then
  28.         Set outApp = CreateObject("Outlook.Application")
  29.     End If
  30.  
  31.     intWC = Forms![frmJobPasses]![frmAssignments].Form![AssignActy]
  32.     intPassCode = Nz(Me![frmAssignments].Form![AssignPassCode], 0)
  33.     intPE = Nz(Forms![frmJobPasses]![sfrmUnitInfo].Form![cboPE], 0)
  34.     intCSPE = Nz(Forms![frmJobPasses]![sfrmUnitInfo].Form![cboCSPE], 0)
  35.     strPassCode = Nz(DLookup("PassDescrip", "tblPassCodes", "PassCodeID = " & intPassCode))
  36.     strShopCode = Nz(DLookup("ShopCode", "tblMaintActyUnits", "UnitID = " & intWC))
  37.     strMaintActy = Nz(DLookup("Activity", "tblMaintActyUnits", "UnitID = " & intWC))
  38.     strShipName = Nz(DLookup("ShipFullName", "qryShipName", "UnitID = " & Me.UnitID))
  39.  
  40.     Set db = CurrentDb
  41.     Set qdf = db.QueryDefs("qrySelectEmail")
  42.     Set rs1 = db.OpenRecordset("qrySelectCC")
  43.  
  44.     strTo = vbNullString
  45.     With qdf
  46.         .Parameters("intUnitID") = intWC
  47.         Set rs = .OpenRecordset
  48.         rs.MoveFirst
  49.         Do Until rs.EOF
  50.             strTo = strTo & rs.Fields(0) & ";" & vbCrLf
  51.             rs.MoveNext
  52.         Loop
  53.     End With
  54.     strTo = strTo & GetPE(intPE, intCSPE)
  55.  
  56.     With rs1
  57.         strCC = vbNullString
  58.         rs1.MoveFirst
  59.         Do Until rs1.EOF
  60.             strCC = strCC & .Fields(0) & ";" & vbCrLf
  61.             rs1.MoveNext
  62.         Loop
  63.         strCC = strCC & ";" & Me.UnitPOC_Email
  64.     End With
  65.  
  66.  
  67.     strSubject = "** EMAIL PASS ** FROM FDRMC TO " & strMaintActy & " - " & strShopCode _
  68.                 & " FOR " & strShipName & " / " & Me.JobEquip & " / " & Replace(Me.JobCat, "C", "CAT") _
  69.                 & " / CASREP: " & Me.JobNum & " / DTG: " & Me.JobDTG
  70.  
  71.     strBody = strMaintActy & ": Please reply to all and confirm acceptance of support and assigned technician," _
  72.             & " and include the FDRMC Naples CDO <mailto:YourEmail@YourDomain.com > on all related email traffic." & vbCrLf _
  73.             & vbCrLf _
  74.             & "1. Equipment: " & Me.JobEquip & vbCrLf _
  75.             & "2. For detailed information see DTG: " & Me.JobDTG & vbCrLf _
  76.             & "3. Job Number: " & Me.JCN & vbCrLf _
  77.             & "4. Requested Action: " & Me.JobAction & vbCrLf _
  78.             & "5. Reason for pass: " & strPassCode & vbCrLf _
  79.             & "6. Ship's POC information: " & Me.UnitPOC & " / " & Me.UnitPOC_Email & vbCrLf _
  80.             & "7. Request CC <YourEmail@YourDomain.com> on all correspondence for tracking purposes."
  81.     strBody = strBody & vbCrLf _
  82.             & vbCrLf _
  83.             & "Very Respectfully,"
  84.  
  85.  
  86.     Set outMail = outApp.CreateItem(olMailItem)
  87.     With outMail
  88.         .To = strTo
  89.         .CC = strCC
  90.         .Subject = strSubject
  91.         .Body = strBody
  92.         .Display
  93.  
  94.     End With
  95.  
  96. exit_handler:
  97.     rs.Close
  98.     rs1.Close
  99.     Set rs = Nothing
  100.     Set db = Nothing
  101.     Set rs = Nothing
  102.     Set outMail = Nothing
  103.     Set outApp = Nothing
  104.  
  105. End Sub
  106.  
Jan 5 '19 #2
twinnyfo
3,653 Expert Mod 2GB
If you are using Outlook, as very helpful article is here.

Yes, the basics are this:

Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2. Option Explicit
  3.  
  4. Private Sub SendEmail_Click()
  5. On Error GoTo EH
  6.     Dim db              As DAO.Database
  7.     Dim rst             As DAO.Recordset
  8.     Dim strSendTo       As String
  9.     Dim strSubject      As String
  10.     Dim strEMailBody    As String
  11.     Dim strCCLine       As String
  12.     Dim strBCCLine      As String
  13.     Dim strOnBehalfOf   As String
  14.     Dim strAtchs        As String
  15.  
  16.     Set db = CurrentDb()
  17.     Set rst = db.OpenRecordset("YourQuery")
  18.     With rst
  19.         If Not (.BOF And .EOF) Then
  20.             Call .MoveFirst
  21.             Do While Not .EOF
  22.                 strSendTo = !EmailAddress
  23.                 '...
  24.                 'build the rest of your e-mail
  25.                 '...
  26.  
  27.                 'Generate and Display the E-Mail
  28.                 Call SendAnEMail(olSendTo:=strSendTo, _
  29.                                  olSubject:=strSubject, _
  30.                                  olEMailBody:=strEMailBody, _
  31.                                  olDisplay:=True, _
  32.                                  olCCLine:=strCCLine, _
  33.                                  olBCCLine:=strBCCLine, _
  34.                                  olOnBehalfOf:=strOnBehalfOf, _
  35.                                  olAtchs:=strAtchs, _
  36.                                  SendAsHTML:=False)
  37.  
  38.                 Call .MoveNext
  39.             Loop
  40.         End If
  41.         Call .Close
  42.     End With
  43.     db.Close
  44.     Set rst = Nothing
  45.     Set db = Nothing
  46.  
  47. EH:
  48.     Set rst = Nothing
  49.     Set db = Nothing
  50.     Call MsgBox(Prompt:="There was an Error", _
  51.                 Buttons:=vbOKOnly, _
  52.                 Title:="Call teh DBA!")
  53. End Sub
Hope this hepps!
Jan 7 '19 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: dave | last post by:
hi guys I m trying to execute few lines code tht i have copies from microsoft tech script centre. basically its to send email without using any smtp service. u can find the code snippet from...
1
by: Just D. | last post by:
All, What is the simplest way to send an email message through the MS SQL Server? There was a method to send email messages using a standard C# way via SMTP server, but the required port was...
6
by: ErwinF | last post by:
Hi there, I would like to know the following: How to send send email attachments using WebDAV in VB .NET? Sample code please................... Thanks for your help.
0
by: Atenza | last post by:
Hi all, I develop an VB6 program to send email by using CDOSYS.DLL. It works fine as follow: Dim iMsg As New CDO.Message Dim iDsrc As CDO.IDataSource Set iDsrc = iMsg ' (QueryInterface)...
2
by: tulasikumar | last post by:
hi all, i have faced with one problem regarding multiple attachments sending through System.Net.Mail in Asp.net2.0.Any one Please tell me the solution for this.Any one have solution for this...
8
Atran
by: Atran | last post by:
Hello Everybody, I want to send a message to somebody email using C# I have the right code but when I run the application it shown the "ArgumentExecption was unhandled" error, it is been yellow, it...
3
by: phill86 | last post by:
Hi I am using the following code to send an email from access via outlook automatically DoCmd.SendObject acSendReport, stDocName, acFormatRTF, "User", , , , , False
5
by: mulamootil | last post by:
Hi - Is it possible to send email attachments to an Access Database. I know that we can collect data using email messages. It would be nice if there was a way to send an email with an attachment and...
68
by: Jeffrey Tan | last post by:
Hi. I was wondering if there was a way to send an email without using the Send Object method for a macro. if you use the macro, you will get this warning:...
2
by: vffg5555555555 | last post by:
how to send email attachments with encryption
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.