Clear immediate window revisited

Discussion in 'AutoCAD' started by MP, Oct 27, 2004.

  1. MP

    MP Guest

    Hi all,
    I'm trying to get the often asked question how to clear the immed wind
    programatically.
    this almost works but it not only clears it when the sub is called but also
    reclears it even after other code is run

    it's like it's on some kind of automatic loop or something

    Sub ClearImmediateWindow()
    Application.VBE.Windows.Item("Immediate").SetFocus
    SendKeys "^a"
    SendKeys "{del}"
    'here there must be required something to close this operation but I don't
    know what it might be

    End Sub

    Sub test()
    Dim i As Integer
    While i < 10
    Debug.Print i
    i = i + 1
    Wend

    ClearImmediateWindow

    While i < 20
    Debug.Print i
    i = i + 1
    Wend

    End Sub

    that test sub should leave 11 - 20 showing in immediate window but it acts
    like it's recalling the clearImmediateWindow sub a second time after the
    second while loop

    any thoughts?
    Thanks
    Mark
     
    MP, Oct 27, 2004
    #1
  2. Hi Mark,

    The SendKeys do not seem to be executing until after the rest of the code. I added a messagebox after the call to the sub and checked the immediate window. When the messagebox displayed there was still the result of your first loop and after I dismissed the messagebox there was the result of your second loop.

    Regards - Nathan
     
    Nathan Taylor, Oct 28, 2004
    #2
  3. MP

    MP Guest

    Hi Nathan,
    Thanks for checking.
    thats weird!
    :)
    with msgbox calls before and after the call to clear it actually works now
    here
    im calling it via f5 in the ide at this point
    without the msgbox calls, it clears both loops and the imm wind is blank at
    end of sub test running

    back to the dwg board....

    'this sub with msgbox calls uncommented almost works(leaves one line at top
    of window uncleared each time)
    'but without msgbox calls uncommented does not work(clears first loop and
    second loop leaving window blank)

    I'm trying to figure how to "Unset focus" from imm wind after clearing....
    to see if that helps

    ha! got it...partially
    see new versions below
    that helps but i'm still "one off" on the lines remaining in window
    according to my calculations :)

    after running testclear the following is shown in IW

    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19

    from my code I would have thought that should have been

    10
    11
    12
    13
    14
    15
    16
    17
    18
    19

    'new versions
    '-------------------------------------
    Sub testclear()
    Dim i As Integer
    While i < 10
    Debug.Print i
    i = i + 1
    Wend
    ClearImmediateWindow
    While i < 20
    Debug.Print i
    i = i + 1
    Wend
    End Sub
    '-------------------------------------
    Function ImmediateIndex() As Long
    Dim lngWind As Long
    For lngWind = 1 To Application.VBE.Windows.Count
    If Application.VBE.Windows.Item(lngWind).Caption = "Immediate" Then
    ImmediateIndex = lngWind
    Exit For
    End If
    Next lngWind
    End Function
    '-------------------------------------
    Sub ClearImmediateWindow()
    Dim lngImmIdx As Long
    lngImmIdx = ImmediateIndex
    Application.VBE.Windows.Item("Immediate").SetFocus
    SendKeys "^a"
    SendKeys "{del}"
    'now get out of IW so doesn't run itself again magically without being
    called again!!!
    Application.VBE.Windows.Item(lngImmIdx - 1).SetFocus
    End Sub
    '-------------------------------------
    thanks
    Mark

    I added a messagebox after the call to the sub and checked the immediate
    window. When the messagebox displayed there was still the result of your
    first loop and after I dismissed the messagebox there was the result of your
    second loop.
     
    MP, Oct 28, 2004
    #3
  4. When I run your new code I get 0-20 in the IW. So it doesn't seem to be clearing for me.
     
    Nathan Taylor, Oct 28, 2004
    #4
  5. MP

    MP Guest

    the weirdness continues!
    :)

    I have grave misgivings on the ugly hacking workaround i calle
    ImmediateIndex
    this is obviously just for debugging purposes to save clicking one button
    before running code that will spit out its' garbage on teh IW.
    but I assume if the IW was not shown at the time this was run there would be
    an error of some kind
    haven't thought though it much cause I'm jamming on a crunch deadline so not
    the best time to be trying to get this to work.
    I just thought initially it would be a quickie, but I see there's more to it
    than that for some reason.
    i have mztools loaded so there's a button for it.
    I can set a keyboard shortcut for that button i assume
    so maybe a sendkeys of that shortcut would work
    but I know send keys is the ugly stepchild so i feel dirty even using
    it....but hey....
    :)
    later
    Thanks again for looking at it.
    Mark

    clearing for me.
     
    MP, Oct 28, 2004
    #5
  6. Since the IW only has a limited history. Would sending a load of blanklines suffice eg.

    Sub testclear()
    Dim i As Integer
    While i < 10
    Debug.Print i
    i = i + 1
    Wend
    ClearImmediateWindow
    While i < 20
    Debug.Print i
    i = i + 1
    Wend
    End Sub

    Sub ClearImmediateWindow()
    Dim i As Integer
    While i < 1000
    Debug.Print ""
    i = i + 1
    Wend
    End Sub
     
    Nathan Taylor, Oct 28, 2004
    #6
  7. MP

    MP Guest

    I had considered that, and pretty sure I've seen the same solution posted in
    the past.
    I was just erroneously thinking that that was a "funky workaround".
    Actually it is a beautifully simple and expedient solution.
    Thanks for beating into my dense head the beauty of simplicity.
    :)
    Mark
     
    MP, Oct 28, 2004
    #7
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.