|
-
Oct 23rd, 2003, 08:50 AM
#1
Thread Starter
Fanatic Member
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? 
-
Oct 23rd, 2003, 09:22 AM
#2
Lively Member
Doof,
Only thing I can think of is to block/capture the Crtl - V sequence ...
-
Oct 23rd, 2003, 09:35 AM
#3
Thread Starter
Fanatic Member
Thought of that, but what about pasting from toolbar, or drag-drop?
Do canibals not eat clowns because they taste funny? 
-
Oct 23rd, 2003, 09:32 PM
#4
New Member
Should you protect the speadsheet incase you only want to view the spreadsheet ?
-
Oct 24th, 2003, 05:23 AM
#5
Thread Starter
Fanatic Member
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? 
-
Oct 24th, 2003, 11:14 PM
#6
Fanatic Member
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:
Option Explicit
Sub PasteEnable(ByVal blnEnabled As Boolean)
On Error Resume Next
' Insert command controls.
CommandControlEnable 248, blnEnabled, True ' Insert Copied C&ells...
CommandControlEnable 262, blnEnabled, True ' Insert Copied C&ells...
CommandControlEnable 272, blnEnabled, True ' Insert Copied C&ells...
CommandControlEnable 282, blnEnabled, True ' Insert Copied C&ells...
CommandControlEnable 295, blnEnabled, True ' Copied C&ells...
CommandControlEnable 299, blnEnabled, True ' Insert Copied C&ells...
CommandControlEnable 316, blnEnabled, True ' Insert Copied C&ells...
CommandControlEnable 340, blnEnabled, True ' Insert Copied C&ells...
CommandControlEnable 454, blnEnabled, True ' Insert Copied C&ells...
CommandControlEnable 468, blnEnabled, True ' Insert Copied C&ells...
CommandControlEnable 3184, blnEnabled, True ' Copied C&ells...
CommandControlEnable 3185, blnEnabled, True ' Insert Copied C&ells...
CommandControlEnable 3187, blnEnabled, True ' Insert Copied C&ells
' Paste command controls.
CommandControlEnable 22, blnEnabled, True ' &Paste
CommandControlEnable 755, blnEnabled, True ' Paste &Special...
CommandControlEnable 879, blnEnabled, True ' &Paste...
CommandControlEnable 945, blnEnabled, True ' &Paste
CommandControlEnable 2787, blnEnabled, True ' Paste as &Hyperlink
' Paste All on Clipboard toolbar cannot be disabled.
' Disable/enable toolbar completely.
'CommandControlEnable 3633, blnEnabled, "Paste A&ll", "Paste A&ll Disabled"
CommandBars("Clipboard").Enabled = blnEnabled
' Set protection from customization for all toolbars.
CommandBarsProtectionEnable blnEnabled
' Paste keyboard functions.
If blnEnabled = True Then
Application.OnKey "^v"
Application.OnKey "+{INSERT}"
Else
Application.OnKey "^v", "PasteEnableFalseMessage"
Application.OnKey "+{INSERT}", "PasteEnableFalseMessage"
End If
End Sub
Sub CommandControlEnable(ByVal lngCommandId As Long, _
ByVal blnEnabled As Boolean, _
Optional ByVal blnChangeCaption As Boolean)
On Error Resume Next
Dim ctlControls As CommandBarControls
Dim ctlControl As CommandBarControl
Dim strCaption As String
' Get all matching command controls.
Set ctlControls = CommandBars.FindControls(ID:=lngCommandId)
If Not ctlControls Is Nothing Then
' Get caption.
strCaption = ctlControls.Item(1).Caption
If blnChangeCaption = True Then
If blnEnabled = True Then
If Right$(strCaption, 9) = " Disabled" Then
strCaption = Left$(strCaption, Len(strCaption) - 9)
End If
Else
If Not Right$(strCaption, 9) = " Disabled" Then
strCaption = strCaption & " Disabled"
End If
End If
End If
' Toggle enabled and set caption.
For Each ctlControl In ctlControls
ctlControl.Enabled = blnEnabled
ctlControl.Caption = strCaption
Next
End If
End Sub
Sub CommandBarsProtectionEnable(ByVal blnProtect As Boolean)
On Error Resume Next
Dim intProtect As Integer
Dim i As Integer
' Get protection value.
If blnProtect = True Then
intProtect = msoBarNoProtection
Else
intProtect = msoBarNoCustomize
End If
' Set protection for all command bars.
For i = 1 To CommandBars.Count
CommandBars(i).Protection = intProtect
Next
End Sub
Sub PasteEnableFalseMessage()
' Display message for Paste disabled.
MsgBox "Paste Disabled" & vbCrLf & vbCrLf & _
"Paste function is not allowed in this workbook.", _
vbExclamation
End Sub
In a workbook:
VB Code:
Option Explicit
Private Sub Workbook_Open()
' Disable Paste method.
PasteEnable False
End Sub
Private Sub Workbook_Activate()
' Disable Paste method.
PasteEnable False
End Sub
Private Sub Workbook_Deactivate()
' Enable Paste method.
PasteEnable True
End Sub
Private Sub Workbook_BeforeClose(Cancel As Boolean)
' Enable Paste method.
PasteEnable True
End Sub
Last edited by WorkHorse; Oct 24th, 2003 at 11:19 PM.
-
Oct 27th, 2003, 09:20 AM
#7
Thread Starter
Fanatic Member
Yo WorkHorse, thanks man...........exactly what I needed!
Do canibals not eat clowns because they taste funny? 
-
Oct 27th, 2003, 10:03 AM
#8
Thread Starter
Fanatic Member
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? 
-
Oct 27th, 2003, 12:52 PM
#9
Hyperactive Member
Should be possible using apis...
do a search on disabling menu commands in the api section.
-
Oct 27th, 2003, 07:32 PM
#10
Fanatic Member
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.
-
Nov 3rd, 2003, 06:28 AM
#11
Thread Starter
Fanatic Member
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? 
-
Nov 9th, 2007, 01:47 PM
#12
New Member
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!!!
-
Nov 9th, 2007, 02:19 PM
#13
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Nov 9th, 2007, 02:26 PM
#14
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:
Private Sub Workbook_Deactivate()
Application.CellDragAndDrop = True
Application.OnKey "^c"
Application.CutCopyMode = False
End Sub
Private Sub Workbook_WindowActivate(ByVal Wn As Window)
Application.CutCopyMode = False
Application.OnKey "^c", ""
Application.CellDragAndDrop = False
End Sub
Private Sub Workbook_WindowDeactivate(ByVal Wn As Window)
Application.CellDragAndDrop = True
Application.OnKey "^c"
Application.CutCopyMode = False
End Sub
Private Sub Workbook_SheetBeforeRightClick(ByVal Sh As Object, ByVal Target As Range, Cancel As Boolean)
Cancel = True
MsgBox "Right click menu deactivated." & vbCrLf & _
"Cannot copy or ''drag & drop''.", 16, "For this workbook:"
End Sub
Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
Application.CutCopyMode = False
End Sub
Private Sub Workbook_SheetActivate(ByVal Sh As Object)
Application.OnKey "^c", ""
Application.CellDragAndDrop = False
Application.CutCopyMode = False
End Sub
Private Sub Workbook_SheetDeactivate(ByVal Sh As Object)
Application.CutCopyMode = False
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
-
Nov 9th, 2007, 03:31 PM
#15
New Member
Re: Disable Paste(?)
 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.
-
Nov 9th, 2007, 04:16 PM
#16
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|