Prevent printing by excel normal feature & save worksheet in pdf by a command button

Atiqul Haque

New Member
Joined
Dec 13, 2020
Messages
45
Office Version
  1. 2016
Platform
  1. Windows
Dear All,

I want to prevent printing of worksheet by excel normal feature and allow only to save and print the worksheet as pdf by a command button.
Would you pls solve this problem for me. Your help will be highly appreciated.

i use the following code's but unfortunately when i put the prevent print code in workbook module i can't make the workbook saved as pdf.

Code in workbook module:
Private Sub Workbook_BeforePrint(Cancel As Boolean)
MsgBox "Please print via [Print] or [Pdf] button"
Cancel = True
End Sub

Code in Command Button:
Sub createPDF()
Sheet2.Range("A7:M37").ExportAsFixedFormat xlTypePDF, Filename:= _
"C:\Users\User\Desktop\Save File As PDF\" & Range("C9") & "-" & Range("C10") & "_" & Range("I10") & "_" & "Biochemical_Report", _
OpenAfterPublish:=True
End Sub
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
Hi & welcome to MrExcel.
In a standard module put
VBA Code:
Public AllowPrint As Boolean
It must go at the very top of the module, before any code.
Then in the Thisworkbook module use
VBA Code:
Private Sub Workbook_BeforePrint(Cancel As Boolean)
   If AllowPrint Then Exit Sub
   MsgBox "Please print via [Print] or [Pdf] button"
   Cancel = True
End Sub
and your print button
VBA Code:
Sub createPDF()
   AllowPrint = True
   Sheet2.Range("A7:M37").ExportAsFixedFormat xlTypePDF, Filename:= _
      "C:\Users\User\Desktop\Save File As PDF\" & Range("C9") & "-" & Range("C10") & "_" & Range("I10") & "_" & "Biochemical_Report", _
      OpenAfterPublish:=True
   AllowPrint = False
End Sub
 
Upvote 0
Solution
Hi & welcome to MrExcel.
In a standard module put
VBA Code:
Public AllowPrint As Boolean
It must go at the very top of the module, before any code.
Then in the Thisworkbook module use
VBA Code:
Private Sub Workbook_BeforePrint(Cancel As Boolean)
   If AllowPrint Then Exit Sub
   MsgBox "Please print via [Print] or [Pdf] button"
   Cancel = True
End Sub
and your print button
VBA Code:
Sub createPDF()
   AllowPrint = True
   Sheet2.Range("A7:M37").ExportAsFixedFormat xlTypePDF, Filename:= _
      "C:\Users\User\Desktop\Save File As PDF\" & Range("C9") & "-" & Range("C10") & "_" & Range("I10") & "_" & "Biochemical_Report", _
      OpenAfterPublish:=True
   AllowPrint = False
End Sub
Thank you very much sir. Now it's work perfectly.
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0
S
You're welcome & thanks for the feedback.

Sir one more thing,
can i put print command directly to the printer? What i mean that the file will be saved as pdf w/out showing the pdf file but go directly to the printer to make print.

Thanks in advance.
 
Upvote 0
Just change OpenAfterPublish: to False in-stead of True
 
Upvote 0
Just change OpenAfterPublish: to False in-stead of True
Dear Sir thank you very much for your quick reply. To use this code pdf file do not show but not go for printing directly. I need w/out showing the pdf file, file goes to the printer directly to print. Is it possible?
Thank you in advance.
 
Upvote 0
Do you want to print the file, or save it as a pdf?
 
Upvote 0
Do you want to print the file, or save it as a pdf?
Thank you very much for your reply.
Actually i want to print but also wants to save it in pdf format without showing the pdf file with a single command button. Is it possible sir?
Thanks in advance.
 
Upvote 0
You can use the code you already have & then add something to the end to print the sheet.
Just use the macro recorder to record yourself printing the sheet & you will have that code.
 
Upvote 0

Forum statistics

Threads
1,214,643
Messages
6,120,707
Members
448,981
Latest member
recon11bucks

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top