• Clear Immediate Window

    Author
    Topic
    #354977

    I know this is an old issue, but I was just wondering if anybody had anything new on using code to clear the Immediate window in the Word 97 IDE. I’d love to be able to put a statement in code, during a debugging phase, that clears the immediate window so that old debug.print results are cleared. Clicking on the windows, pressing Ctrl-A and del is a real bother when I’m trying to concentrate on the code execution.

    Call ClearImmediate ‘Like this

    Viewing 0 reply threads
    Author
    Replies
    • #522708

      Kevin,

      The way I’ve gotten at lines in the VBE is via the CodeModule property (of ActiveCodePane or SelectedVBComponent), but the Immediate window doesn’t appear to have a CodeModule property.

      You can get to the Immediate window via the VBE.Windows collection, but can’t do much more than .SetFocus, which gets the cursor in there anyway.

      So using that, here’s a SendKeys method that does work – use it at your own risk! (might want to add an On Error Resume Next statement in there just for insurance…)

      VBE.Windows(“Immediate”).SetFocus
      SendKeys “+{UP}” ‘this selects everything, somehow…
      SendKeys “{DEL}”

      Gary

      • #522714

        > SendKeys “+{UP}” ‘this selects everything, somehow…

        Not in MY Word97SR2/VBA.

        I wasn’t sure why Shift-Up arrow should select everything.

        I tried various combinations for Ctrl-A (which works by hand) such as
        ^A
        ^(A)

        • #522716

          This turns out to be very flaky: after I posted this, I tried it again and found it wasn’t working. Then it was working. Then it wasn’t.

          In the general spirit of trying weird things, I then tried using:

          VBE.Windows(“Immediate”).SetFocus
          SendKeys “+{UP}” ‘this selects everything, somehow…
          SendKeys “{DEL}”
          VBE.Windows(“Immediate”).SetFocus
          SendKeys “+{UP}” ‘this selects everything, somehow…
          SendKeys “{DEL}”

          (just running it twice in the same procedure.)
          Now, it works every time! (well, the 5 times I tried it before I got bored…)

          I have to admit I didn’t try the SendKeys for Ctrl+A.

          • #522738

            Gary,

            Thanks. I’ll try this. I tried the same code (very similar anyway… with Ctrl-A, which did not work.) I’m eager to try this shift-up business.

          • #522741

            What version of Word did you test this code with?

            It definitely does not work in Word 97. I’m still trying though…

          • #522746

            Ok, get this. Your VBE.Windows(“Immediate”).Setfocus did absolutely nothing in my Word 97 IDE.

            So….

            I tried looping thru the collection. That worked!

            Your shift-up did something, but not what you report. I think you have to run it twice or more because Shift-Up only grabs the current line. So I tried Ctrl-Shift-Home to grab all lines from the current line up. It Worked! Also, better move to the end of the window text first, then ^+{Home}

            So….

            Here’s the code that works in our climate:

            Sub ClearImmediate()
                '#Const mDeveloping = False  'Unrem this if not declared elsewhere and if needed
                #If mDeveloping Then
                    Dim ideWindow As VBIDE.Window
                #Else
                    Dim ideWindow As Object
                #End If
                
                For Each ideWindow In VBE.Windows
                    If ideWindow.Caption = "Immediate" Then
                        ideWindow.SetFocus
                        SendKeys "^{End}"
                        SendKeys "^+{Home}"
                        SendKeys "{DEL}"
                    End If
                Next
                Set ideWindow = Nothing
                
            End Sub
            

            I loathe Sendkeys just as much as the next guy, but this code appears to work quite reliably, and if *anybody* can find any property/method/trick in the object model that can do this I’ll gladly abandon the key pusher code!

            Please test this for me. Chris, you too. And anyone else who would like to give some feedback. Do we have a “universal” ClearImmediate sub here, or not?

            • #522771

              depends what you mean by universal. If we’re talking Excel, in 97 i had to change

              For Each ideWindow In VBE.Windows

              to

              For Each ideWindow In Application.VBE.Windows

              (I was getting a run time error 429: “activex component can’t create object” without this) -otherwise it works fine, and that’s not a major change.

              Brooke

            • #522772

              Hey, that’s pretty cool. Being explicit with ‘Application’ is better form, anyway. Thanks for feedback Brooke. I’ll update mine too.

            • #522781

              Well, after further testing, I’m finding this little module is not all it’s cracked up to be.

              Put the statement

              Call ClearImmediate

              in a module you are testing. Put some junk in the immediate window. Run the module. Did the ClearImmediate code clear the immediate window? Not me for. Grand bummer. flee fire smash

              Finicky.

            • #522779

              Good on ya, mate.
              This works in both my Word 97 and Word 2K setups, so I’ll take two copies, please.

              Since you can only get to the Immediate Window as a window (and not a code module), and since you can’t access the code via the window object, it’s a safe bet that this is as good as it gets.

              For something that’s never going to be used outside my own VBE, I couldn’t care less if it uses SendKeys, trained egrets or whatever!

            • #522782

              Gary,

              More testing, please. See my previous post to myself (which you may have missed).

              Thanks for the feedback from down under. Good to know you are having some success in your zone. But see my post, please.

            • #522785

              HA!

              I added

              ideWindow.Visible = true

              and it worked from a code call!

              Sub ClearImmediate()
                  #If mDeveloping Then
                      Dim ideWindow As VBIDE.Window
                  #Else
                      Dim ideWindow As Object
                  #End If
                  
                  For Each ideWindow In Application.VBE.Windows
                      If ideWindow.Caption = "Immediate" Then
                          ideWindow.SetFocus
                          ideWindow.Visible = True
                          SendKeys "^{End}"
                          SendKeys "^+{Home}"
                          SendKeys "{Del}"
                      End If
                  Next
                  Set ideWindow = Nothing
                  
              End Sub
            • #522788

              By ‘down under’ are you referring to the placement of the Immediate window on my screen? – this missive comes to you straight from the corporate epicenter of NYC (maybe you are referring to the subway?).

              Don’t know whether you’ll find this disappointing, but your earlier version is still working fine for me! Setting the window to visible certainly wouldn’t hurt though.

              By the way you don’t need the “Call” keyword when you call ClearImmediate, since you’re not passing it any arguments (but I know you know that grin)

              BTW I just learned something new: if I make a mistake while typing in the Immediate window, and reflexively try Ctrl+Z to undo it, what gets undone is the last change I made up in the “real” code module doh.

            • #522804

              I don’t need to set .visible = true in excel to call it from other code but as you’ve both said, that won’t hurt.

            • #522806

              Oh yes, I forgot to ask, is training egrets any easier than learning VBA?

            • #522813

              I can’t speak from direct experience. Getting started with VBA was hard, but I’ve had no egrets. laugh

            • #522819

              Charlotte alert!

            • #522836

              Now stop that! You’ll make everyone think that I pounce on every pun I see when it’s really only every pun I notice! scream

            • #522859

              Not that we’re getting off topic or anything… but it’s only to make you notice that the alerts are issued laugh

            • #522851

              Egrets – i’ve had a few!

            • #522860

              [indent]


              Egrets – i’ve had a few!


              [/indent]
              and so have I- and quite tasty too.

            • #522865

              have you tried cooking them my way?

            • #522890

              As a matter of interest , the immediate window is Type 5, whic enables you to use If ideWin.Type = 5 Then etc. But you can do all that without loops by using

              Sub ImmediateCls()
                  Dim iCls As String
                  iCls = "^{End}^+{Home}{DEL}"
                  Application.VBE.Windows.Item("Immediate").SetFocus
                  SendKeys iCls
              End Sub

              The Item is an index and can be an integer, and you CAN zap code with it. Try putting 1 or 2 in place of Immediate, but be ready to click undo. I am not sure if Gary was of the opinion that you cannot access VBA modules by reference to windows. Well you can.

              BTW , the above was only used from Excel VBA (2000).

              Andrew C

            • #522895

              Andrew,

              Should that be

                  Dim iCls As String    

              Instead of Cls?

            • #522897

              Geoff,

              Yes, it should and thanks for pointing that out. Must have deleted it in error whilst posting as it is ok in the original. Maybe one of those egrets got it.

              Sorry,

              Andrew.

            • #522912

              Hi Andrew,

              Just to clarify, this is what I said (about a million posts higher up on this thread):
              [indent]


              The way I’ve gotten at lines in the VBE is via the CodeModule property (of ActiveCodePane or SelectedVBComponent), but the Immediate window doesn’t appear to have a CodeModule property.

              You can get to the Immediate window via the VBE.Windows collection, but can’t do much more than .SetFocus, which gets the cursor in there anyway.

              So using that, here’s a SendKeys method that does work – use it at your own risk! (might want to add an On Error Resume Next statement in there just for insurance…)


              [/indent]So we’re saying much the same thing, which is that you can get to the Immediate window, but have to use SendKeys from there as you can’t get to the code in there via the methods which you’d use for a proper code module. (Oh, and your SendKeys method works better than mine!)

              Now, I haven’t yet tackled the mysteries of API calls (just on the verge of starting), but I’d have thought there would be some way to clear the contents of the Immediate window via an API call – does that sound feasible?

              Gary

            • #522920

              Gary,

              [indent]


              Since you can only get to the Immediate Window as a window (and not a code module), and since you can’t access the code via the window object


              [/indent]
              Sorry about any misunderstanding, but I was unclear what exactly the above meant.

              The same notion of using an API call did occur to me, and I can see no reason why it would not work – research is continuing.

            • #523008

              Andrew,

              Nicely done. Your code works great! I’ll replace it for mine ’cause it’s cleaner and shorter – the programs are supposed to grow!

              Hey, when I first tried accessing the Immediate window without looping, it would never go active with setfocus. I tried looping (which would not seem to make any difference) — just for grins and it worked. I assumed Word 97 had some problem. I wonder if the problem was not expliciting starting with the Application object.

            • #523041

              Kevin,

              Problem – The code I posted does NOT work when the Immediate window is undocked. It works fine when the window is docked. If run from from whist in the VBE, and the Immediate window is floating it clears text from whatever module it is lcated in. When run as macro from Excel, it does nothing. Works fine in both situations whilst the window is docked.

              I’ll have to try and figure what is going on – any ideas ?. Must have something to do with the VBIDE.

              Andrew

            • #523044

              I tested with the immediate window closed too and, you are correct. It does not work.

              Back to my looping code? It appears to work fine with the immediate windows closed. The .visible = true property comes in handy to open the window so .setfocus works. However, it is not necessary to make the immediate window visible *first*. It looks like you can set focus first, then visible and it works. Either way. Also tested with and without VBIDE loaded. Works fine either way.

              I cleaned it up using one Sendkeys statement as you showed. I call your code and raise you one routine:

              Sub ClearImmediate()
              Dim iCls As String
              
                  #If mDeveloping Then
                      Dim ideWindow As VBIDE.Window
                  #Else
                      Dim ideWindow As Object
                  #End If
                  
                  iCls = "^{End}^+{Home}{DEL}"
                  For Each ideWindow In Application.VBE.Windows
                      If ideWindow.Caption = "Immediate" Then
                          ideWindow.Visible = True
                          ideWindow.SetFocus
                          SendKeys iCls
                      End If
                  Next
                  Set ideWindow = Nothing
                  
              End Sub
              
            • #523081

              Kevin,

              I’ll see your call and raise you one module.

              I had the same problem in Excel97 (not 2000) with your code. Strangely they both work as advertised when run as a macro from Excel (97) – even when the immediate window is hidden.

              Clearly neither are completely save on all systems, and I suspect it must have something to do with what references sre set up in VB.

              Interesting !

              Andrew

            • #523115

              Andrew,

              I’ll fold. I’m moving to the blackjack tables…

            • #523255

              Kevin,

              I have discovered, FWIW, that if you switch off the immediate windows dockable status in Tools, Options, then all versions of code posted work fine,(of course, then you cannot dock the window). The rule would seem to be if the immediate window is set as Dockable, then it should be docked, otherwise you cannot give it focus, not even via API calls, which I tried. Must have something to do with Window Linking.

              Just thought I’d mention that

              Andrew

            • #523344

              Andrew,

              Thanks for “staying on topic” with me on this. I appreciate your interest and your discoveries. I was trying similar things with window linking. It appears that we have no access to the “dockable” property of ide windows, but we can undock them with the .remove method — none of which does us any good. Now I cannot even get the code to work at all. Too weird.

            • #522906

              This works FINE for me. The DAM have awarded you the Grand Order Of The Tegres!

              Neat having it work as a command, but I really want to drag it on to my VBE toolbar.

              While you guys were wussing around with this, iw as putting the nowhere-near-finishing touches to me Procedure Clipper. The GUI is a thing of dynamic terror.

              I made me a 30MB library string last night. Took 5 hours to build it.

    Viewing 0 reply threads
    Reply To: Clear Immediate Window

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: