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

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
56,729
Office Version
  1. 365
Platform
  1. Windows
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
 
Solution

Atiqul Haque

New Member
Joined
Dec 13, 2020
Messages
45
Office Version
  1. 2016
Platform
  1. Windows
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.
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
56,729
Office Version
  1. 365
Platform
  1. Windows
You're welcome & thanks for the feedback.
 

Atiqul Haque

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

ADVERTISEMENT

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.
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
56,729
Office Version
  1. 365
Platform
  1. Windows
Just change OpenAfterPublish: to False in-stead of True
 

Atiqul Haque

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

ADVERTISEMENT

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.
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
56,729
Office Version
  1. 365
Platform
  1. Windows
Do you want to print the file, or save it as a pdf?
 

Atiqul Haque

New Member
Joined
Dec 13, 2020
Messages
45
Office Version
  1. 2016
Platform
  1. Windows
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.
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
56,729
Office Version
  1. 365
Platform
  1. Windows
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.
 

Watch MrExcel Video

Forum statistics

Threads
1,129,931
Messages
5,639,064
Members
417,067
Latest member
rohitbabshet

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
Top