Solved

email PDF from currently open form

Posted on 2014-04-22
10
515 Views
Last Modified: 2014-04-27
I need to output the currently open Access form to a pdf and attach it to an email.

The code below will create a pdf from a named report and attach it to an email:
DoCmd.SendObject acSendReport, [name of report], acFormatPDF, "recipient@company.com",[cc email(s)],[bcc email(s)], [Message Subject], [MessageBody], True

Is it possible to print the currently open access form to a pdf attachment in an email?

Thank you in advance for your help.
0
Comment
Question by:Rwardlow
  • 5
  • 3
  • 2
10 Comments
 
LVL 34

Assisted Solution

by:PatHartman
PatHartman earned 250 total points
ID: 40014891
Reports are not optimized for printing.  In addition to subforms not working to expand to show all records, the backgrounds are too dark and so waste ink.  Create another report.

In order to control the records selected for a report exported as PDF, you need to use parameters in the query.  There is no "where" argument for the SendObject method as there is for the OpenForm and OpenReport methods.
0
 
LVL 1

Author Comment

by:Rwardlow
ID: 40015907
Thank you for your comment Pat, I am aware of the reasons it is not normal to print from forms. In this case we don't have the problems you reference. The forms print fine. However, I inherited this design with a lot of forms and the client would like to email pdf's. I have used VBA to manage PDF's with reports. I was curious if anyone had experience creating and emailing the currently open form.

Thanks again.
0
 
LVL 34

Expert Comment

by:PatHartman
ID: 40016005
You can use the OutputTo to print a form BUT it does NOT offer a where argument so it will print everything.  The ONLY way this will work, as I mentioned earlier,  is if you have used selection criteria in your form to select only the record you are viewing.  I don't believe filters will work.  Every record in the Form's RecordSource will end up in the pdf.
0
 
LVL 1

Author Comment

by:Rwardlow
ID: 40016053
Thank you Pat. I understood. The forms in question only have 1 record in the RecordSource.
0
 
LVL 31

Accepted Solution

by:
Helen_Feddema earned 250 total points
ID: 40018072
Here is some code to create a PDF from a report and email it.  As others have said, it is much better to use a report for this purpose.  Also, Automation code gives you a lot more control than the old SendObject method.

Public Sub ExportReportToPDF()
'Created by Helen Feddema 12-Jul-2013
'Last modified by Helen Feddema 12-Jul-2013

On Error GoTo ErrorHandler

   Dim appOutlook As New Outlook.Application
   Dim strReport As String
   Dim strSavePath As String
   Dim strReportFile As String
   Dim itm As Outlook.MailItem
   Dim strEmail As String
   
   strReport = "rptCurrentData"
   strSavePath = "G:\Documents\PDFs\"
   strEmail = "hfeddema@hvc.rr.com"
   strReportFile = strSavePath & "Current Data.pdf"
   
   'Create PDF file
   DoCmd.OutputTo objecttype:=acOutputReport, _
      objectname:=strReport, _
      outputformat:=acFormatPDF, _
      outputfile:=strReportFile, _
      autostart:=True
   
   'Create email
   strEmail = "Email address"
   Set itm = appOutlook.CreateItem(olMailItem)
   itm.Subject = "Message Subject"
   itm.Body = "Message body"
   itm.To = strEmail
   itm.Attachments.Add Source:=strReportFile, _
      Type:=olByValue
   itm.Display
   
ErrorHandlerExit:
   Set appOutlook = Nothing
   Exit Sub

ErrorHandler:
   MsgBox "Error No: " & Err.Number & "; Description: "
   Resume ErrorHandlerExit

End Sub

Open in new window

0
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 
LVL 1

Author Comment

by:Rwardlow
ID: 40022316
Thank you for this example Helen. However, I am aware of all of the reasons why it is better to use a report for this purpose. I asked this question to see if someone knew how to create a pdf from a form. I have specific reasons. Apparently it is not possible.

Thank you very much for your help.
0
 
LVL 1

Author Closing Comment

by:Rwardlow
ID: 40022320
I assume this is not possible since no one answered my question.

Thank you
0
 
LVL 34

Expert Comment

by:PatHartman
ID: 40022550
You can "print" to a PDF printer or use OutputTo.  But OutputTo ONLY works if the recordset of the form is controlled by criteria in its RecordSource.  Filters will not work.
0
 
LVL 31

Expert Comment

by:Helen_Feddema
ID: 40022572
I tried exporting a form to PDF (there is such an option) in the interface, and while there were no error messages, the PDF was entirely blank.  If I have some time later on, I will experiment with creating a PDF from a form in code, just to see if it is possible.
0
 
LVL 1

Author Comment

by:Rwardlow
ID: 40025677
Thank you Helen, I appreciate your help. I have started using the print dialog in Acrobat manually. It would have been nice to be able to automate the whole process like you can with reports but that doesn't seem to be possible.
Thanks again.
Bob
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In the article entitled Working with Objects – Part 1 (http://www.experts-exchange.com/Microsoft/Development/MS_Access/A_4942-Working-with-Objects-Part-1.html), you learned the basics of working with objects, properties, methods, and events. In Work…
Introduction When developing Access applications, often we need to know whether an object exists.  This article presents a quick and reliable routine to determine if an object exists without that object being opened. If you wanted to inspect/ite…
Familiarize people with the process of utilizing SQL Server views from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Access…
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now