Link to home
Start Free TrialLog in
Avatar of Gordon Smith
Gordon SmithFlag for United States of America

asked on

Prevent cut and paste?

Hi experts,

I was wondering if there was a way to prevent people from cutting and pasting in a wrokbook.

I have a workbook that is formatted in a way that if somone does cut and paste any information, it messes up the formatting of the sheet, (i.e. borders that are set a certain way or the background colors). All they need to do is either add numbers or delete numbers, that's it, then save the sheet.

I know that I can disable a toolbar or the shortcut menu, but I want to prevent ALL the ways a user can cut and paste information. This includes drag and drop.

It takes a bit of time to get the worksheet back to the way it should be after it gets messed up.

Thanks,
smidy
Avatar of WATYF
WATYF

You're gonna have to go through a few steps to accomplish that... Here's the first step... more to come.


This will take care of the drag and drop problem.

Sub Test()

    Application.CellDragAndDrop = False
   
End Sub


WATYF
This will also take care of the Edit menu items... more to come.


Sub DisableCopyPaste()

' disable drag and drop.
    Application.CellDragAndDrop = False
   
' disable Edit menu items.
    With CommandBars("Edit")
        .Controls("Cut").Enabled = False
        .Controls("Copy").Enabled = False
        .Controls("Paste").Enabled = False
        .Controls(6).Enabled = False
    End With
   
End Sub



WATYF
OK. This will disable the drag and drop... the Edit menu items... and the Copy/Paste functions on the right-click menu of the cells...


the last thing to handle would be keystoke controls (i.e. Ctrl-C for Copy, Ctrl-V for Paste, etc.)



Sub DisableCopyPaste()

' disable drag and drop.
    Application.CellDragAndDrop = False
   
' disable Edit menu items.
    With CommandBars("Edit")
        .Controls("Cut").Enabled = False
        .Controls("Copy").Enabled = False
        .Controls("Paste").Enabled = False
        .Controls("Paste Special...").Enabled = False
    End With
   
' disbale Cell right-click menu items.
    With CommandBars("Cell")
        .Controls("Cut").Enabled = False
        .Controls("Copy").Enabled = False
        .Controls("Paste").Enabled = False
        .Controls("Paste Special...").Enabled = False
    End With
   
End Sub



WATYF
OK. This should handle everything... let me know if there's anything I missed.


The toolbar buttons are disabled using the "FindControl" method, in case the user moves them to a toolbar other than the default (i.e. the Standard Toolbar)



Sub DisableCopyPaste()

' disable keystroke commands.
    Application.OnKey "^x", ""
    Application.OnKey "^c", ""
    Application.OnKey "^v", ""

' disable drag and drop.
    Application.CellDragAndDrop = False
   
' disable Edit menu items.
    With CommandBars("Edit")
        .Controls("Cut").Enabled = False
        .Controls("Copy").Enabled = False
        .Controls("Paste").Enabled = False
        .Controls("Paste Special...").Enabled = False
    End With
   
' disable Cell right-click menu items.
    With CommandBars("Cell")
        .Controls("Cut").Enabled = False
        .Controls("Copy").Enabled = False
        .Controls("Paste").Enabled = False
        .Controls("Paste Special...").Enabled = False
    End With
   
' disable Toolbar buttons.
    CommandBars.FindControl(msoControlButton, 21).Enabled = False
    CommandBars.FindControl(msoControlButton, 19).Enabled = False
    CommandBars.FindControl(msoControlButton, 22).Enabled = False
    CommandBars.FindControl(msoControlButton, 108).Enabled = False
    CommandBars.FindControl(msoControlButton, 369).Enabled = False
    CommandBars.FindControl(msoControlButton, 370).Enabled = False
    CommandBars.FindControl(msoControlButton, 755).Enabled = False

End Sub



WATYF
Avatar of Gordon Smith

ASKER

Okay WATYF,

With the disable drag and drop:

I placed this in Workbook_Open(). It works fine, but if I open a new work book I don't have the drag and drop capability. I have to change the False to True and open the workbook again.

I also tried the disable Edit menu items. It didn't seem to deactivate anything. I also placed this in the same location.  One step at a time.Should it be placed somewhere else?

I didn't try the others yet.

Thanks,
smidy
Yes... For the workbook that you want to "protect", you'll need to place these codes in the Activate/Deactive events. That way, when they look at the "protected" workbook, all the copy/paste options will be turned off... and when they switch to another workbook, the options will be turned back on.


