VBA to Print a specific page to printer or pdf (1 Viewer)

sxschech

Registered User.
Local time
Today, 08:35
Joined
Mar 2, 2010
Messages
793
Found an almost workable vba solution to the issue of printing a specific page and saving it as a pdf and thought I'd share it.

Searched through many threads and sites and combined a few commands from each. The nearly fully automated solution used SendKeys which I commented out and left them in as comments, in case the trade-off is worthwhile. In that case, comment out the CopytoClip and uncomment the SendKeys section. The main problem I encountered from SendKeys was that it turned the Num Lock off on the numeric keypad and since I use the numeric keypad quite often, didn't want any interference that SendKeys caused. As a work around, used the Sub CopyToClip to allow pasting the filename into the saveas dialog that pops up with docmd.PrintOut. With CopytoClip, have to manually issue a paste command once the popup displays.

Code can be modified to allow user to specify print quality and number of copies to print, however, left that off.

If not planning to save to pdf and only sending directly to a printer, then can use the smaller Sub PrintPage which does not need the additional subs and functions to handle saving and naming files.

NOTE: The code is intended for situations where the more common solutions do not work: such as getting a report to fit on a page or printing a specific record which can usually be taken care of by adjusting the layout, adjusting the scale percentage to fit or using a filter to select the record or records to print.

If there is no need for PDF, then this code is all that is needed, otherwise download the attached the code instead.
Code:
Sub PrintPage(repName As String, startpage As Integer, endpage As Integer)
'Demo for those who don't need to save as PDF and
'are only going to send directly to printer. This
'code doesn't need the other subs and functions
'to operate
'Export Report to Printer and select which page(s) to print
'20230720
'https://codekabinett.com/rdumps.php?Lang=2&targetDoc=export-filtered-access-report-pdf-docmd-outputto
'https://bytes.com/topic/access/answers/625233-printing-printer-pdf-file
'https://bytes.com/topic/access/answers/879916-create-pdf-report-using-cutepdf-means-vba
'20230721
'***
'NOTE: DoCmd.PrintOut has additional parameters for Print Quality and Number of Copies
'      which are not included in this demo
'***
    DoCmd.OpenReport repName, acViewPreview, , , acWindowNormal
    DoCmd.PrintOut acPages, startpage, endpage
    DoCmd.Close acReport, repName, acSaveNo
End Sub


Due to length of text, had to put the code that can handle both PDF and Direct to Printer as an attachment as it needs to include additional subs.
 

Attachments

  • ExportPage.txt
    5.3 KB · Views: 105

June7

AWF VIP
Local time
Today, 07:35
Joined
Mar 9, 2014
Messages
5,472
Probably should post in Code Repository forum.
 

GinnyR

New member
Local time
Today, 16:35
Joined
Apr 27, 2023
Messages
24
Found an almost workable vba solution to the issue of printing a specific page and saving it as a pdf and thought I'd share it.

Searched through many threads and sites and combined a few commands from each. The nearly fully automated solution used SendKeys which I commented out and left them in as comments, in case the trade-off is worthwhile. In that case, comment out the CopytoClip and uncomment the SendKeys section. The main problem I encountered from SendKeys was that it turned the Num Lock off on the numeric keypad and since I use the numeric keypad quite often, didn't want any interference that SendKeys caused. As a work around, used the Sub CopyToClip to allow pasting the filename into the saveas dialog that pops up with docmd.PrintOut. With CopytoClip, have to manually issue a paste command once the popup displays.

Code can be modified to allow user to specify print quality and number of copies to print, however, left that off.

If not planning to save to pdf and only sending directly to a printer, then can use the smaller Sub PrintPage which does not need the additional subs and functions to handle saving and naming files.

NOTE: The code is intended for situations where the more common solutions do not work: such as getting a report to fit on a page or printing a specific record which can usually be taken care of by adjusting the layout, adjusting the scale percentage to fit or using a filter to select the record or records to print.

If there is no need for PDF, then this code is all that is needed, otherwise download the attached the code instead.
Code:
Sub PrintPage(repName As String, startpage As Integer, endpage As Integer)
'Demo for those who don't need to save as PDF and
'are only going to send directly to printer. This
'code doesn't need the other subs and functions
'to operate
'Export Report to Printer and select which page(s) to print
'20230720
'https://codekabinett.com/rdumps.php?Lang=2&targetDoc=export-filtered-access-report-pdf-docmd-outputto
'https://bytes.com/topic/access/answers/625233-printing-printer-pdf-file
'https://bytes.com/topic/access/answers/879916-create-pdf-report-using-cutepdf-means-vba
'20230721
'***
'NOTE: DoCmd.PrintOut has additional parameters for Print Quality and Number of Copies
'      which are not included in this demo
'***
    DoCmd.OpenReport repName, acViewPreview, , , acWindowNormal
    DoCmd.PrintOut acPages, startpage, endpage
    DoCmd.Close acReport, repName, acSaveNo
End Sub


Due to length of text, had to put the code that can handle both PDF and Direct to Printer as an attachment as it needs to include additional subs.
This is a wonderful workaround for my problem with DoCmd.OutputTo. Thanks a million.
 

Users who are viewing this thread

Top Bottom