I have the code written and I feel like it works well (I am a beginner so any suggestions to improve the code would be greatly appreciated). I am working on a project that will send emails using the information found in a query. Now that I have the code working so it will actually create the correct email with all of the intended attachments and etc. I need to add a piece of code to the end to keep track of the emails sent in another table. Here is the code that I am using...
---------
Option Compare Database
Public Function SendMail()
Dim db As DAO.Database
Dim MailList As DAO.Recordset
Dim MyOutlook As Outlook.Application
Dim MyMail As Outlook.MailItem
Dim Subjectline As String
Dim BodyFile As String
Dim fso As FileSystemObject
Dim MyBody As TextStream
Dim MyBodyText As String
Dim Invoice As String
Dim A1013R As String
Dim A2016R As String
Dim Azip As String
Dim FolderName As String
Set db = CurrentDb()
Set MailList = db.OpenRecordset("CycleDistribution")
Set fso = New FileSystemObject
Set MyOutlook = New Outlook.Application
MyInput = InputBox("Please enter the Billing Range", _
"Biller", "Input the period range in MM/DD/YYYY - MM/DD/YYYY format")
If MyInput = "Input the period range in MM/DD/YYYY - MM/DD/YYYY format" Then
MsgBox "You have entered an incorrect Range. Please try again!"
End If
MsgBox "Emails for the " & MyInput & " billing period will now be sent!"
Set Fld = CreateObject("Shell.Application").BrowseForFolder(0, "Select Back-Up Folder", 512)
If Not Fld Is Nothing Then
FolderName = Fld.Self.Path
If Right(FolderName, 1) <> "\" Then
FolderName = FolderName & "\"
End If
End If
Do Until MailList.EOF
Set MyMail = MyOutlook.CreateItem(olMailItem)
MyMail.To = MailList("email") & ";" & MailList("EmailAdd2") & ";" & MailList("EmailAdd3") & ";" & MailList("EmailAdd4") & ";" & MailList("EmailAdd5") & ";" & MailList("EmailAdd6") & ";" & MailList("EmailAdd7")
Invoice$ = FolderName & MailList("InvoiceFN")
A1013R$ = FolderName & MailList("1013RFN")
A2016R$ = FolderName & MailList("2016RFN")
Azip$ = FolderName & MailList("ZipFN")
Subjectline$ = MailList("Subjectln") & " - " & MailList("ClientName")
If Subjectline$ = "" Then
MsgBox "No subject line, no message." & vbNewLine & vbNewLine & _
"Quitting...", vbCritical, "E-Mail Merger"
Exit Function
End If
If MailList("EmailBody") = "Zip-File" Then
MyBodyText$ = "Attached please find a password protected zip file for the period " & MyInput & ". "
End If
MyMail.Subject = Subjectline$
MyMail.Body = MyBodyText$
MyMail.Attachments.Add Invoice$, olByValue, 1, ""
If Not MailList("1013RFN") = "" Then
MyMail.Attachments.Add A1013R$, olByValue, 1, ""
End If
If Not MailList("2016RFN") = "" Then
MyMail.Attachments.Add A2016R$, olByValue, 1, ""
End If
If Not MailList("ZipFN") = "" Then
MyMail.Attachments.Add Azip$, olByValue, 1, ""
End If
'MyMail.Send
MyMail.Display
MailList.MoveNext
Loop
Set MyMail = Nothing
Set MyOutlook = Nothing
MailList.Close
Set MailList = Nothing
db.Close
Set db = Nothing
End Function
--------
I appreciate any and all help!
Thank you