Disabling Printing

by Allen Wyatt
(last updated July 28, 2014)

8

If you are creating workbooks to be used by others, you may be interested in disabling the print options (menu and toolbar) whenever a specific workbook is open. The easiest way to do so is to use the Auto_Open macro (which runs immediately upon opening the workbook that contains it) to make the menu and toolbar printing commands no longer available. The following will do the trick nicely:

Sub Auto_Open()
    'Prevent Printing via menu
    MenuBars(xlWorksheet).Menus("File").MenuItems("Print...").Delete

    'Turn off Print icon wherever it may be in the toolbars
    For J = 1 To Toolbars.Count
        For K = 1 To Toolbars(J).ToolbarButtons.Count
            If Toolbars(J).ToolbarButtons(K).Id = 2 Then
                Toolbars(J).ToolbarButtons(K).Enabled = False
            End If
            If Toolbars(J).ToolbarButtons(K).Id = 3 Then
                Toolbars(J).ToolbarButtons(K).Enabled = False
            End If
        Next K
    Next J
End Sub

You can also create a special Auto_Close macro that restores the menus and toolbars when the workbook is closed:

Sub Auto_Close()
    'Reset the menu items
    For Each mb In MenuBars
        mb.Reset
    Next mb

    'Reset the buttons
    For J = 1 To Toolbars.Count
        For K = 1 To Toolbars(J).ToolbarButtons.Count
            If Toolbars(J).ToolbarButtons(K).Id = 2 Then
                Toolbars(J).ToolbarButtons(K).Enabled = True
            End If
            If Toolbars(J).ToolbarButtons(K).Id = 3 Then
                Toolbars(J).ToolbarButtons(K).Enabled = True
            End If
        Next K
    Next J
End Sub

You should note that these macros only run when the specific workbook is opened and closed. That means that your printing capability will be unavailable as long as the workbook is open—even for any other open workbooks you may have.

Another approach is to cancel any printing before it starts. The following is a macro you can place within a workbook module:

Private Sub Workbook_BeforePrint(Cancel As Boolean)
    Cancel = True
End Sub

Whenever someone tries to print the workbook, the process is automatically cancelled. Otherwise, the menu choices and toolbar buttons remain visible. (You could also change the macro to not only cancel but to display a message box indicating that users are not allowed to print.)

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (2556) applies to Microsoft Excel 97, 2000, 2002, and 2003.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He  is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Trimming Spaces from Strings

When processing text with a macro, you often need to remove extraneous spaces from the text. VBA provides three handy ...

Discover More

Intelligent Title Case

A common editorial need is to change the capitalization used on different words in a selection of text. Word provides a ...

Discover More

Chapter Numbers in Indexes and TOAs

Word allows you to define prefixes for page numbers. These are often used for chapter or section numbers in a large document. ...

Discover More

Program Successfully in Excel! John Walkenbach's name is synonymous with excellence in deciphering complex technical topics. With this comprehensive guide, "Mr. Spreadsheet" shows how to maximize your Excel experience using professional spreadsheet application development tips from his own personal bookshelf. Check out Excel 2013 Power Programming with VBA today!

MORE EXCELTIPS (MENU)

Printing Selected Worksheets

When you accumulate quite a few workbooks in folder, you might need to print out selected worksheets from all of the ...

Discover More

Printing Rows Conditionally

Need to only print out certain rows from your data? It's easy to do if you apply the filtering or sorting techniques ...

Discover More

Printing Workbook Properties

Want to create a printed record of the properties associated with a workbook? There is no easy way to do it in Excel. Here's ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments for this tip:

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] in your comment text. You’ll be prompted to upload your image when you submit the comment. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is eight more than 7?

2015-07-20 18:01:19

Mike

Does this work if macros are disabled, as is standard practice when initially opening excel files these days?


2015-04-20 07:33:28

Barry

@Margus

The "Workbook_BeforePrint" will intercept any request to print (even if it is from a macro). So to allow a macro to print I would create a global variable, say "AllowPrinting" which is set just before your macro wants to print, and reset in the "Workbook_BeforePrint" routine:

Private Sub Workbook_BeforePrint(Cancel As Boolean)
If AllowPrinting = False then
Cancel = True
End If
AllowPrinting = False
End Sub
This should be placed in the "ThisWorkBook" code page.

Sub YourCode()
....... your code
....... your code
AllowPrinting =True
Worksheets("Sheet1").Printout
....... your code
....... your code
End sub
The .Printout method is using default settings but can have quite a few parameters if required.

The variable "AllowPrinting" should be declared on a "Module" page outside of any routine.

Public AllowPrinting as Boolean


2015-04-20 04:35:42

Margus

Is there any way I could disable normal printing for all files, but users should be able to print with set macro.


2014-12-20 06:06:25

Barry

@Vuke
I suggest the following macro which only allows "Sheet2" to be printed if it is the "active" sheet:

Private Sub Workbook_BeforePrint(Cancel As Boolean)
if Activesheet.Name<>"Sheet2" then
Cancel = True
End If
End Sub

This should be placed in the "ThisWorkBook" code page, and the name of sheet2 should be substituted in the "If...." statement.


2014-12-19 09:38:46

Vuke

I have a workbook with two worksheets, I do not want clerks to be able to print sheet 1 but to print sheet 2 only. Reply to my address.


2014-10-07 05:17:58

Himanshu

Pls explain when their is no macro enable but still printing options are not getting enable.


2014-09-22 11:31:56

John

Does not work. Have tried numerous suggestions and all fail to prevent printing.


2014-07-16 15:59:14

Chris

Your variables are not defined so this will not run.


Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.

Links and Sharing
Share