Generate and send by email individual reports based on a query (1 Viewer)

joaolabisa

New member
Local time
Today, 03:37
Joined
Mar 19, 2014
Messages
5
Hello to All,

I am trying to make an automation in order generate reports based on query ( person name,person email address ) , export them to a folder in PDF and then send them one by one to each person email address.
Please find my code below.

What is happening:
- the reports are generated and exported fine
- the email are sent to the right addresses but the first person receives the correct report , the second person receives the report from the first person plus its own report and so on...

I think some is wrong my structure , I am a VBA beginner ...

Help will be much appreciated !

Best Regards,

Here is my code :


Code:
Private Sub MakeReportSendEmail_Click()


Dim MyDB As DAO.Database
Dim MyRS As DAO.Recordset
Dim strSQL As String
Dim strRptName As String
Dim count As Integer
Dim strFilter As String


Dim imsg As Object 
Dim iconf As Object 
Dim flds As Object
Dim schema As String

Dim strPath As String
Dim strFilterEmail As String
Dim strFile As String


strRptName = "MyReportName"
strSQL = "SELECT * FROM MyQueryNameEmail ORDER BY Name"


Set MyDB = CurrentDb
Set MyRS = MyDB.OpenRecordset(strSQL, dbOpenForwardOnly)


Set imsg = CreateObject("CDO.Message")
Set iconf = CreateObject("CDO.Configuration")
Set flds = iconf.Fields

schema = "...schemas.microsoft.com/cdo/configuration/"
flds.Item(schema & "sendusing") = 2
flds.Item(schema & "smtpserver") = "smtp"
flds.Item(schema & "smtpserverport") = 25
flds.Item(schema & "smtpauthenticate") = 1
flds.Item(schema & "smtpusessl") = True
flds.Item(schema & "smtpconnectiontimeout") = 60
flds.Item(schema & "sendusername") = "xxxx"
flds.Item(schema & "sendpassword") = "xxxx"
flds.Update

With MyRS


Do While Not MyRS.EOF
DoCmd.OpenReport strRptName, acViewPreview, , "[TableWithNames].Name='" & ![Name] & "'"
DoCmd.OutputTo acOutputReport, strDocName, acFormatPDF, "C:\Pdfs\" & ![Name] & ".pdf"
DoCmd.Close acReport, strRptName, acSaveNo

strPath = "C:\Pdfs\"
strFilterEmail = "*.pdf"
strFile = Dir(strPath & strFilterEmail)


With imsg
.to = MyRS.Fields("Email")
.From = "some_email"
.Subject = "Test Subject:"
.HTMLBody = "Test Body"
.AddAttachment strPath & strFile
Set .Configuration = iconf
.Send
End With


.MoveNext
Loop
End With
MyRS.Close
Set MyRS = Nothing
End Sub
 

pbaldy

Wino Moderator
Staff member
Local time
Yesterday, 19:37
Joined
Aug 30, 2003
Messages
36,125
You're using a wildcard with the attachment path. Try the same thing you use in the OutputTo line.
 

joaolabisa

New member
Local time
Today, 03:37
Joined
Mar 19, 2014
Messages
5
Hi, Thanks !

i used this_

Do While Not MyRS.EOF

strPath = "\\FET\Departamento Tecnico e Aplicações\DocsExportadosGestaoGlobal\ras\"
strFile = ![Nome] & ".pdf"

DoCmd.OpenReport strRptName, acViewPreview, , "[1_Tecnicos Master].Nome='" & ![Nome] & "'"
DoCmd.OutputTo acOutputReport, strRptName, acFormatPDF, strPath & strFile
DoCmd.Close acReport, strRptName, acSaveNo

With imsg
.to = MyRS.Fields("Email")
.From = "interno@thl.pt"
.Subject = "Test Subject:"
.HTMLBody = "Test Body"
.AddAttachment strPath & strFile
Set .Configuration = iconf
.Send
End With


exact same results!
 

joaolabisa

New member
Local time
Today, 03:37
Joined
Mar 19, 2014
Messages
5
and if i use

Do While Not MyRS.EOF

