Solved

email PDF from currently open form

Posted on 2014-04-22
10
523 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
  • 2
10 Comments
 
LVL 38

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 38

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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
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 38

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

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
Phishing attempts can come in all forms, shapes and sizes. No matter how familiar you think you are with them, always remember to take extra precaution when opening an email with attachments or links.
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.

717 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