Excel VBA to activate when print the screen

Biz

Well-known Member
Joined
May 18, 2009
Messages
1,773
Office Version
  1. 2021
Platform
  1. Windows
Hi
I am trying to run vba clear clipboard. Not sure how check if Application.SendKeys "(%{1068})", is pressed.<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>
<o:p> </o:p>
<o:p>Any help or suggestions would be appreciated.</o:p>
<o:p></o:p>
Kind Regards,<o:p></o:p>
<o:p> </o:p>
Biz
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
Does this work?

Code:
Application.CutCopyMode = False

Hi Vog

I am currently trying to disable Print Screen. Not sure how to activate the code when Print Screen is done to clear clipboard.

Code:
Option Explicit
Public Declare Function OpenClipboard Lib "user32" (ByVal hwnd As Long) As Long
Public Declare Function EmptyClipboard Lib "user32" () As Long
Public Declare Function CloseClipboard Lib "user32" () As Long
 
Public Function ClearClipboard()
    OpenClipboard (0&)
    EmptyClipboard
    CloseClipboard
End Function
 
Sub ccc()
    Call ClearClipboard
End Sub
Sub PrintTheScreen()
Application.SendKeys "(%{1068})", False
Call ccc
DoEvents
End Sub

Biz
 
Upvote 0
Hi.

If you just want to disable the Print Screen key then you could use the RegisterHotKey API as follows :

Workbook Example:

Place this in a Standard module :

Code:
Option Explicit
 
Private Declare Function RegisterHotKey Lib "user32" _
(ByVal hWnd As Long, _
ByVal id As Long, _
ByVal fsModifiers As Long, _
ByVal vk As Long) As Long
 
Private Declare Function UnregisterHotKey Lib "user32" _
(ByVal hWnd As Long, _
ByVal id As Long) As Long
 
Private Const VK_SNAPSHOT = &H2C
Private Const MOD_ALT = &H1
Private Const MOD_CONTROL = &H2
Private Const MOD_SHIFT = &H4
 
Private lPrint As Long
Private lAltPrint As Long
Private lCtrlPrint As Long
Private lShiftPrint As Long
 
Sub HookPrintKey()
 
    'set the hotkeys IDs
    lPrint = 1
    lAltPrint = 2
    lCtrlPrint = 3
    lShiftPrint = 4
 
    'Register the hotkeys
    Call RegisterHotKey(0, lPrint, 0&, VK_SNAPSHOT)
    Call RegisterHotKey(0, lAltPrint, MOD_ALT, VK_SNAPSHOT)
    Call RegisterHotKey(0, lCtrlPrint, MOD_CONTROL, VK_SNAPSHOT)
    Call RegisterHotKey(0, lShiftPrint, MOD_SHIFT, VK_SNAPSHOT)
 
End Sub
 
Sub UnhookPrintKey()
 
    'Unregister the hotkeys
    Call UnregisterHotKey(0, lPrint)
    Call UnregisterHotKey(0, lAltPrint)
    Call UnregisterHotKey(0, lCtrlPrint)
    Call UnregisterHotKey(0, lShiftPrint)
 
End Sub

Place this in the Workbook module :

Code:
Option Explicit
 
Private Sub Workbook_Open()
 
    Call HookPrintKey
 
End Sub
 
Private Sub Workbook_BeforeClose(Cancel As Boolean)
 
    Call UnhookPrintKey
 
End Sub



If however you want to run some code ( Such as clearing the clipboard as requested ) upon pressing the Print Screen Key then it is more difficult and you will need to use a low level hook as follows :

Workbook Example:

Place this in a Standard module .

Code:
'Jaafar Tribak 20/10/2010
'code that traps the pressing of the
'Print Screen Key and optionally
'clears the clipboard.
Option Explicit
 
 
Private Type KBDLLHOOKSTRUCT
    vkCode As Long
    scanCode As Long
    flags As Long
    time As Long
    dwExtraInfo As Long
