Jon von der Heyden
MrExcel MVP, Moderator
- Joined
- Apr 6, 2004
- Messages
- 10,912
- Office Version
- 365
- Platform
- Windows
Hi All
I thought I would share a small solution to a problem that has niggled me for a while. I hope it helps someone and perhaps indeed there is a better approach? At the very least I know I can come back here in the future should I need it again.
I want to stop users from using paste that will overwrite my formats and validation rules. However I do not want to prevent paste, not from Excel contents nor from other applications contents. The following code will:
1. Only effect the change where global constant g_blnDESIGN_MODE = False
2. Place the contents of the Office clipboard into the Windows clipboard
3. Note the contents of the Windows clipboard, as Text
4. Clear the clipboard (remove formats)
5. Put the string back into the clipboard
6. Clear the Office clipboard
I call the routine each time the Workbook is activated, a sheet is activated or a selection change occurs:
Note - it requires a reference to MSForms library.
I thought I would share a small solution to a problem that has niggled me for a while. I hope it helps someone and perhaps indeed there is a better approach? At the very least I know I can come back here in the future should I need it again.
I want to stop users from using paste that will overwrite my formats and validation rules. However I do not want to prevent paste, not from Excel contents nor from other applications contents. The following code will:
1. Only effect the change where global constant g_blnDESIGN_MODE = False
2. Place the contents of the Office clipboard into the Windows clipboard
3. Note the contents of the Windows clipboard, as Text
4. Clear the clipboard (remove formats)
5. Put the string back into the clipboard
6. Clear the Office clipboard
Code:
Public Sub NoPasteAll()
Dim dobClipboard As MSForms.DataObject
Dim strData As String
Dim blnEvents As Boolean
If g_blnDESIGN_MODE Then Exit Sub
With Application
blnEvents = .EnableEvents
.EnableEvents = False
End With
Set dobClipboard = New MSForms.DataObject
On Error Resume Next
With dobClipboard
Call .GetFromClipboard
strData = .GetText
Call .Clear
Call .SetText(strData)
Application.CutCopyMode = False
Call .PutInClipboard
End With
On Error GoTo 0
Application.EnableEvents = blnEvents
Set dobClipboard = Nothing
End Sub
I call the routine each time the Workbook is activated, a sheet is activated or a selection change occurs:
Code:
Private Sub Workbook_Activate()
Call NoPasteAll
End Sub
Private Sub Workbook_SheetActivate(ByVal Sh As Object)
Call NoPasteAll
End Sub
Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
Call NoPasteAll
End Sub
Note - it requires a reference to MSForms library.
Last edited: