Solved

email PDF from currently open form

Posted on 2014-04-22
10
513 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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
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

Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

Join & Write a Comment

Suggested Solutions

The first two articles in this short series — Using a Criteria Form to Filter Records (http://www.experts-exchange.com/A_6069.html) and Building a Custom Filter (http://www.experts-exchange.com/A_6070.html) — discuss in some detail how a form can be…
Regardless of which version on MS Access you are using, one of the harder data-entry forms to create is one where most data from previous entries needs to be appended to new records, especially when there are numerous fields and records involved.  W…
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

707 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

15 Experts available now in Live!

Get 1:1 Help Now