Skip to content

Instantly share code, notes, and snippets.

@dzzie
Created September 10, 2022 21:25
Show Gist options
  • Save dzzie/4913ac1d99beafc81191078f01219a14 to your computer and use it in GitHub Desktop.
Save dzzie/4913ac1d99beafc81191078f01219a14 to your computer and use it in GitHub Desktop.
ClearImmediateWindow
Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Public Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWndParent As Long, ByVal hWndChildAfter As Long, ByVal lpszClass As String, ByVal lpszWindow As String) As Long
Public Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)
Public Const KEYEVENTF_KEYUP = &H2
Public Const VK_CANCEL = &H3
Public Const VK_CONTROL = &H11
Public Const WM_ACTIVATE As Long = &H6
Public Sub ClearImmediateWindow()
Dim lWinVB As Long, lWinEmmediate As Long
lWinVB = FindWindow("wndclass_desked_gsk", vbNullString)
'Last param depends on languages, use your inmediate window caption:
lWinEmmediate = FindWindowEx(lWinVB, ByVal 0&, "VbaWindow", "Immediate")
PostMessage lWinEmmediate, WM_ACTIVATE, 1, 0&
keybd_event VK_CANCEL, 0, 0, 0 ' (This is Control-Break)
keybd_event VK_CONTROL, 0, 0, 0 'Select All
keybd_event vbKeyA, 0, 0, 0 'Select All
keybd_event vbKeyDelete, 0, 0, 0 'Clear
keybd_event vbKeyA, 0, KEYEVENTF_KEYUP, 0
keybd_event VK_CONTROL, 0, KEYEVENTF_KEYUP, 0
keybd_event vbKeyF5, 0, 0, 0 'Continue execution
keybd_event vbKeyF5, 0, KEYEVENTF_KEYUP, 0
End Sub
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment