Tuesday, November 25, 2008

Make a non printable Excel file through macro

Hi all,

Today we are discussed about how to make a non printable excel file through vba macro.

Open a new excel sheet and then open Visual basic editor by pressing alt+F11 or you also can open visual basic editor by the following path Tools>Macro>visual basic editor.
Now open Thisworkbook and copy and paste the following code on it.

Private Sub Workbook_Activate()
Dim loectrl As Office.CommandBarControl

'disable print menu
For Each loectrl In Application.CommandBars.FindControls(ID:=4)
loectrl.Enabled = False
Next loectrl

'disable print privew menu
For Each loectrl In Application.CommandBars.FindControls(ID:=109)
loectrl.Enabled = False
Next loectrl

'disable print area menu
For Each loectrl In Application.CommandBars.FindControls(ID:=30255)
loectrl.Enabled = False
Next loectrl

'disable standard print munu
For Each loectrl In Application.CommandBars.FindControls(ID:=2521)
loectrl.Enabled = False
Next loectrl

'disable set print area
For Each loectrl In Application.CommandBars.FindControls(ID:=364)
loectrl.Enabled = False
Next loectrl

' disable add to print area
For Each loectrl In Application.CommandBars.FindControls(ID:=1583)
loectrl.Enabled = False
Next loectrl

'disable exclude from print area
For Each loectrl In Application.CommandBars.FindControls(ID:=1586)
loectrl.Enabled = False
Next loectrl

'disable reset print area
For Each loectrl In Application.CommandBars.FindControls(ID:=2521)
loectrl.Enabled = False
Next loectrl

'disable page setup
For Each loectrl In Application.CommandBars.FindControls(ID:=247)
loectrl.Enabled = False
Next loectrl

End Sub

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

Private Sub Workbook_Deactivate()
Dim loectrl As Office.CommandBarControl

'enable print menu
For Each loectrl In Application.CommandBars.FindControls(ID:=4)
loectrl.Enabled = True
Next loectrl

'enable print privew menu
For Each loectrl In Application.CommandBars.FindControls(ID:=109)
loectrl.Enabled = True
Next loectrl

'enable print area menu
For Each loectrl In Application.CommandBars.FindControls(ID:=30255)
loectrl.Enabled = True
Next loectrl

'enable standard print munu
For Each loectrl In Application.CommandBars.FindControls(ID:=2521)
loectrl.Enabled = True
Next loectrl

'enable set print area
For Each loectrl In Application.CommandBars.FindControls(ID:=364)
loectrl.Enabled = True
Next loectrl

' enable add to print area
For Each loectrl In Application.CommandBars.FindControls(ID:=1583)
loectrl.Enabled = True
Next loectrl

'enable exclude from print area
For Each loectrl In Application.CommandBars.FindControls(ID:=1586)
loectrl.Enabled = True
Next loectrl

'enable reset print area
For Each loectrl In Application.CommandBars.FindControls(ID:=2521)
loectrl.Enabled = True
Next loectrl

'enable page setup
For Each loectrl In Application.CommandBars.FindControls(ID:=247)
loectrl.Enabled = True
Next loectrl
End Sub

Here you can see a lot of Ids and for write a code like this you must be know about all IDs. For the list of IDs click here

From the following link you can down load a sample file for the same

Download file: Click Here

No comments:

Subscribe Now: From your browser

Search:

Learn Office Excel