Disable manual printing just only done by macro-vba in excel

sadaqatt

New Member
Joined
Jan 31, 2023
Messages
25
Office Version
  1. 365
  2. 2021
Platform
  1. MacOS
I want to disable print for all users manually, Just print only from the macro assign button (want to use for small office billing invoices)

Sub PostToRegister()
Dim WS1 As worksheet
Dim WS2 As worksheet
Set WS1 = Worksheets("INVOICE")
Set WS2 = Worksheets("DATABASE")
WS2.Unprotect Password:="123"
WS1.Unprotect Password:="123"


' figure out which row is the next row
nextrow = WS2.Cells(Rows.Count, 1).End(xlUp).Row + 1

' write the important values to DATABASE
WS2.Cells(nextrow, 1).Resize(1, 7).Value = Array(WS1.Range("E11"), WS1.Range("E12"), WS1.Range("B11"), _
WS1.Range("B17"), WS1.Range("E25"), WS1.Range("E26"), WS1.Range("E27"))



WS2.Protect Password:="123"
WS1.Protect Password:="123"


End Sub
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
In the ThisWorkbook events, put the following code:

VBA Code:
Option Explicit

Public EnableToPrint As Boolean

Private Sub Workbook_BeforePrint(Cancel As Boolean)
  If EnableToPrint = False Then
    Cancel = True
    MsgBox "To print use the macro button"
  End If
End Sub

Ex:
1675351293456.png



In a module put the following macro:
VBA Code:
Option Explicit

Sub MacroToPrint()
  ThisWorkbook.EnableToPrint = True
  Sheets("INVOICE").PrintOut
  ThisWorkbook.EnableToPrint = False
End Sub

Ex:
1675351357643.png



In the sheet button assign the macro "MacroToPrint".

----- --
 
Upvote 0
Sorry, it showing errors, I am really struggling with this, and i cannot attach the screenshot showing too large,
kindly send me the your email so I can explain you in detail.
 
Upvote 0
Upvote 0
Hi, Thanks for you valueable time, I am adding all in one workbook but facing error like "Only comments may appear after End Sub, End Function, or End Property"... I have succeeded in disabling the worksheets but facing issue in printing it from the same macro worksheet which I asign it to the shape button.

Is there any way to upload the excel sheet here?


Sub PostToRegister() FACING ERROR HERE
Dim WS1 As Worksheet
Dim WS2 As Worksheet
Set WS1 = Worksheets("INVOICE")
Set WS2 = Worksheets("DATABASE")
WS2.Unprotect Password:="41297"
WS1.Unprotect Password:="41297"

' figure out which row is the next row
nextrow = WS2.Cells(Rows.Count, 1).End(xlUp).Row + 1

' write the important values to DATABASE
WS2.Cells(nextrow, 1).Resize(1, 7).Value = Array(WS1.Range("E11"), WS1.Range("E12"), WS1.Range("B11"), _
WS1.Range("B17"), WS1.Range("E25"), WS1.Range("E26"), WS1.Range("E27"))

Range("e12").Value = Range("e12").Value + 1
Range("b16:b23").ClearContents
Range("b11:b13").ClearContents
Range("e25").ClearContents

WS2.Protect Password:="41297"
WS1.Protect Password:="41297"

End Sub

Option Explicit

Public EnableToPrint As Boolean

Private Sub Workbook_BeforePrint(Cancel As Boolean)
If EnableToPrint = False Then
Cancel = True
MsgBox "To print use the macro button"
End If
End Sub

Private Sub Workbook_Open()

End Sub

Option Explicit

Sub MacroToPrint() FACING ERROR HERE
ThisWorkbook.EnableToPrint = True
Sheets("INVOICE").PrintOut FACING ERROR HERE
ThisWorkbook.EnableToPrint = False
End Sub
 
Upvote 0
images attached
 

Attachments

  • 1.png
    1.png
    112.7 KB · Views: 12
  • 2com.jpg
    2com.jpg
    33.4 KB · Views: 10
Upvote 0
and when I assign "Macrotoprint" it work, but it never save all invoice data to another sheet and also doesn't clear the current cells
 
Upvote 0
I am now facing an error with the protection sheet can you help?
 
Upvote 0

Forum statistics

Threads
1,224,812
Messages
6,181,102
Members
453,021
Latest member
Justyna P

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