+ Reply to Thread
Results 1 to 4 of 4

Clearing the Intermediate Window in VBA

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    10-02-2008
    Location
    Berkeley, CA
    MS-Off Ver
    Windows 2007
    Posts
    105

    Clearing the Intermediate Window in VBA

    I have the follwoing code
     Sub Debug_Print(Message)
        Application.SendKeys "^g ^a {DEL}"
        Debug.Print Message
        Stop    
     End Sub ' Debug_Print
    It does NOT print "Messge" unless I put a debug stop on the "Debug.Print" line and manually restart the code.

    I have tried putting a Wait after the "Application.Sendkeys" line, but that doesn't make it work either.

    Can anybody tell me how to make this work?

    Thanks,
    Mac

  2. #2
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Re: Clearing the Intermediate Window in VBA

    Hello
    Try this code
    Private Declare Function GetWindow Lib "user32" (ByVal hWnd As Long, ByVal wCmd 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 GetKeyboardState Lib "user32" (pbKeyState As Byte) As Long
    Private Declare Function SetKeyboardState Lib "user32" (lppbKeyState As Byte) 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 Const WM_KEYDOWN As Long = &H100
    Private Const KEYSTATE_KEYDOWN As Long = &H80
    
    Private savState(0 To 255) As Byte
    
    Sub Clear_Immediate_Window_Using_API()
    'Adapted By KeepITcool
    'Original From Jamie Collins FKA "OneDayWhen"
    'http://www.*****-blog.com/excel/2004/06/clear_the_immed.html
    '------------------------------------------------------------
        Dim hPane As Long
        Dim tmpState(0 To 255) As Byte
    
        hPane = GetImmHandle
        If hPane = 0 Then MsgBox "Immediate Window Not Found."
        If hPane < 1 Then Exit Sub
    
        'Save The Keyboardstate
        GetKeyboardState savState(0)
    
        'Sink The CTRL (Note We Work With The Empty tmpState)
        tmpState(vbKeyControl) = KEYSTATE_KEYDOWN
        SetKeyboardState tmpState(0)
    
        'Send CTRL+End
        PostMessage hPane, WM_KEYDOWN, vbKeyEnd, 0&
    
        'Sink The SHIFT
        tmpState(vbKeyShift) = KEYSTATE_KEYDOWN
        SetKeyboardState tmpState(0)
    
        'Send CTRL+SHIFT+Home And CTRL+SHIFT+BackSpace
        PostMessage hPane, WM_KEYDOWN, vbKeyHome, 0&
        PostMessage hPane, WM_KEYDOWN, vbKeyBack, 0&
    
    
        'Schedule Cleanup Code To Run
        Application.OnTime Now + TimeSerial(0, 0, 0), "DoCleanUp"
    End Sub
    
    Sub DoCleanUp()
    'Restore Keyboard State
    '----------------------
        SetKeyboardState savState(0)
    End Sub
    
    Function GetImmHandle() As Long
    'This Function Finds The Immediate Pane And Returns A Handle
    'Docked Or MDI, Desked Or Floating, Visible Or Hidden
    '------------------------------------------------------------
        Dim oWnd As Object, bDock As Boolean, bShow As Boolean
        Dim sMain$, sDock$, sPane$
        Dim lMain&, lDock&, lPane&
    
    
        On Error Resume Next
        sMain = Application.VBE.MainWindow.Caption
    
        If Err <> 0 Then
            MsgBox "No Access to Visual Basic Project"
            GetImmHandle = -1
            Exit 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 If
    
    
        For Each oWnd In Application.VBE.Windows
            If oWnd.Type = 5 Then
                bShow = oWnd.Visible
                sPane = oWnd.Caption
                If Not oWnd.LinkedWindowFrame Is Nothing Then
                    bDock = True
                    sDock = oWnd.LinkedWindowFrame.Caption
                End If
                Exit For
            End If
        Next oWnd
    
        lMain = FindWindow("wndclass_desked_gsk", sMain)
    
        If bDock Then
            'Docked Within The VBE
            lPane = FindWindowEx(lMain, 0&, "VbaWindow", sPane)
    
            If lPane = 0 Then
                'Floating Pane Which May Have Its Own Frame
                lDock = FindWindow("VbFloatingPalette", vbNullString)
                lPane = FindWindowEx(lDock, 0&, "VbaWindow", sPane)
    
                While lDock > 0 And lPane = 0
                    lDock = GetWindow(lDock, 2)
                    lPane = FindWindowEx(lDock, 0&, "VbaWindow", sPane)
                Wend
            End If
        ElseIf bShow Then
            lDock = FindWindowEx(lMain, 0&, "MDIClient", _
                                 vbNullString)
            lDock = FindWindowEx(lDock, 0&, "DockingView", _
                                 vbNullString)
            lPane = FindWindowEx(lDock, 0&, "VbaWindow", sPane)
        Else
            lPane = FindWindowEx(lMain, 0&, "VbaWindow", sPane)
        End If
    
        GetImmHandle = lPane
    End Function
    < ----- Please click the little star * next to add reputation if my post helps you
    Visit Forum : From Here

  3. #3
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Re: Clearing the Intermediate Window in VBA

    Or simply you can use sendkeys
    Sub Clear_Immediate_Window_Using_SendKeys()
        Dim x As Long
    
        For x = 1 To 10
            Debug.Print x
        Next x
    
        Debug.Print Now
        
        Debug.Print String(65535, vbCr)
        'OR
        'Application.SendKeys "^g ^a {DEL}"
    End Sub

  4. #4
    Forum Contributor
    Join Date
    10-02-2008
    Location
    Berkeley, CA
    MS-Off Ver
    Windows 2007
    Posts
    105

    Re: Clearing the Intermediate Window in VBA

    Tried both of the previous posts. Didn't work. The "Code" one didn't clear the Intermediate
    window but also caused the running VBA program to reset back to programming level as well.

    The first solution as I mentioned in the original report didn't work unless there wa
    s a "Stop" command between the "SendKeys" line and the "Debug.Print" command.

    My solution was to put a "Stop" command into my "Debug_Print" routine. Then when I pressed
    the "Jump back to the calling program" key, I ended up at the line after the call in th
    e calling program.
     Sub Debug_Print(Message, _
                      Optional Short_Comment = "")
        ' IF Trace() Then clears the INTERMEDIATE Pane priot to _
          displaying "Message" and "Short_Comment""'.
        ' 12/10/17 Created. Mac Lingo
        
        Prog = "Debug_Print"
        
        If Trace() Then
        
            Application.SendKeys "^g ^a {DEL}"
            Stop
            
             If Short_Comment <> "" Then
                Debug.Print Short_Comment
                Debug.Print "===========================================" & _
                            "==================="
            End If
    
            Debug.Print Chr(10) & Message
            
            Debug.Print "===========================================" & _
                        "==================="
        End If
    Last edited by CaptMac; 12-11-2017 at 01:30 AM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Hi everyone, intermediate but gaining knowedge here.
    By simpdogg in forum Hello..Introduce yourself
    Replies: 1
    Last Post: 03-13-2016, 06:11 AM
  2. Novice to Intermediate User Says Hello!
    By Skegeeace in forum Hello..Introduce yourself
    Replies: 1
    Last Post: 07-07-2014, 12:57 PM
  3. Hi. Intermediate Mac Excel user here
    By gilroy0 in forum Hello..Introduce yourself
    Replies: 1
    Last Post: 09-28-2013, 05:52 AM
  4. [SOLVED] How to get an Intermediate value ?
    By thilag in forum Excel General
    Replies: 4
    Last Post: 01-27-2013, 09:25 AM
  5. Intermediate Excel Formula Help
    By BigChris10 in forum Excel General
    Replies: 3
    Last Post: 01-12-2011, 05:27 PM
  6. Searching for intermediate value
    By zapp7 in forum Excel General
    Replies: 5
    Last Post: 07-01-2008, 03:29 PM
  7. date returning time in vba intermediate window
    By papa jonah in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 12-24-2005, 10:35 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1