Use VBA to Clear Immediate Window? Use VBA to Clear Immediate Window? vba vba

Use VBA to Clear Immediate Window?


Below is a solution from here

Sub stance()Dim x As LongFor x = 1 To 10        Debug.Print xNextDebug.Print NowApplication.SendKeys "^g ^a {DEL}"    End Sub


SendKeys is straight, but you may dislike it (e.g. it opens the Immediate window if it was closed, and moves the focus).

The WinAPI + VBE way is really elaborate, but you may wish not to grant VBA access to VBE (might even be your company group policy not to).

Instead of clearing you can flush its content (or part of it...) away with blanks:

Debug.Print String(65535, vbCr)

Unfortunately, this only works if the caret position is at the end of the Immediate window (string is inserted, not appended). If you only post content via Debug.Print and don't use the window interactively, this will do the job. If you actively use the window and occasionally navigate to within the content, this does not help a lot.


Much harder to do that I'd envisaged. I found an version here by keepitcool that avoids the dreaded Sendkeys

Run this from a regular module.

Updated as initial post missed the Private Function Declarations - poor copy and paste job by yours truly

Private Declare Function GetWindow _Lib "user32" ( _ByVal hWnd As Long, _ByVal wCmd As Long) As LongPrivate Declare Function FindWindow _Lib "user32" Alias "FindWindowA" ( _ByVal lpClassName As String, _ByVal lpWindowName As String) As LongPrivate Declare Function FindWindowEx _Lib "user32" Alias "FindWindowExA" _(ByVal hWnd1 As Long, ByVal hWnd2 As Long, _ByVal lpsz1 As String, _ByVal lpsz2 As String) As LongPrivate Declare Function GetKeyboardState _Lib "user32" (pbKeyState As Byte) As LongPrivate Declare Function SetKeyboardState _Lib "user32" (lppbKeyState As Byte) As LongPrivate Declare Function PostMessage _Lib "user32" Alias "PostMessageA" ( _ByVal hWnd As Long, ByVal wMsg As Long, _ByVal wParam As Long, ByVal lParam As Long _) As LongPrivate Const WM_KEYDOWN As Long = &H100Private Const KEYSTATE_KEYDOWN As Long = &H80Private savState(0 To 255) As ByteSub ClearImmediateWindow()'Adapted  by   keepITcool'Original from Jamie Collins fka "OneDayWhen"'http://www.dicks-blog.com/excel/2004/06/clear_the_immed.htmlDim hPane As LongDim tmpState(0 To 255) As BytehPane = GetImmHandleIf hPane = 0 Then MsgBox "Immediate Window not found."If hPane < 1 Then Exit Sub'Save the keyboardstateGetKeyboardState savState(0)'Sink the CTRL (note we work with the empty tmpState)tmpState(vbKeyControl) = KEYSTATE_KEYDOWNSetKeyboardState tmpState(0)'Send CTRL+EndPostMessage hPane, WM_KEYDOWN, vbKeyEnd, 0&'Sink the SHIFTtmpState(vbKeyShift) = KEYSTATE_KEYDOWNSetKeyboardState tmpState(0)'Send CTRLSHIFT+Home and CTRLSHIFT+BackSpacePostMessage hPane, WM_KEYDOWN, vbKeyHome, 0&PostMessage hPane, WM_KEYDOWN, vbKeyBack, 0&'Schedule cleanup code to runApplication.OnTime Now + TimeSerial(0, 0, 0), "DoCleanUp"End SubSub DoCleanUp()' Restore keyboard stateSetKeyboardState savState(0)End SubFunction GetImmHandle() As Long'This function finds the Immediate Pane and returns a handle.'Docked or MDI, Desked or Floating, Visible or HiddenDim oWnd As Object, bDock As Boolean, bShow As BooleanDim sMain$, sDock$, sPane$Dim lMain&, lDock&, lPane&On Error Resume NextsMain = Application.VBE.MainWindow.CaptionIf Err <> 0 ThenMsgBox "No Access to Visual Basic Project"GetImmHandle = -1Exit Function' Excel2003: Registry Editor (Regedit.exe)'    HKLM\SOFTWARE\Microsoft\Office\11.0\Excel\Security'    Change or add a DWORD called 'AccessVBOM', set to 1' Excel2002: Tools/Macro/Security'    Tab 'Trusted Sources', Check 'Trust access..'End IfFor Each oWnd In Application.VBE.WindowsIf oWnd.Type = 5 ThenbShow = oWnd.VisiblesPane = oWnd.CaptionIf Not oWnd.LinkedWindowFrame Is Nothing ThenbDock = TruesDock = oWnd.LinkedWindowFrame.CaptionEnd IfExit ForEnd IfNextlMain = FindWindow("wndclass_desked_gsk", sMain)If bDock Then'Docked within the VBElPane = FindWindowEx(lMain, 0&, "VbaWindow", sPane)If lPane = 0 Then'Floating Pane.. which MAY have it's own framelDock = FindWindow("VbFloatingPalette", vbNullString)lPane = FindWindowEx(lDock, 0&, "VbaWindow", sPane)While lDock > 0 And lPane = 0lDock = GetWindow(lDock, 2) 'GW_HWNDNEXT = 2lPane = FindWindowEx(lDock, 0&, "VbaWindow", sPane)WendEnd IfElseIf bShow ThenlDock = FindWindowEx(lMain, 0&, "MDIClient", _vbNullString)lDock = FindWindowEx(lDock, 0&, "DockingView", _vbNullString)lPane = FindWindowEx(lDock, 0&, "VbaWindow", sPane)ElselPane = FindWindowEx(lMain, 0&, "VbaWindow", sPane)End IfGetImmHandle = lPaneEnd Function