thenaturalprogrammer
New Member
- Joined
- May 23, 2013
- Messages
- 34
I have some code to trap the Cut/Copy/Paste/Paste Special commands. When I enable trapping, why does it add an "Add-In" tab with the commands on the Ribbon Bar? It's not too big a deal, but it's a little irritating.
Code:
Public Function initiliazeCommandTrapping(Optional ByVal setToEnabled As Boolean = True)
Const fnName = "initiliazeCommandTrapping"
Dim oCtrl As Office.CommandBarControl
If Not debugMode Then On Error GoTo err_Catch
'if not currently cutting/copying...
If Application.CutCopyMode = 0 Then Application.CellDragAndDrop = Not setToEnabled
If setToEnabled Then
Application.OnKey "^x", "'trapCutCommand ""OnKey""'"
Application.OnKey "^c", "'trapCopyCommand ""OnKey""'"
Application.OnKey "^v", "'trapPasteCommand ""OnKey""'"
'Cut
For Each oCtrl In Application.CommandBars.FindControls(ID:=21)
oCtrl.OnAction = "'trapCutCommand """ & oCtrl.Caption & """'"
Next oCtrl
'Copy
For Each oCtrl In Application.CommandBars.FindControls(ID:=19)
oCtrl.OnAction = "'trapCopyCommand """ & oCtrl.Caption & """'"
Next oCtrl
'Paste
For Each oCtrl In Application.CommandBars.FindControls(ID:=22)
oCtrl.OnAction = "'trapPasteCommand """ & oCtrl.Caption & """'"
Next oCtrl
'Paste Special
For Each oCtrl In Application.CommandBars.FindControls(ID:=755)
oCtrl.OnAction = "'trapPasteSpecialCommand """ & oCtrl.Caption & """'"
Next oCtrl
Else
'RELEASE COMMAND TRAPPING
Application.OnKey "^v"
Application.OnKey "^x"
Application.OnKey "^c"
'Cut
For Each oCtrl In Application.CommandBars.FindControls(ID:=21)
oCtrl.OnAction = ""
Next oCtrl
'Copy
For Each oCtrl In Application.CommandBars.FindControls(ID:=19)
oCtrl.OnAction = ""
Next oCtrl
'Paste
For Each oCtrl In Application.CommandBars.FindControls(ID:=22)
oCtrl.OnAction = ""
Next oCtrl
'Paste Special
For Each oCtrl In Application.CommandBars.FindControls(ID:=755)
oCtrl.OnAction = ""
Next oCtrl
End If
'Only run the resetTheRibbonBar() Function if using Excel 2007+ (12+); not tested on 2010 or 2013
'Could be set to a range, if necessary, such as "12-13"
execFn "resetTheRibbonBar", 12 'for ribbon bar commands (set separately)
'also requires the Module mod2007
' If Application.Version >= 12 Then resetTheRibbonBar 'for ribbon bar commands (set separately)
initiliazeCommandTrapping = "Success probable " & IIf(setToEnabled, "enabling", "disabling") & "."
finish_Function: If Not debugMode Then On Error Resume Next
If Not oCtrl Is Nothing Then Set oCtrl = Nothing
Exit Function
err_Catch:
If Err.Number = escErrorNumber Then
escapeErrorHandler fnName
Resume Next
End If
initiliazeCommandTrapping = errorMessage(fnName, Err.Description, Err.Number)
Resume finish_Function
Exit Function
End Function