End Type
 
Private Declare Function SetWindowsHookEx Lib _
"user32" Alias "SetWindowsHookExA" _
(ByVal idHook As Long, _
ByVal lpfn As Long, _
ByVal hmod As Long, _
ByVal dwThreadId As Long) As Long
 
Private Declare Function CallNextHookEx Lib "user32" _
(ByVal hHook As Long, _
ByVal nCode As Long, _
ByVal wParam As Long, _
lParam As Any) As Long
 
Private Declare Function UnhookWindowsHookEx Lib _
"user32" (ByVal hHook As Long) As Long
 
Private Declare Function GetWindowLong Lib "user32" _
Alias "GetWindowLongA" _
(ByVal hwnd As Long, _
ByVal nIndex 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 FindWindowEx Lib "user32" _
Alias "FindWindowExA" _
(ByVal hWnd1 As Long, _
ByVal hWnd2 As Long, _
ByVal lpsz1 As String, _
ByVal lpsz2 As String) As Long
 
Private Declare Function IsWindow Lib "user32" _
(ByVal hwnd As Long) As Long
 
Private Declare Function SetTimer Lib "user32" _
(ByVal hwnd As Long, _
ByVal nIDEvent As Long, _
ByVal uElapse As Long, _
ByVal lpTimerFunc As Long) As Long
 
Private Declare Function KillTimer Lib "user32" ( _
ByVal hwnd As Long, _
ByVal uIDEvent As Long) As Long
 
Private Declare Function OpenClipboard Lib "user32" _
(ByVal hwnd As Long) As Long
 
Private Declare Function EmptyClipboard Lib "user32" () As Long
 
Private Declare Function CloseClipboard Lib "user32" () As Long
 
Private Const WH_KEYBOARD_LL = 13
Private Const HC_ACTION = 0
Private Const GWL_HINSTANCE = (-6)
Private Const VK_SNAPSHOT = &H2C
 
Private blnHookEnabled As Boolean
Private hhkLowLevelKybd As Long
Private lWBhwnd As Long
Private lTimerID As Long
Private oNewApp As Application
 
'-------------------
'Public routines .
'-------------------
Public Sub SetHook()
 
    Dim owb As Workbook
 
    'Create an invisible xl instance
    'to run the keyboard hook callback.
    'this is to avoid accidental VBIDE crash.
    Set oNewApp = New Application
 
    oNewApp.EnableEvents = False
 
    Set owb = oNewApp.Workbooks.Open _
    (ThisWorkbook.FullName, ReadOnly:=True)
 
    oNewApp.Run "Hook_KeyBoard", ThisWorkbook.FullName
 
End Sub
 
Public Sub RemoveHook()
 
    'Proper cleanup.
    If Not oNewApp Is Nothing Then
        oNewApp.Run "Unhook_KeyBoard"
        oNewApp.Quit
    End If
 
End Sub
 
Public Sub Hook_KeyBoard(WbPathName As String)
 
    Dim lXLhwnd, lXLHinstance As Long
    Dim owb As Workbook
 
    'get a temp pointer to calling WB.
    Set owb = GetObject(WbPathName)
 
    'store the hwnd of the calling WB.
    lWBhwnd = FindWindowEx _
    (owb.Parent.hwnd, 0, "XLDESK", vbNullString)
    lWBhwnd = FindWindowEx(lWBhwnd, 0, "EXCEL7", owb.Name)
 
    'release ref to calling WB.
    Set owb = Nothing
 
    'hook the keyboard system wide.
    lXLhwnd = FindWindow("XLMAIN", Application.Caption)
    lXLHinstance = GetWindowLong(lXLhwnd, GWL_HINSTANCE)
 
    If blnHookEnabled = False Then
        hhkLowLevelKybd = SetWindowsHookEx _
        (WH_KEYBOARD_LL, _
        AddressOf LowLevelKeyboardProc, lXLHinstance, 0)
        blnHookEnabled = True
        'unhook the keyboard in case it wasn't properly
        'unhooked in the Before_Close event.
        lTimerID = SetTimer(0, 0, 1, AddressOf MonitorWorkBook)
    End If
 
End Sub
 
Public Sub Unhook_KeyBoard()
 
    If hhkLowLevelKybd <> 0 Then _
    UnhookWindowsHookEx hhkLowLevelKybd
    hhkLowLevelKybd = 0
    blnHookEnabled = False
 
End Sub
 
'------------------
'Private routines.
'------------------
Private Function LowLevelKeyboardProc _
(ByVal nCode As Long, ByVal wParam As Long, _
ByRef lParam As KBDLLHOOKSTRUCT) As Long
 
    If nCode = HC_ACTION Then
        'never process the Print Key.
        If lParam.vkCode = VK_SNAPSHOT Then
            LowLevelKeyboardProc = -1
            SetTimer FindWindow("XLMAIN", Application.Caption) _
            , 0, 1, AddressOf ClearTheClipboard
            Exit Function
        End If
    End If
 
    LowLevelKeyboardProc = _
    CallNextHookEx(0, nCode, wParam, ByVal lParam)
 
End Function
 
Private Sub ClearTheClipboard()
 
    'display msgbox only once.
 
    KillTimer FindWindow("XLMAIN", Application.Caption), 0
    If FindWindowEx(0, 0, "#32770", _
    "Microsoft Excel") Then Exit Sub
    MsgBox "The Print Key is disabled.", _
    vbSystemModal + vbInformation
 
    'Optionally clear the clipboard.
    OpenClipboard (0&)
    EmptyClipboard
    CloseClipboard
 
    [COLOR=seagreen][B]'run any other code here...[/B][/COLOR]
 
End Sub
 
Private Sub MonitorWorkBook()
 
    'safety net in case the calling WB didn't
    'unhook the keyboard before closing.
    If IsWindow(lWBhwnd) = 0 Then
        DoEvents
        KillTimer 0, lTimerID
        Unhook_KeyBoard
        Application.Quit
    End If
 
End Sub

Place this code in the Workbook module :

Code:
Option Explicit
 
Private Sub Workbook_Open()
 
    Call SetHook
 
End Sub
 
Private Sub Workbook_BeforeClose(Cancel As Boolean)
 
    Call RemoveHook
 
End Sub
 
Upvote 0
Hi.

If you just want to disable the Print Screen key then you could use the RegisterHotKey API as follows :

Workbook Example:

Place this in a Standard module :

Code:
Option Explicit
 
Private Declare Function RegisterHotKey Lib "user32" _
(ByVal hWnd As Long, _
ByVal id As Long, _
ByVal fsModifiers As Long, _
ByVal vk As Long) As Long
 
Private Declare Function UnregisterHotKey Lib "user32" _
(ByVal hWnd As Long, _
ByVal id As Long) As Long
 
Private Const VK_SNAPSHOT = &H2C
Private Const MOD_ALT = &H1
Private Const MOD_CONTROL = &H2
Private Const MOD_SHIFT = &H4
 
Private lPrint As Long
Private lAltPrint As Long
Private lCtrlPrint As Long
Private lShiftPrint As Long
 
Sub HookPrintKey()
 
    'set the hotkeys IDs
    lPrint = 1
    lAltPrint = 2
    lCtrlPrint = 3
    lShiftPrint = 4
 
    'Register the hotkeys
    Call RegisterHotKey(0, lPrint, 0&, VK_SNAPSHOT)
    Call RegisterHotKey(0, lAltPrint, MOD_ALT, VK_SNAPSHOT)
    Call RegisterHotKey(0, lCtrlPrint, MOD_CONTROL, VK_SNAPSHOT)
    Call RegisterHotKey(0, lShiftPrint, MOD_SHIFT, VK_SNAPSHOT)
 
End Sub
 
Sub UnhookPrintKey()
 
    'Unregister the hotkeys
    Call UnregisterHotKey(0, lPrint)
    Call UnregisterHotKey(0, lAltPrint)
    Call UnregisterHotKey(0, lCtrlPrint)
    Call UnregisterHotKey(0, lShiftPrint)
 
End Sub

Place this in the Workbook module :

Code:
Option Explicit
 
Private Sub Workbook_Open()
 
    Call HookPrintKey
 
End Sub
 
Private Sub Workbook_BeforeClose(Cancel As Boolean)
 
    Call UnhookPrintKey
 
End Sub



If however you want to run some code ( Such as clearing the clipboard as requested ) upon pressing the Print Screen Key then it is more difficult and you will need to use a low level hook as follows :

Workbook Example:

Place this in a Standard module .

Code:
'Jaafar Tribak 20/10/2010
'code that traps the pressing of the
'Print Screen Key and optionally
'clears the clipboard.
Option Explicit
 
 
Private Type KBDLLHOOKSTRUCT
    vkCode As Long
    scanCode As Long
    flags As Long
    time As Long
    dwExtraInfo As Long
End Type
 
Private Declare Function SetWindowsHookEx Lib _
"user32" Alias "SetWindowsHookExA" _
(ByVal idHook As Long, _
ByVal lpfn As Long, _
ByVal hmod As Long, _
ByVal dwThreadId As Long) As Long
 
Private Declare Function CallNextHookEx Lib "user32" _
(ByVal hHook As Long, _
ByVal nCode As Long, _
ByVal wParam As Long, _
lParam As Any) As Long
 
Private Declare Function UnhookWindowsHookEx Lib _
"user32" (ByVal hHook As Long) As Long
 
Private Declare Function GetWindowLong Lib "user32" _
Alias "GetWindowLongA" _
(ByVal hwnd As Long, _
ByVal nIndex 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 FindWindowEx Lib "user32" _
Alias "FindWindowExA" _
(ByVal hWnd1 As Long, _
ByVal hWnd2 As Long, _
ByVal lpsz1 As String, _
ByVal lpsz2 As String) As Long
 
Private Declare Function IsWindow Lib "user32" _
(ByVal hwnd As Long) As Long
 
Private Declare Function SetTimer Lib "user32" _
(ByVal hwnd As Long, _
ByVal nIDEvent As Long, _
ByVal uElapse As Long, _
ByVal lpTimerFunc As Long) As Long
 
Private Declare Function KillTimer Lib "user32" ( _
ByVal hwnd As Long, _
ByVal uIDEvent As Long) As Long
 
Private Declare Function OpenClipboard Lib "user32" _
(ByVal hwnd As Long) As Long
 
Private Declare Function EmptyClipboard Lib "user32" () As Long
 
Private Declare Function CloseClipboard Lib "user32" () As Long
 
Private Const WH_KEYBOARD_LL = 13
Private Const HC_ACTION = 0
Private Const GWL_HINSTANCE = (-6)
Private Const VK_SNAPSHOT = &H2C
 
Private blnHookEnabled As Boolean
Private hhkLowLevelKybd As Long
Private lWBhwnd As Long
Private lTimerID As Long
Private oNewApp As Application
 
'-------------------
'Public routines .
'-------------------
Public Sub SetHook()
 
    Dim owb As Workbook
 
    'Create an invisible xl instance
    'to run the keyboard hook callback.
    'this is to avoid accidental VBIDE crash.
    Set oNewApp = New Application
 
    oNewApp.EnableEvents = False
 
    Set owb = oNewApp.Workbooks.Open _
    (ThisWorkbook.FullName, ReadOnly:=True)
 
    oNewApp.Run "Hook_KeyBoard", ThisWorkbook.FullName
 
End Sub
 
Public Sub RemoveHook()
 
    'Proper cleanup.
    If Not oNewApp Is Nothing Then
        oNewApp.Run "Unhook_KeyBoard"
        oNewApp.Quit
    End If
 
End Sub
 
Public Sub Hook_KeyBoard(WbPathName As String)
 
    Dim lXLhwnd, lXLHinstance As Long
    Dim owb As Workbook
 
    'get a temp pointer to calling WB.
    Set owb = GetObject(WbPathName)
 
    'store the hwnd of the calling WB.
    lWBhwnd = FindWindowEx _
    (owb.Parent.hwnd, 0, "XLDESK", vbNullString)
    lWBhwnd = FindWindowEx(lWBhwnd, 0, "EXCEL7", owb.Name)
 
    'release ref to calling WB.
    Set owb = Nothing
 
    'hook the keyboard system wide.
    lXLhwnd = FindWindow("XLMAIN", Application.Caption)
    lXLHinstance = GetWindowLong(lXLhwnd, GWL_HINSTANCE)
 
    If blnHookEnabled = False Then
        hhkLowLevelKybd = SetWindowsHookEx _
        (WH_KEYBOARD_LL, _
        AddressOf LowLevelKeyboardProc, lXLHinstance, 0)
        blnHookEnabled = True
        'unhook the keyboard in case it wasn't properly
        'unhooked in the Before_Close event.
        lTimerID = SetTimer(0, 0, 1, AddressOf MonitorWorkBook)
    End If
 
End Sub
 
Public Sub Unhook_KeyBoard()
 
    If hhkLowLevelKybd <> 0 Then _
    UnhookWindowsHookEx hhkLowLevelKybd
    hhkLowLevelKybd = 0
    blnHookEnabled = False
 
End Sub
 
'------------------
'Private routines.
'------------------
Private Function LowLevelKeyboardProc _
(ByVal nCode As Long, ByVal wParam As Long, _
ByRef lParam As KBDLLHOOKSTRUCT) As Long
 
    If nCode = HC_ACTION Then
        'never process the Print Key.
        If lParam.vkCode = VK_SNAPSHOT Then
            LowLevelKeyboardProc = -1
            SetTimer FindWindow("XLMAIN", Application.Caption) _
            , 0, 1, AddressOf ClearTheClipboard
            Exit Function
        End If
    End If
 
    LowLevelKeyboardProc = _
    CallNextHookEx(0, nCode, wParam, ByVal lParam)
 
End Function
 
Private Sub ClearTheClipboard()
 
    'display msgbox only once.
 
    KillTimer FindWindow("XLMAIN", Application.Caption), 0
    If FindWindowEx(0, 0, "#32770", _
    "Microsoft Excel") Then Exit Sub
    MsgBox "The Print Key is disabled.", _
    vbSystemModal + vbInformation
 
    'Optionally clear the clipboard.
    OpenClipboard (0&)
    EmptyClipboard
    CloseClipboard
 
    [COLOR=seagreen][B]'run any other code here...[/B][/COLOR]
 
End Sub
 
Private Sub MonitorWorkBook()
 
    'safety net in case the calling WB didn't
    'unhook the keyboard before closing.
    If IsWindow(lWBhwnd) = 0 Then
        DoEvents
        KillTimer 0, lTimerID
        Unhook_KeyBoard
        Application.Quit
    End If
 
End Sub

Place this code in the Workbook module :

Code:
Option Explicit
 
Private Sub Workbook_Open()
 
    Call SetHook
 
End Sub
 
Private Sub Workbook_BeforeClose(Cancel As Boolean)
 
    Call RemoveHook
 
End Sub

Thank you very much for solving this problem.

Biz :bow:
 
Upvote 0
Sorry for bringing this up after such a long time but when I use the CTRL+Alt+Print Screen combination I can still paste the screenshot.
 
Upvote 0

Forum statistics

Threads
1,223,630
Messages
6,173,454
Members
452,514
Latest member
cjkelly15

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top