Results 1 to 16 of 16

Thread: Disable Paste(?)

  1. #1

    Thread Starter
    Fanatic Member doofusboy's Avatar
    Join Date
    Apr 2003
    Posts
    526

    Disable Paste(?)

    Is it possible to prevent a user from Pasting to any cell in a spreadsheet?
    Do canibals not eat clowns because they taste funny?

  2. #2
    Lively Member
    Join Date
    Jun 2003
    Posts
    114
    Doof,

    Only thing I can think of is to block/capture the Crtl - V sequence ...

  3. #3

    Thread Starter
    Fanatic Member doofusboy's Avatar
    Join Date
    Apr 2003
    Posts
    526
    Thought of that, but what about pasting from toolbar, or drag-drop?
    Do canibals not eat clowns because they taste funny?

  4. #4
    New Member
    Join Date
    Jun 2003
    Location
    Dist 2, Ho Chi Minh City,Viet Nam
    Posts
    13
    Should you protect the speadsheet incase you only want to view the spreadsheet ?

  5. #5

    Thread Starter
    Fanatic Member doofusboy's Avatar
    Join Date
    Apr 2003
    Posts
    526
    Thanks Chien, but no. I need a slew of users to input data to the spreadsheet and I'm trying to control what they put in there.

    I've put Data Validation on all the cells but it gets blown away if someone pastes data into a cell.
    Do canibals not eat clowns because they taste funny?

  6. #6
    Fanatic Member WorkHorse's Avatar
    Join Date
    Jul 2002
    Location
    Where you live.
    Posts
    591
    I don't know of any way to stop paste on the basic process level. I wish MS would put all command processes into workbook events. But I came up with this, which should disable any paste functions in Excel. I can't guarnatee that I caught them all, but this should give pretty good protection.

    In a module:

    VB Code:
    1. Option Explicit
    2.  
    3. Sub PasteEnable(ByVal blnEnabled As Boolean)
    4.    
    5.     On Error Resume Next
    6.  
    7.     ' Insert command controls.
    8.     CommandControlEnable 248, blnEnabled, True  ' Insert Copied C&ells...
    9.     CommandControlEnable 262, blnEnabled, True  ' Insert Copied C&ells...
    10.     CommandControlEnable 272, blnEnabled, True  ' Insert Copied C&ells...
    11.     CommandControlEnable 282, blnEnabled, True  ' Insert Copied C&ells...
    12.     CommandControlEnable 295, blnEnabled, True  ' Copied C&ells...
    13.     CommandControlEnable 299, blnEnabled, True  ' Insert Copied C&ells...
    14.     CommandControlEnable 316, blnEnabled, True  ' Insert Copied C&ells...
    15.     CommandControlEnable 340, blnEnabled, True  ' Insert Copied C&ells...
    16.     CommandControlEnable 454, blnEnabled, True  ' Insert Copied C&ells...
    17.     CommandControlEnable 468, blnEnabled, True  ' Insert Copied C&ells...
    18.     CommandControlEnable 3184, blnEnabled, True ' Copied C&ells...
    19.     CommandControlEnable 3185, blnEnabled, True ' Insert Copied C&ells...
    20.     CommandControlEnable 3187, blnEnabled, True ' Insert Copied C&ells
    21.    
    22.     ' Paste command controls.
    23.     CommandControlEnable 22, blnEnabled, True   ' &Paste
    24.     CommandControlEnable 755, blnEnabled, True  ' Paste &Special...
    25.     CommandControlEnable 879, blnEnabled, True  ' &Paste...
    26.     CommandControlEnable 945, blnEnabled, True  ' &Paste
    27.     CommandControlEnable 2787, blnEnabled, True ' Paste as &Hyperlink
    28.    
    29.     ' Paste All on Clipboard toolbar cannot be disabled.
    30.     ' Disable/enable toolbar completely.
    31.     'CommandControlEnable 3633, blnEnabled, "Paste A&ll", "Paste A&ll Disabled"
    32.     CommandBars("Clipboard").Enabled = blnEnabled
    33.    
    34.     ' Set protection from customization for all toolbars.
    35.     CommandBarsProtectionEnable blnEnabled
    36.    
    37.     ' Paste keyboard functions.
    38.     If blnEnabled = True Then
    39.         Application.OnKey "^v"
    40.         Application.OnKey "+{INSERT}"
    41.     Else
    42.         Application.OnKey "^v", "PasteEnableFalseMessage"
    43.         Application.OnKey "+{INSERT}", "PasteEnableFalseMessage"
    44.     End If
    45.  
    46. End Sub
    47.  
    48. Sub CommandControlEnable(ByVal lngCommandId As Long, _
    49.                          ByVal blnEnabled As Boolean, _
    50.                 Optional ByVal blnChangeCaption As Boolean)
    51.  
    52.     On Error Resume Next
    53.  
    54.     Dim ctlControls As CommandBarControls
    55.     Dim ctlControl  As CommandBarControl
    56.     Dim strCaption  As String
    57.    
    58.     ' Get all matching command controls.
    59.     Set ctlControls = CommandBars.FindControls(ID:=lngCommandId)
    60.     If Not ctlControls Is Nothing Then
    61.        
    62.         ' Get caption.
    63.         strCaption = ctlControls.Item(1).Caption
    64.         If blnChangeCaption = True Then
    65.             If blnEnabled = True Then
    66.                 If Right$(strCaption, 9) = " Disabled" Then
    67.                     strCaption = Left$(strCaption, Len(strCaption) - 9)
    68.                 End If
    69.             Else
    70.                 If Not Right$(strCaption, 9) = " Disabled" Then
    71.                     strCaption = strCaption & " Disabled"
    72.                 End If
    73.             End If
    74.         End If
    75.        
    76.         ' Toggle enabled and set caption.
    77.         For Each ctlControl In ctlControls
    78.             ctlControl.Enabled = blnEnabled
    79.             ctlControl.Caption = strCaption
    80.         Next
    81.    
    82.     End If
    83.  
    84. End Sub
    85.  
    86. Sub CommandBarsProtectionEnable(ByVal blnProtect As Boolean)
    87.  
    88.     On Error Resume Next
    89.  
    90.     Dim intProtect  As Integer
    91.     Dim i           As Integer
    92.  
    93.     ' Get protection value.
    94.     If blnProtect = True Then
    95.         intProtect = msoBarNoProtection
    96.     Else
    97.         intProtect = msoBarNoCustomize
    98.     End If
    99.  
    100.     ' Set protection for all command bars.
    101.     For i = 1 To CommandBars.Count
    102.         CommandBars(i).Protection = intProtect
    103.     Next
    104.  
    105. End Sub
    106.  
    107. Sub PasteEnableFalseMessage()
    108.     ' Display message for Paste disabled.
    109.     MsgBox "Paste Disabled" & vbCrLf & vbCrLf & _
    110.            "Paste function is not allowed in this workbook.", _
    111.            vbExclamation
    112. End Sub
    In a workbook:

    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Workbook_Open()
    4.     ' Disable Paste method.
    5.     PasteEnable False
    6. End Sub
    7.  
    8. Private Sub Workbook_Activate()
    9.     ' Disable Paste method.
    10.     PasteEnable False
    11. End Sub
    12.  
    13. Private Sub Workbook_Deactivate()
    14.     ' Enable Paste method.
    15.     PasteEnable True
    16. End Sub
    17.  
    18. Private Sub Workbook_BeforeClose(Cancel As Boolean)
    19.     ' Enable Paste method.
    20.     PasteEnable True
    21. End Sub
    Last edited by WorkHorse; Oct 24th, 2003 at 11:19 PM.

  7. #7

    Thread Starter
    Fanatic Member doofusboy's Avatar
    Join Date
    Apr 2003
    Posts
    526
    Yo WorkHorse, thanks man...........exactly what I needed!
    Do canibals not eat clowns because they taste funny?

  8. #8

    Thread Starter
    Fanatic Member doofusboy's Avatar
    Join Date
    Apr 2003
    Posts
    526
    Ooops, perhaps I spoke too soon?


    WorkHorse, your code seems to cover everything but the Paste button on the toolbar in Excel. Is it possible to disable this button?
    Do canibals not eat clowns because they taste funny?

  9. #9
    Hyperactive Member
    Join Date
    Mar 2002
    Posts
    424
    Should be possible using apis...

    do a search on disabling menu commands in the api section.

  10. #10
    Fanatic Member WorkHorse's Avatar
    Join Date
    Jul 2002
    Location
    Where you live.
    Posts
    591
    Which Paste button on what toolbar? The CommandControlEnable function disables the control on every toolbar in the activeworkbook. The Paste button on the Excel Standard toolbar should be control 22. That one is on the list. It should get every Paste button and menu option that I could find. What version of Excel are you runing? (you don't need no APIs. )

    P.S.: This only disables Paste in the Workbook that has the code. All the other Workbooks should still allow Paste.

  11. #11

    Thread Starter
    Fanatic Member doofusboy's Avatar
    Join Date
    Apr 2003
    Posts
    526
    Sorry for the delay in responding. Was referring to the Paste button on the standard Excel toolbar and I am using Excel 2002.
    Do canibals not eat clowns because they taste funny?

  12. #12
    New Member
    Join Date
    Aug 2007
    Posts
    10

    Talking Re: Disable Paste(?)

    I found the solution! I know this is an old post, but for those people who are reading this in the future.

    Took me a while to find the specific ID numbers, but I found the ones for the paste button in the standard toolbar.

    Use this code for the 'Paste command controls. section instead.

    Code:
    ' Paste command controls.
    CommandControlEnable 22, blnEnabled, True   ' &Paste
    CommandControlEnable 370, blnEnabled, True  ' &Values
    CommandControlEnable 385, blnEnabled, True  ' PasteFunction
    CommandControlEnable 755, blnEnabled, True  ' Paste &Special...
    CommandControlEnable 879, blnEnabled, True  ' &Paste...
    CommandControlEnable 945, blnEnabled, True  ' &Paste
    CommandControlEnable 1956, blnEnabled, True ' Paste Li&nk
    CommandControlEnable 2787, blnEnabled, True ' Paste as &Hyperlink
    CommandControlEnable 5836, blnEnabled, True ' &Formulas
    CommandControlEnable 5837, blnEnabled, True ' No &BordersPaste
    CommandControlEnable 5838, blnEnabled, True ' &Transpose
    CommandControlEnable 6002, blnEnabled, True ' &Paste
    Couldn't have figured this out if it wasn't for WorkHorse's initial code! THNX!!!

  13. #13
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Disable Paste(?)

    Seems it would have been easier to Protect the sheet and add passwording. If they disable macros then all that commandbar work is pointless.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  14. #14
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Disable Paste(?)

    I found this code somewhere on the web...

    This will also take care of the toolbar button...

    Hope this is what you want....

    vb Code:
    1. Private Sub Workbook_Deactivate()
    2. Application.CellDragAndDrop = True
    3. Application.OnKey "^c"
    4. Application.CutCopyMode = False
    5. End Sub
    6.  
    7. Private Sub Workbook_WindowActivate(ByVal Wn As Window)
    8. Application.CutCopyMode = False
    9. Application.OnKey "^c", ""
    10. Application.CellDragAndDrop = False
    11. End Sub
    12.  
    13. Private Sub Workbook_WindowDeactivate(ByVal Wn As Window)
    14. Application.CellDragAndDrop = True
    15. Application.OnKey "^c"
    16. Application.CutCopyMode = False
    17. End Sub
    18.  
    19. Private Sub Workbook_SheetBeforeRightClick(ByVal Sh As Object, ByVal Target As Range, Cancel As Boolean)
    20. Cancel = True
    21. MsgBox "Right click menu deactivated." & vbCrLf & _
    22. "Cannot copy or ''drag & drop''.", 16, "For this workbook:"
    23. End Sub
    24.  
    25. Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
    26. Application.CutCopyMode = False
    27. End Sub
    28.  
    29. Private Sub Workbook_SheetActivate(ByVal Sh As Object)
    30. Application.OnKey "^c", ""
    31. Application.CellDragAndDrop = False
    32. Application.CutCopyMode = False
    33. End Sub
    34.  
    35. Private Sub Workbook_SheetDeactivate(ByVal Sh As Object)
    36. Application.CutCopyMode = False
    37. End Sub
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  15. #15
    New Member
    Join Date
    Aug 2007
    Posts
    10

    Re: Disable Paste(?)

    Quote Originally Posted by RobDog888
    Seems it would have been easier to Protect the sheet and add passwording. If they disable macros then all that commandbar work is pointless.
    Nope, this code runs even when your workbook, vba project, and sheets are protected. My sheet is protected beyond the extent it needs to be, but thats just me. My sheet has only like 15 lines of real useful code, the rest of the 300+ lines are for protection and formating and all the other stuff.

    *edit* haha, I miss read what you said, yes if they disable the macros, it would be kinda useless... crap! now what am i suppose to do Well... if they do eliminate the macros, my sheet would be uterly useless to them then.

    Also, protecting the sheet does not always work (but locking every cell would work). But if you lock all the cells, then ppl can't input values or data or whatever in the excel sheet, and that is what some people want to do. Basically let the people use the tool, but not mess with it or screw any of it up.

    I needed the "prevent paste feature" because some cells (input cells to the formulas etc) are hidden, but not locked, so people could have pasted some weird formated cell and it would have eliminated all my conditional formating, regular formating i had (center, font size, BORDERS, etc), also my validation (pick from list).

    People always say, "O, just protect the sheet and your fine." NOT THE CASE!!!!!!! For some people anyways...


    @ koolsid

    I tried using that cutcopymode thing, but it never did anything. I would have that line of code, but I could still copy and paste and all that fun stuff.

    Application.CutCopyMode = False

    That line of code would be in the worksheet_activate sub, etc. Maybe its just cause I don't fully understand what the "CutCopyMode" means. I thought it was suppose to prevent the user from copying, cuting, AND pasting in excel.
    Last edited by byyu; Nov 9th, 2007 at 03:45 PM.

  16. #16
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Disable Paste(?)

    i think cutcopymode is when you copy a range, the range has active border till you hit esc or return or copy something else

    maybe you could lock every cell then have a userform to for user interface
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width