So,... in the file that you want to protect, open the VB Editor (Alt+F11) then click on the "ThisWorkbook" module on the left and paste this code in.


Private Sub Workbook_WindowActivate(ByVal Wn As Window)

' disable keystroke commands.
   Application.OnKey "^x", ""
   Application.OnKey "^c", ""
   Application.OnKey "^v", ""

' disable drag and drop.
   Application.CellDragAndDrop = False
   
' disable Edit menu items.
   With CommandBars("Edit")
       .Controls("Cut").Enabled = False
       .Controls("Copy").Enabled = False
       .Controls("Paste").Enabled = False
       .Controls("Paste Special...").Enabled = False
   End With
   
' disable Cell right-click menu items.
   With CommandBars("Cell")
       .Controls("Cut").Enabled = False
       .Controls("Copy").Enabled = False
       .Controls("Paste").Enabled = False
       .Controls("Paste Special...").Enabled = False
   End With
   
' disable Toolbar buttons.
   CommandBars.FindControl(msoControlButton, 21).Enabled = False
   CommandBars.FindControl(msoControlButton, 19).Enabled = False
   CommandBars.FindControl(msoControlButton, 22).Enabled = False
   CommandBars.FindControl(msoControlButton, 108).Enabled = False
   CommandBars.FindControl(msoControlButton, 369).Enabled = False
   CommandBars.FindControl(msoControlButton, 370).Enabled = False
   CommandBars.FindControl(msoControlButton, 755).Enabled = False


End Sub


Private Sub Workbook_WindowDeactivate(ByVal Wn As Window)

' enable keystroke commands.
   Application.OnKey "^x"
   Application.OnKey "^c"
   Application.OnKey "^v"

' enable drag and drop.
   Application.CellDragAndDrop = True
   
' enable Edit menu items.
   With CommandBars("Edit")
       .Controls("Cut").Enabled = True
       .Controls("Copy").Enabled = True
       .Controls("Paste").Enabled = True
       .Controls("Paste Special...").Enabled = True
   End With
   
' enable Cell right-click menu items.
   With CommandBars("Cell")
       .Controls("Cut").Enabled = True
       .Controls("Copy").Enabled = True
       .Controls("Paste").Enabled = True
       .Controls("Paste Special...").Enabled = True
   End With
   
' enable Toolbar buttons.
   CommandBars.FindControl(msoControlButton, 21).Enabled = True
   CommandBars.FindControl(msoControlButton, 19).Enabled = True
   CommandBars.FindControl(msoControlButton, 22).Enabled = True
   CommandBars.FindControl(msoControlButton, 108).Enabled = True
   CommandBars.FindControl(msoControlButton, 369).Enabled = True
   CommandBars.FindControl(msoControlButton, 370).Enabled = True
   CommandBars.FindControl(msoControlButton, 755).Enabled = True

End Sub



WATYF
Not sure why the Edit menu didn't disable. Try this latest code and let me know. If it doesn't work, then maybe you can send me the file and I'll check it out.


WATYF
WATYF,

I added the drag and drop = false to worksheet_activate, and changed false to true in a worksheet_deactivate event. that works and is a start.

smidy
OK good. Try the whole code and let me know if it works. It basically does the same thing as what you just set up using activate/deactivate... except it turns a lot more options on/off.


WATYF
Oh wait... you said worksheet?? Is this operation specific to just one worksheet in the workbook??? Let me know.

WATYF
It needs to be the entire ThisWorkbook. This is where I placed the code.

Also, if I need to make changes I'll need a way run a macro to be able to work in it. Some thing I just thought of.

smidy
I'm not sure I follow that last question. Run a macro to work on what?


WATYF
I added the entire code and I get a "Run Time Error 91" at the "With CommandBars ("Edit") on the activate side when the program opens.

smidy
Hhhmmm... OK.

What version of Excel are you using?

Also,... would it be possible to send me the file? If not, I can send you a functioning file using the methods we've outlines and if that doesn't work on your machine, then there must be some kind of compatibility difference.

Let me know. Thanks.


WATYF
Disregard the macro. I obviously didn't read into your code far enough. My fault.

smidy
We just miss the messages. I'm using Office 2000.

smidy
OK. I've got it. Sorry... I built the original code in the standard module... so I can just say "CommandBars" because "Application" is implied... but in a Workbook module, Application isn't implied, so I had to add that.


This should work. Just paste it into the ThisWorkbook module. It will toggle the Copy/Paste features on and off and you go back and forth between your the protected and unprotected files.


