PC Review


Reply
Thread Tools Rate Thread

Disable "Cut&Paste" and "Drag&Drop"

 
 
johnny
Guest
Posts: n/a
 
      28th Sep 2008
I want to disable this two function when I open a workbook. I've tryed both
this code:
Private Sub Workbook_Open()
Application.CellDragAndDrop = False
Application.CutCopyMode = False
End Sub

and

Private Sub WorkbookOpen(ByVal Wb As Workbook)
Application.CellDragAndDrop = False
Application.CutCopyMode = False
End Sub
but no one of them will work the right way. I've put it both in a module, in
"This workbook" and in the actuall sheet.
Anybody got an idea whats wrong?

 
Reply With Quote
 
 
 
 
Ken Johnson
Guest
Posts: n/a
 
      28th Sep 2008
This works by disabling the Menu commands and keyboard shortcuts.
I got the idea from code posted by Jim Rech which disabled Cut, Copy
and Cell Drag and Drop.
I extended it to also disable Paste and Paste Special.
It works between different Excel sessions so that something copied in
a different session cannot be pasted in the protected session.
The three Subs are Private so that they can only be run from within
the VBA Editor.
To turn Cut, Copy, Cell Drag and Drop, Paste and Paste Special... off
run CutsOff().
Run CutsOn() to turn them all back on.

Private Sub CutsOff()
AllowCuts False
End Sub

Private Sub CutsOn()
AllowCuts True
End Sub

Private Sub AllowCuts(bEnable As Boolean)
Dim oCtls As CommandBarControls, oCtl As CommandBarControl
Set oCtls = CommandBars.FindControls(ID:=21) 'Cut
If Not oCtls Is Nothing Then
For Each oCtl In oCtls
oCtl.Enabled = bEnable
Next
End If
Set oCtls = CommandBars.FindControls(ID:=19) 'Copy
If Not oCtls Is Nothing Then
For Each oCtl In oCtls
oCtl.Enabled = bEnable
Next
End If
Set oCtls = CommandBars.FindControls(ID:=6002) 'Paste button
If Not oCtls Is Nothing Then
For Each oCtl In oCtls
oCtl.Enabled = bEnable
Next
End If
Set oCtls = CommandBars.FindControls(ID:=22) 'Paste in Edit menu
If Not oCtls Is Nothing Then
For Each oCtl In oCtls
oCtl.Enabled = bEnable
Next
End If
Set oCtls = CommandBars.FindControls(ID:=755) 'Paste Special...
If Not oCtls Is Nothing Then
For Each oCtl In oCtls
oCtl.Enabled = bEnable
Next
End If
''Disable Tools, Options so D&D cannot be restored
Set oCtls = CommandBars.FindControls(ID:=522)
If Not oCtls Is Nothing Then
For Each oCtl In oCtls
oCtl.Enabled = bEnable
Next
End If
With Application
.CellDragAndDrop = bEnable
If bEnable Then
.OnKey "^x"
.OnKey "+{Del}"
.OnKey "^c"
.OnKey "^v"
Else
.OnKey "^x", ""
.OnKey "+{Del}", ""
.OnKey "^c", ""
.OnKey "^v", ""
End If

End With
End Sub

Special thanks to Jim Rech.

Ken Johnson
 
Reply With Quote
 
Tom Hutchins
Guest
Posts: n/a
 
      28th Sep 2008
I htink you are disabling cut/copy/drag temporarily, but it is getting
re-enabled by subsequent operations. Instead of the Workbook_Open event, use
the Workbook_SheetSelectionChange event. Every time any cell is selected,
cut/copy/drag is disabled. This event code needs to be in the ThisWorkbook
module:

Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target
As Range)
Application.CutCopyMode = False
Application.CellDragAndDrop = False
End Sub

Hope this helps,

Hutch

"johnny" wrote:

> I want to disable this two function when I open a workbook. I've tryed both
> this code:
> Private Sub Workbook_Open()
> Application.CellDragAndDrop = False
> Application.CutCopyMode = False
> End Sub
>
> and
>
> Private Sub WorkbookOpen(ByVal Wb As Workbook)
> Application.CellDragAndDrop = False
> Application.CutCopyMode = False
> End Sub
> but no one of them will work the right way. I've put it both in a module, in
> "This workbook" and in the actuall sheet.
> Anybody got an idea whats wrong?
>

 
Reply With Quote
 
johnny
Guest
Posts: n/a
 
      28th Sep 2008
This was exatly what I needed. Thank you Tom! It works perfectly.

Tom Hutchins skrev:

> I htink you are disabling cut/copy/drag temporarily, but it is getting
> re-enabled by subsequent operations. Instead of the Workbook_Open event, use
> the Workbook_SheetSelectionChange event. Every time any cell is selected,
> cut/copy/drag is disabled. This event code needs to be in the ThisWorkbook
> module:
>
> Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target
> As Range)
> Application.CutCopyMode = False
> Application.CellDragAndDrop = False
> End Sub
>
> Hope this helps,
>
> Hutch
>
> "johnny" wrote:
>
> > I want to disable this two function when I open a workbook. I've tryed both
> > this code:
> > Private Sub Workbook_Open()
> > Application.CellDragAndDrop = False
> > Application.CutCopyMode = False
> > End Sub
> >
> > and
> >
> > Private Sub WorkbookOpen(ByVal Wb As Workbook)
> > Application.CellDragAndDrop = False
> > Application.CutCopyMode = False
> > End Sub
> > but no one of them will work the right way. I've put it both in a module, in
> > "This workbook" and in the actuall sheet.
> > Anybody got an idea whats wrong?
> >

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Disable "Cut" and "Drag & Drop" but not copy CraigKer Microsoft Excel Programming 0 6th Mar 2009 10:11 PM
how can I disable "cutting cells" and "drag and drop "in excel ? mwoody Microsoft Excel Worksheet Functions 4 25th Aug 2008 03:53 PM
Copy/paste or drag/drop to desktop "Access Denied" =?Utf-8?B?RWQgRmxlY2tv?= Windows XP General 1 22nd Mar 2006 11:42 AM
"Date Created" not changing on drag and drop to a folder, but changes on a copy paste Jenny Windows XP General 1 30th Jul 2004 11:01 PM
Make Excel's "grab and drag" behave like cut and paste "values only" ? tur13o Microsoft Excel Misc 5 14th Nov 2003 01:07 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:51 AM.