Option Explicit
'\\Public Declarations...
Public oObjectBeingCutorCopied1 As Object
Public oObjectBeingCutorCopied2 As Object
Public bCutting As Boolean
Public bCopying As Boolean
'\\Private Declarations...
Private Declare Function SetWindowLong Lib "user32" _
Alias "SetWindowLongA" _
(ByVal hwnd As Long, _
ByVal nIndex As Long, _
ByVal dwNewLong As Long) As Long
Private Declare Function CallWindowProc Lib "user32" _
Alias "CallWindowProcA" _
(ByVal lpPrevWndFunc As Long, _
ByVal hwnd As Long, _
ByVal Msg As Long, _
ByVal wParam As Long, _
ByVal lParam As Long) As Long
Private Declare Function FindWindow Lib "user32" _
Alias "FindWindowA" _
(ByVal lpClassName As String, _
ByVal lpWindowName As String) As Long
Private Declare Function OpenClipboard Lib "user32" _
(ByVal hwnd As Long) As Long
Private Declare Function CloseClipboard Lib "user32" () As Long
Private Declare Function EmptyClipboard Lib "user32" () As Long
Private Declare Function GetActiveWindow Lib "user32" () As Long
Private Const GWL_WNDPROC As Long = (-4)
Private Const WM_DESTROYCLIPBOARD As Long = &H307
Private lhwnd As Long
Private lPrevProc As Long
Private bSubClassed As Boolean
'\\Public Subs....
Public Sub StartCutCopyMonitor()
Call SubClassXL
End Sub
Public Sub StopCutCopyMonitor()
Call RemoveSubClassXL
End Sub
'\\Private Subs....
Private Sub SubClassXL()
If Not bSubClassed Then
lhwnd = FindWindow("CLIPBRDWNDCLASS", vbNullString)
OpenClipboard 0
EmptyClipboard
CloseClipboard
lPrevProc = SetWindowLong _
(lhwnd, GWL_WNDPROC, AddressOf WindowProc)
bSubClassed = True
End If
End Sub
Private Sub RemoveSubClassXL()
SetWindowLong lhwnd, GWL_WNDPROC, lPrevProc
bSubClassed = False
Set oObjectBeingCutorCopied1 = Nothing
Set oObjectBeingCutorCopied2 = Nothing
End Sub
Private Function WindowProc _
(ByVal hwnd As Long, ByVal uMsg As Long, _
ByVal wParam As Long, ByVal lParam As Long) As Long
On Error Resume Next
Select Case uMsg
Case Is = WM_DESTROYCLIPBOARD
If GetActiveWindow = _
FindWindow("XLMAIN", Application.Caption) Then
If Application.CutCopyMode = xlCut Then _
bCutting = True: bCopying = False
If Application.CutCopyMode = xlCopy Then _
bCopying = True: bCutting = False
If ActiveWorkbook Is ThisWorkbook Then
Set oObjectBeingCutorCopied1 = Selection
Else
Set oObjectBeingCutorCopied2 = Selection
End If
End If
End Select
WindowProc = CallWindowProc _
(lPrevProc, hwnd, uMsg, wParam, lParam)
End Function