Private Sub Workbook_WindowActivate(ByVal Wn As Window)

    Set XLApp = Application

' disable keystroke commands.
    XLApp.OnKey "^x", ""
    XLApp.OnKey "^c", ""
    XLApp.OnKey "^v", ""

' disable drag and drop.
    XLApp.CellDragAndDrop = False
 
' disable Edit menu items.
    With XLApp.CommandBars("Edit")
        .Controls("Cut").Enabled = False
        .Controls("Copy").Enabled = False
        .Controls("Paste").Enabled = False
        .Controls("Paste Special...").Enabled = False
    End With
 
' disable Cell right-click menu items.
    With XLApp.CommandBars("Cell")
        .Controls("Cut").Enabled = False
        .Controls("Copy").Enabled = False
        .Controls("Paste").Enabled = False
        .Controls("Paste Special...").Enabled = False
    End With
 
' disable Toolbar buttons.
    XLApp.CommandBars.FindControl(msoControlButton, 21).Enabled = False
    XLApp.CommandBars.FindControl(msoControlButton, 19).Enabled = False
    XLApp.CommandBars.FindControl(msoControlButton, 22).Enabled = False
    XLApp.CommandBars.FindControl(msoControlButton, 108).Enabled = False
    XLApp.CommandBars.FindControl(msoControlButton, 369).Enabled = False
    XLApp.CommandBars.FindControl(msoControlButton, 370).Enabled = False
    XLApp.CommandBars.FindControl(msoControlButton, 755).Enabled = False

End Sub


Private Sub Workbook_WindowDeactivate(ByVal Wn As Window)

    Set XLApp = Application
   
' enable keystroke commands.
    XLApp.OnKey "^x"
    XLApp.OnKey "^c"
    XLApp.OnKey "^v"

' enable drag and drop.
    XLApp.CellDragAndDrop = True
 
' enable Edit menu items.
    With XLApp.CommandBars("Edit")
        .Controls("Cut").Enabled = True
        .Controls("Copy").Enabled = True
        .Controls("Paste").Enabled = True
        .Controls("Paste Special...").Enabled = True
    End With
 
' enable Cell right-click menu items.
    With XLApp.CommandBars("Cell")
        .Controls("Cut").Enabled = True
        .Controls("Copy").Enabled = True
        .Controls("Paste").Enabled = True
        .Controls("Paste Special...").Enabled = True
    End With
 
' enable Toolbar buttons.
    XLApp.CommandBars.FindControl(msoControlButton, 21).Enabled = True
    XLApp.CommandBars.FindControl(msoControlButton, 19).Enabled = True
    XLApp.CommandBars.FindControl(msoControlButton, 22).Enabled = True
    XLApp.CommandBars.FindControl(msoControlButton, 108).Enabled = True
    XLApp.CommandBars.FindControl(msoControlButton, 369).Enabled = True
    XLApp.CommandBars.FindControl(msoControlButton, 370).Enabled = True
    XLApp.CommandBars.FindControl(msoControlButton, 755).Enabled = True

End Sub


WATYF
Now I get  "Set XLApp = Application" variable not defined. The "XLApp" is highlighted.

smidy
You must have turned Option Explicit on.


Add this line ABOVE all the other code.


Dim XLApp As Application



WATYF
You're getting there!

I thought maybe it had to be Dim'd. I'm way too new into programming to have really got that.

Now I get Error 91 again on:
XLApp.CommandBars.FindControl(msoControlButton, 369).Enabled = False

This is on the activate.

smidy
OK. Sorry for all the hang ups. :oP

Those last three buttons are custom paste buttons (Paste Values, Paste Formats, etc) and I just included them in there just in case... but I guess you don't have those buttons active... so I just deleted them from the activate/deactivate.


Use this code instead:



Dim XLApp As Application

Private Sub Workbook_WindowActivate(ByVal Wn As Window)

    Set XLApp = Application

' disable keystroke commands.
    XLApp.OnKey "^x", ""
    XLApp.OnKey "^c", ""
    XLApp.OnKey "^v", ""

' disable drag and drop.
    XLApp.CellDragAndDrop = False
 
' disable Edit menu items.
    With XLApp.CommandBars("Edit")
        .Controls("Cut").Enabled = False
        .Controls("Copy").Enabled = False
        .Controls("Paste").Enabled = False
        .Controls("Paste Special...").Enabled = False
    End With
 