DoCmd.OpenReport strRptName, acViewPreview, , "[1_Tecnicos Master].Nome='" & ![Nome] & "'"
DoCmd.OutputTo acOutputReport, strRptName, acFormatPDF, "\\FET\Departamento Tecnico e Aplicações\DocsExportadosGestaoGlobal\ras\" & ![Nome] & ".pdf"
DoCmd.Close acReport, strRptName, acSaveNo

With imsg
.to = MyRS.Fields("Email")
.From = "aaa@bbb"
.Subject = "Test Subject:"
.HTMLBody = "Test Body"
.AddAttachment "\\FET\Departamento Tecnico e Aplicações\DocsExportadosGestaoGlobal\ras\" & ![Nome] & ".pdf"
Set .Configuration = iconf
.Send
End With

.MoveNext
Loop
End With
MyRS.Close

It gives an error on the .AddAttachment line : " Object doesn´t support this property or method"

any ideas ? Thanks!
 

pbaldy

Wino Moderator
Staff member
Local time
Yesterday, 19:37
Joined
Aug 30, 2003
Messages
36,125
Do the files get created correctly? You're saying the second person gets 2 attachments, the third gets 3, etc?
 

pbaldy

Wino Moderator
Staff member
Local time
Yesterday, 19:37
Joined
Aug 30, 2003
Messages
36,125
Regarding the error here:

.AddAttachment "\\FET\Departamento Tecnico e Aplicações\DocsExportadosGestaoGlobal\ras\" & ![Nome] & ".pdf"

It's because ![Nome] is using the imsg With block, not the recordset With block.
 

joaolabisa

New member
Local time
Today, 03:37
Joined
Mar 19, 2014
Messages
5
Yes , the files are created just fine.
and
Yes that´s what is happening , the second person gets 2 different attachments ( from the first person + second person ), the third gets all 3, etc
 

pbaldy

Wino Moderator
Staff member
Local time
Yesterday, 19:37
Joined
Aug 30, 2003
Messages
36,125
Oh, I think you need to clear the imsg variable and create a new one inside the loop. I think it's using the same message over and over that you created here:

Set imsg = CreateObject("CDO.Message")
 

pbaldy

Wino Moderator
Staff member
Local time
Yesterday, 19:37
Joined
Aug 30, 2003
Messages
36,125
Happy to help!
 

Boomkweker

Registered User.
Local time
Yesterday, 19:37
Joined
Apr 1, 2014
Messages
17
Hello joaolabisa,

Good this worked out for you so well. Now this is where i'm really looking for but I think I have some questions. I want to start with this one:

You have the following line:

strSQL = "SELECT * FROM MyQueryNameEmail ORDER BY Name"

Where does the * stands for?

Hope you will help

Thanks in advance,
Jaap
 

Boomkweker

Registered User.
Local time
Yesterday, 19:37
Joined
Apr 1, 2014
Messages
17
Allright Paul,

Thanks for your quick reply. Then another question. I'm still not able to reproduce the next part to my own situation.

DoCmd.OpenReport strRptName, acViewPreview, , "[TableWithNames].Name='" & ![Name] & "'"
DoCmd.OutputTo acOutputReport, strDocName, acFormatPDF, "C:\Pdfs\" & ![Name] & ".pdf"

I have a query which gives back [mailadress]; [order_ID]; [Customer_ID];[Order_month]; [Invoice_ID]

What I want is that this select reports from the report "Invoice" and save them by "Invoice_ID.pdf"

I hope you or somebody else can help me out with this.

Thanks in advance,
Jaap
 

pbaldy

Wino Moderator
Staff member
Local time
Yesterday, 19:37
Joined
Aug 30, 2003
Messages
36,125
What have you tried? What went wrong?
 

Boomkweker

Registered User.
Local time
Yesterday, 19:37
Joined
Apr 1, 2014
Messages
17
Oke Paul this is what I've tried:

Private Sub MakeReportSendEmail_Click()


Dim MyDB As DAO.Database
Dim MyRS As DAO.Recordset
Dim strSQL As String
Dim strRptName As String
Dim count As Integer
Dim strFilter As String


Dim imsg As Object
Dim iconf As Object
Dim flds As Object
Dim schema As String

Dim strPath As String
Dim strFilterEmail As String
Dim strFile As String


strRptName = "DigitalInvoice"
strSQL = "SELECT * FROM DigitaInvoicing ORDER BY Invoice_ID"


