Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Disable print in MS Word?

Status
Not open for further replies.

drlandau

IS-IT--Management
Jul 20, 2000
36
DK
Hi,

Does anyone know how to disable the print functionality in a Word document? Maybe using a VB macro?

I have a sensitive document that was accidently printed (even though I had explicitly written in the header that the document was NOT to be printed - and even in flashing letters...doh!)

I would like to somehow diable the print functionality, maybe by gryaing out the option all together - or "capturing" the Print messagebox and displaying another instead saying that "Printing of this document is not allowed".

Or maybe there is a much easiere way - or none at all?

Any help is highly appreciated.

Best regards,
Nicolaj Rasmussen
 
Copy and paste the attached code into the VBA declaration of your document- you can get there by pressing alt-F11. Note- while this document is open you will not be able to print at all, even another opened doc. Until this doc is closed. Note on code- It may appear that the OnOpen Event is deleting the same command twice, however after the first delete the commands are all bumped down one. Therefore once you delete Print Preview(Number 9 counting down from the top), Print is moved to the Number 9 spot and must be deleted as Number 9. Good luck- here's your code...

Private Sub Document_Close()

CommandBars("File").Reset

End Sub

Private Sub Document_Open()

'Delete Print Preview
CommandBars("File").Controls(9).Delete True
'Delete Print
CommandBars("File").Controls(9).Delete True

End Sub

 
One more note- ctrl-p will still print. I'll see if there is another work-around and let you know if I find something.
 
Hi Pezamystik,

Thanks for your reply, it works!

But as you write Ctrl-P still works. And te Print icon is still displayed in the menubar (and this is probably the one people use the most). Any ideas for this?

Best regards,
Nicolaj

 
Here you go- forget the previous code that I posted. I don't think it was flexible enough for your needs. In the code below subCommandBars will enumerate through all Command Bars/Toolbars, if they contain copy, cut or paste it will disable/enable them- also a bit more professional than the previous code(which deleted commands). I don't really use Word, let alone code for Word, so I could not write the code to enumerate through the keyboard shortcuts in the same way. But it this code will atleast disable the default keyboard shortcuts. If you have any questions let me know...

Private Sub Document_Close()

KeyBindings.ClearAll

subCommandBars True

End Sub

Private Sub Document_Open()

MsgBox "Copy, Cut and Print have been disabled." & vbCr & "Close this document to enable.", vbOKOnly, Me.Name

' Disable Copy Keyboard Shortcuts
FindKey(BuildKeyCode(wdKeyC, wdKeyControl)).Disable
FindKey(BuildKeyCode(wdKeyInsert, wdKeyControl)).Disable
' Disable Cut Keyboard Shortcuts
FindKey(BuildKeyCode(wdKeyX, wdKeyControl)).Disable
FindKey(BuildKeyCode(wdKeyDelete, wdKeyShift)).Disable
' Disable Print Keyboard Shortcuts
FindKey(BuildKeyCode(wdKeyF12, wdKeyControl, wdKeyShift)).Disable
FindKey(BuildKeyCode(wdKeyP, wdKeyControl)).Disable
' Disable Print Preview Keyboard Shortcuts
FindKey(BuildKeyCode(wdKeyF2, wdKeyControl)).Disable
FindKey(BuildKeyCode(wdKeyI, wdKeyControl, wdKeyAlt)).Disable

subCommandBars False

End Sub

Private Sub subCommandBars(IsEnabled As Boolean)

Dim Bar As CommandBar
Dim Con As CommandBarControl

For Each Bar In Application.CommandBars
For Each Con In Bar.Controls
If Con.Caption = "&Copy" Or Con.Caption = "Cu&t" Or Con.Caption = "&Print" Or Con.Caption = "Print Pre&view" Then
Con.Enabled = IsEnabled
End If
Next Con
Next Bar

End Sub
 
Here it is. Now if someone creates their own toolbar or own keyboard shortcuts this will disable theirs as well. Hope its what you wanted.

Private Sub Document_Close()

KeyBindings.ClearAll

subCommandBars True

End Sub

Private Sub Document_Open()

MsgBox "Copy, Cut and Print have been disabled." & vbCr & "Close this document to enable.", vbOKOnly, Me.Name

subCommandBars False

subKeyCommands "EditCopy"
subKeyCommands "EditCut"
subKeyCommands "FilePrint"
subKeyCommands "FilePrintPreview"

End Sub

Private Sub subCommandBars(IsEnabled As Boolean)

Dim Bar As CommandBar
Dim Con As CommandBarControl

For Each Bar In Application.CommandBars
For Each Con In Bar.Controls
If Con.Caption = "&Copy" Or Con.Caption = "Cu&t" Or Con.Caption = "&Print" Or Con.Caption = "Print Pre&view" Then
Con.Enabled = IsEnabled
End If
Next Con
Next Bar

End Sub

Private Sub subKeyCommands(strCommand As String)

Dim Key As KeyBinding

For Each Key In KeysBoundTo(KeyCategory:=wdKeyCategoryCommand, Command:=strCommand)
Key.Disable
Next Key

End Sub
 
Hi again,

Wow I'm impressed of all the work you've done, thanks!
The buttons are grayed out, but the options in the Files and Exit menues are still available - and so are the keyboard shortcuts.

I use a Danish Word 97 so I guess this could have something to do with it?

All the best,
Nicolaj
 
Ehh I kinda have a problem now, because now in every document I open the Print and Print Preview are grayed out... Can you help me enable them again?

Thanks!

All the best,
Nicolaj
 
Make sure you still have this bit of code attached to your document. You must also have the other procedures I wrote attached to the document with it...

Private Sub Document_Close()

KeyBindings.ClearAll

subCommandBars True

End Sub

Try that first, if it doesn't work try this bit of code, this is completely independent- You need no other code for it to function. Copy and paste this into the module section of your document and run it manually- click the blue arrow on the menu bar OR press F5.

Private Sub Reset()

CommandBars("Edit").Reset
CommandBars("File").Reset
KeyBindings.ClearAll

End Sub

Let me know what happens.
 
Darn, I dunno if I'm doin' something wrong, but it doesn't seem to work.

Lets step through:

1) I choose Alt+F11
2) In the project Tab and choose Project - Microsoft Word Documents - ThisDocument
3) Paste the code
4) Run it

I also tried to change the Reset-code to a Document_Open and/or Document_Close, save the code and document and close and reopen it, but th Print buttons are still grayed out. Is there anywhere else I can change it?

Note: I haven't tried to reboot as my computer as always turned on (but lcked when I'm away). I use MS Windows 2000 Prof. - does this have anything to say?

Thanks,
Nicolaj
 
To do this manually right-click on the toolbar, click customize, select Standard, then click the reset button- A Dialog box will pop-up asking which document you want to reset the toolbar for, select normal.dot. You will need to do this for the Standard and Menu Bar toolbars. Let me know if this doesn't work.
 
That did the trick, thanks! :)

Why the script version of the reset didn't work on my machine I don't know. Do you have any idea?

All the best,
Nicolaj
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top