' disable Cell right-click menu items.
    With XLApp.CommandBars("Cell")
        .Controls("Cut").Enabled = False
        .Controls("Copy").Enabled = False
        .Controls("Paste").Enabled = False
        .Controls("Paste Special...").Enabled = False
    End With
 
' disable Toolbar buttons.
    XLApp.CommandBars.FindControl(msoControlButton, 21).Enabled = False
    XLApp.CommandBars.FindControl(msoControlButton, 19).Enabled = False
    XLApp.CommandBars.FindControl(msoControlButton, 22).Enabled = False
    XLApp.CommandBars.FindControl(msoControlButton, 108).Enabled = False

End Sub

Private Sub Workbook_WindowDeactivate(ByVal Wn As Window)

    Set XLApp = Application
   
' enable keystroke commands.
    XLApp.OnKey "^x"
    XLApp.OnKey "^c"
    XLApp.OnKey "^v"

' enable drag and drop.
    XLApp.CellDragAndDrop = True
 
' enable Edit menu items.
    With XLApp.CommandBars("Edit")
        .Controls("Cut").Enabled = True
        .Controls("Copy").Enabled = True
        .Controls("Paste").Enabled = True
        .Controls("Paste Special...").Enabled = True
    End With
 
' enable Cell right-click menu items.
    With XLApp.CommandBars("Cell")
        .Controls("Cut").Enabled = True
        .Controls("Copy").Enabled = True
        .Controls("Paste").Enabled = True
        .Controls("Paste Special...").Enabled = True
    End With
 
' enable Toolbar buttons.
    XLApp.CommandBars.FindControl(msoControlButton, 21).Enabled = True
    XLApp.CommandBars.FindControl(msoControlButton, 19).Enabled = True
    XLApp.CommandBars.FindControl(msoControlButton, 22).Enabled = True
    XLApp.CommandBars.FindControl(msoControlButton, 108).Enabled = True

End Sub




WATYF
BY CRACKY, I THINK YOU'VE GOT IT!!!

Now, if I needed to unprotect the work book to make changes to it, what would I need to do in order to be able to do that?

smidy
Can I set up the deactivate as a macro?

smidy
ASKER CERTIFIED SOLUTION
Avatar of WATYF
WATYF

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
WATYF,

You topped yourself from when you helped me earlier this year.

This was a lot of fun and I learned a lot too. Thank god I have a little bit of programming under my belt.
BTW, I went and bought two Excel VBA books. But there was no way I would have been able to get to this.

Keep up the good work.

Thanks,
Gordon
No Problem Gordon. Thanks for the grade.


WATYF
I added the coding to my workbook.  In most cases I can't copy/paste, but if i go to a cell with content I can copy and paste from the input field at the top.

Jp
I have the worksheet locked and hidden so users can't even see any of the information in the formula bar.
Hi Team

I have a question around the above please. I need to disable drag and drop and cut and paste so I am using the code given by Watyf on 10/17 at 11.29AM.  I pasted it into VBA and I receive the following message error when I run the macro
Object Variable not set
for the line With CommandBars("Edit")

I suppose i will get the same for
With CommandBars("Cells")


Thanks for your input
VP
You're probably running the code in the Workbook or Worksheet module... in which case, the Application object is not implied.

So you would need to use:


Application.CommandBars("Edit") etc. etc.


basically... anywhere that it references the CommandBars collection, you'll need to add the application object before it.

Or you could just run the code from a standard module, in which case the application object is implied.


WATYF
Will this also work in Excel 2003?  I am an Excel user, not a programmer....I just wanted to make sure this would work in 2003 before I spent much time with it.  Thanks
Yes. The code should work fine in Excel 2003.


WATYF
I originally found your code at www.ozgrid.com, and came here for more information.  I was able to copy and paste the code to my VBA editor, and seems to be working good.  I have found one small problem and not sure if I messed the code up or not.  If I highlight a cell and right click, the cut-copy-paste features are disabled.  But, if I highlight the number in the formula bar and right click, the cut-copy-paste features are enabled.  Thanks again for your help.
Well... that's true... but I don't know that that's as much of a security issue as copying an actual cell. They won't be able to copy any formats/comments/etc by doing that, and they won't be able to paste into a protected cell from there (because the paste feature won't be available once they click on a cell). So all they could really do with that is copy some text from the formula bar and paste it into another application (like Word, for example).


That's just my initial reaction, though. If you can think of a reason why that copy/paste should be disabled then maybe we can try to find the objects we'd need to reference to disable that.


WATYF