Set MyDB = CurrentDb
Set MyRS = MyDB.OpenRecordset(strSQL, dbOpenForwardOnly)


Set imsg = CreateObject("CDO.Message")
Set iconf = CreateObject("CDO.Configuration")
Set flds = iconf.Fields

schema = "...schemas.microsoft.com/cdo/configuration/"
flds.Item(schema & "sendusing") = 2
flds.Item(schema & "smtpserver") = "smtp"
flds.Item(schema & "smtpserverport") = 25
flds.Item(schema & "smtpauthenticate") = 1
flds.Item(schema & "smtpusessl") = True
flds.Item(schema & "smtpconnectiontimeout") = 60
flds.Item(schema & "sendusername") = "xxxxx"
flds.Item(schema & "sendpassword") = "xxxxx"
flds.Update

With MyRS


Do While Not MyRS.EOF
DoCmd.OpenReport strRptName, acViewPreview, , "[VARBOrder].Invoice_ID='" & ![Invoice_ID] & "'"
DoCmd.OutputTo acOutputReport, strDocName, acFormatPDF, "D:\AJ de Lange\Jaaroverzichten\Digitale Facturen\" & ![Invoice_ID] & ".pdf"
DoCmd.Close acReport, strRptName, acSaveNo

strPath = "D:\AJ de Lange\Jaaroverzichten\Digitale Facturen\"
strFilterEmail = "*.pdf"
strFile = Dir(strPath & strFilterEmail)


With imsg
.To = MyRS.Fields("Invoicemailadres")
.From = "some_email"
.Subject = "Test Subject:"
.HTMLBody = "Test Body"
.AddAttachment strPath & strFile
Set .Configuration = iconf
.Send
End With


.MoveNext
Loop
End With
MyRS.Close
Set MyRS = Nothing
End Sub


What I'm trying there is this:
- on the strSQL part I try to select a record set from a query which holds the Invoice_ID, which I want to Use to select the right Invoices to send.
When I start the module it asks me for Invoice_ID and for [VARBOrder].Invoice_ID (so it don't get this from my instructions I guess)

at the end I get a failure -2147220960(80040220) SendUsing is not valid and this highlights the .SEND

Thanks for your help anyway and I'm getting somewhere allready.

Jaap
 

pbaldy

Wino Moderator
Staff member
Local time
Yesterday, 19:37
Joined
Aug 30, 2003
Messages
36,125
That error would appear to related to the email configuration, not the PDF. Does the PDF get created properly? Do you have an actual server specified here in the actual code?

flds.Item(schema & "smtpserver") = "smtp"
 

Boomkweker

Registered User.
Local time
Yesterday, 19:37
Joined
Apr 1, 2014
Messages
17
No I don't have a smtp there, but first I want to ask is this also for access 2007 or just 2010?

The created pdf.1 is a report with 3 pages where I want a 1.pdf, 2.pdf and 3.pdf

But why does de module ask me for Invoice_ID and VARBOrder.Invoice_ID?

Thanks

Jaap
 

Boomkweker

Registered User.
Local time
Yesterday, 19:37
Joined
Apr 1, 2014
Messages
17
Paul

I've tried some more right now. The asking for the Invoice_ID was something in my report which I've changed. The effect from that change is that I get a 3464 error.

What I've asked in my last reply about 2007 or 2010 is related to the Send method for mail is this possible in 2007 or do I have to use SendObject in combination with Outlook?

Thanks
Jaap
 

pbaldy

Wino Moderator
Staff member
Local time
Yesterday, 19:37
Joined
Aug 30, 2003
Messages
36,125
You should be able to use CDO; I have. On the server line noted above I have the IP address of the server in my code.
 

papic1972

Registered User.
Local time
Today, 12:37
Joined
Apr 14, 2004
Messages
122
Hi,

I've tried using something similar to this but with a Ms Access 2013 Frontend and SQL Server 2008 backend. I receive the following error:

Runtime Error 3061

Too few parameters. Expected 1

The code breaks on:

Set MyRS = MyDB.OpenRecordset(strSQL, dbOpenForwardOnly)

Is there something extra that I need to add to this specific line (e.g dbSeeChanges)?
 

Users who are viewing this thread

Top Bottom