Can this be done with code from within the current project?
Jim
Can this be done with code from within the current project?
Jim
You cannot delete anything from the Immediate window while running mode, but only while stop or pause.
I don't think it's impossible, there must be way to do it at least pausing execution first (also by code), but the true is there hasn't been any direct solution for this. MZTools can do it but not at runtime.
Also there is this 4 year old thread with exactly the same topic: Clear Emmediate Window where i've been trying to do it by code with partial success, i'll play a bit now with that code again, who knows.
Edgemeal also provided a solution in that thread but it's not working for me.
Last edited by jcis; Feb 14th, 2012 at 11:16 PM.
Thanks for replies. Looked at the old thread. Damn!! Wouldn't Debug.Cls be handy!!!
I do not think that the Immediate window can be disabled while running a project, after all Debug.Print still works.!
Perhaps I'll try Module Subs MyDebugPrint and MyDebugCls to work on a frmDebug with a Scrolable Picture or RTB???
Sound daft?
Jim
Debug.Cls probably doesn't exist for a good reason, after all that window is meant as a debugging log.
I do wish the window was cleared each run though. At least there are add-ins that can do that much for us.
I can't believe it, i think i've got it. Just playing with my old attempt in that thread then changed to keybd_event API. Use your inmediate window caption as last param when calling FindWindowEx, i'm not sure but in english it should be "Inmediate".
You can call ClearInmediateWindow at runtime (for example in a button event) or as last line in Form_Load, it will pause execution, clear the inmediate window and then continue execution.Code:Option Explicit 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 PostMessage Lib "User32" Alias "PostMessageA" _ (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, _ ByVal lParam As Long) As Long Private Declare Sub keybd_event Lib "User32" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long) Private Const WM_ACTIVATE As Long = &H6 Private Const KEYEVENTF_KEYUP = &H2 Private Const VK_CANCEL = &H3 Private Const VK_CONTROL = &H11 Private Sub ClearInmediateWindow() 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
For testing pourposes you can also add another button to fill your inmediate window, like..
Code:Private Sub Command2_Click() Dim i As Long For i = 0 To 1000 Debug.Print "TEST!!!! Number: " & i Next End Sub
Last edited by jcis; Feb 15th, 2012 at 01:36 AM.
JCIS that works fine for me. Just one spelling change for my location --- Immediate for Inmediate.
Many many thanks
Jim
Ok, i corrected that in my previous post.
The only limitation i see till now: the call to ClearInmediateWindow() shouldn't be mixed with your code, it should be alone, i mean for example an event (as a button click event) or as last line inside an event/sub with more code, like Form_Load. the Form should always get focus after ClearInmediateWindow() is executed or else some weird things may happen, like your own vb code being affected by that "Select all" inside ClearInmediateWindow().
Also noticed that the current active form also gets a paint event fired
Yes, that's ok. It happens because the Form loses focus for a moment while the immediate window get focus to be cleared. It's like minimizing your Form and then bringing it back, when your Form gets focus back Paint() event is always executed. Anyway, if you want you can also keep execution in break mode after calling ClearInmediateWindow() by removing last 2 lines in this sub.
Last edited by jcis; Feb 15th, 2012 at 01:54 AM.
Wow thanks jcis! Had given up on that a while ago after a fiasco trying to use the VBIDE object.