In my last question (https://bytes.com/topic/access/answers/972172-split-ms-access-reports-into-separate-emails) I wanted to make separated reports from one big report and have them sent matching the EmployeeIDs and Emails... However after every email I get a message from Microsoft Outlook:
"A program is trying to send an e-mail message on your behalf. If this is unexpected, click Deny and verify your antivirus is up-to-date...."
so someone suggested to try coding VBA through Outlook and suggested:
however, I keep receiving "Compile error: Variable not defined" at different locations. This time I have it at
"A program is trying to send an e-mail message on your behalf. If this is unexpected, click Deny and verify your antivirus is up-to-date...."
so someone suggested to try coding VBA through Outlook and suggested:
Code:
Option Explicit Sub test() Dim rsAccountNumber As DAO.Recordset Dim olApp As Outlook.Application, olEmail As Outlook.MailItem Dim fileName As String, todayDate As String, strEmail As String todayDate = Format(Date, "YYYY-MM-DD") Set rsAccountNumber = CurrentDb.OpenRecordset("SELECT DISTINCT EmployeeID, [Email] FROM [queAutoUpdate]", dbOpenSnapshot) Set olApp = New Outlook.Application With rsAccountNumber Do Until .EOF ' SETTING FILE NAME TO SAME PATH AS DATABASE (ADJUST AS NEEDED) fileName = Application.CurrentProject.Path & "\Desktop\trial.accdb" & !EmployeeID & "_" & todayDate & ".pdf" ' OPEN AND EXPORT PDF TO FILE DoCmd.OpenReport "test", acViewPreview, "EmployeeID = '" & !EmployeeID & "'" ' INTENTIONALLY LEAVE REPORT NAME BLANK FOR ABOVE FILTERED REPORT DoCmd.OutputTo acReport, , acFormatPDF, fileName, False DoCmd.Close acReport, "test" ' CREATE EMAIL OBJECT strEmail = ![Email] Set olEmail = olApp.CreateItem(olMailItem) With olEmail .Recipients.Add strEmail .Subject = "Updated Balance" .Body = "Text Here" .Attachments.Add fileName ' ATTACH PDF REPORT .Send ' SEND WITHOUT DISPLAY TO SCREEN End With Set olEmail = Nothing .MoveNext Loop .Close End With End Sub
Code:
Set rsAccountNumber = CurrentDb.OpenRecordset
Comment