PDA

View Full Version : How to Disable Copy from Menu Bar/Edit Commandbar



fionabolt
06-07-2007, 02:20 AM
Can anyone help. I have created a template that results in a document they is password protected where editing rights are restricted only to a certain style.
However, users could select all, copy the contents and paste them into another document and edit the elements they are not supposed to!

Therefore I am trying to disable the copy command on the Edit commandbar of the Menu bar. Do do this, I wrote the following:

Public Sub Document_New()
Call DisableCopy
End Sub
Public Sub DisableCopy()
Dim myControls As CommandBarControls
Dim nS As Boolean, tS As Boolean, dS As Boolean
Dim oCtrl As CommandBarControl
Dim aKey As KeyBinding
Dim myStr As String

'capture saved state
nS = NormalTemplate.Saved
tS = ActiveDocument.AttachedTemplate.Saved
dS = ActiveDocument.Saved

Rem--disable copy on toolbar

Customizati
Set myC For Each oCtrl In CommandBars.FindControls(ID:=19)
If oCtrl.Enabled = True Then
oCtrl.Enabled = False
End If
Next oCtrl

Rem--disable "CTL+C" copy

Customizati For Each aKey In KeysBoundTo(KeyCategory:=wdKeyCategoryCommand, _
Command:="EditCopy")
myStr = myStr & aKey.KeyString & vbCr
aKey.Disable
Next aKey
MsgBox myStr

'return to captured saved state
NormalTemplate.Saved = nS
ActiveDocument.AttachedTemplate.Saved = tS
ActiveDocument.Saved = dS

End Sub

But it doesnt work. What am I doing wrong?

Thanks

lucas
06-07-2007, 06:59 AM
This has been discussed at length here in the forum...search for disable
Here is a link to a recent discussion in Excel but it will still be relevant:
http://www.vbaexpress.com/forum/showthread.php?t=13064&highlight=disable

fionabolt
06-07-2007, 07:44 AM
Thanks Lucas (I had done a search on the Kb but hadn't found what I was looking for).

I have changed my code to:
Public Sub Document_New()
Call DisableCopy
End Sub
Public Sub DisableCopy()
ActiveDocument.CommandBars("Edit").Controls("Copy").Enabled = False
ActiveDocument.CommandBars("Standard").Controls("Copy").Enabled = False
CustomizationContext = ActiveDocument
For Each aKey In KeysBoundTo(KeyCategory:=wdKeyCategoryCommand, _
Command:="EditCopy")
myStr = myStr & aKey.KeyString & vbCr
aKey.Disable
Next aKey
End Sub

Which does exactly what I want to the activedocument. Which is great. However, it also disables copy on any other open documents, or any document opened in addition. Which is not what I want.
How can I disable the copy function just from this document?

lucas
06-07-2007, 07:59 AM
Correct me if I'm wrong but that only happens when you create a document from that particular template....it shouldn't happen if you open a regular new word document after running this template..

lucas
06-07-2007, 08:01 AM
you have to select some text before you check edit..cut, copy paste.

fumei
06-10-2007, 05:47 PM
You can do this much easier.Sub EditCopy()
MsgBox "Copy actions have been disabled for this document."
End SubThis disables both the menu copy command and the Ctrl-C command.

Put the above into the ThisDocument module of the .DOT file. Any document created from that template will have the copy function disabled.

You can do the same for just about any other Word command. So you could disable the Paste functions, etc. etc.

fumei
06-10-2007, 05:54 PM
BTW: the overwrite is specific to the template.

Also, the overwrite will follow a SaveAs. Copy will still be disabled.

fionabolt
06-26-2007, 12:21 AM
Fumei
Sorry about the delay in responding. Pressures elsewhere meant I couldn't look further into this. However, you got it spot on. This is simple and works like a dream. Many thanks
Fiona