Use VBA to Clear Immediate Window?

前端 未结 16 959
刺人心
刺人心 2021-01-30 00:19

Does anyone know how to clear the immediate window using VBA?

While I can always clear it myself manually, I am curious if there is a way to do this programmatically.

相关标签:
16条回答
  • 2021-01-30 00:28

    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.

    0 讨论(0)
  • 2021-01-30 00:33

    After some experimenting, I made some mods to mehow's code as follows:

    1. Trap errors (the original code is falling over due to not setting a reference to "VBE", which I also changed to myVBE for clarity)
    2. Set the Immediate window to visible (just in case!)
    3. Commented out the line to return the focus to the original window as it's this line that causes the code window contents to be deleted on machines where timing issues occur (I verified this with PowerPoint 2013 x32 on Win 7 x64). It seems the focus is switching back before SendKeys has completed, even with Wait set to True!
    4. Change the wait state on SendKeys as it doesn't seem to be adhered to on my test environment.

    I also noted that the project must have trust for the VBA project object model enabled.

    ' DEPENDENCIES
    ' 1. Add reference:
    ' Tools > References > Microsoft Visual Basic for Applications Extensibility 5.3
    ' 2. Enable VBA project access:
    ' Backstage / Options / Trust Centre / Trust Center Settings / Trust access to the VBA project object model
    
    Public Function ClearImmediateWindow()
      On Error GoTo ErrorHandler
      Dim myVBE As VBE
      Dim winImm As VBIDE.Window
      Dim winActive As VBIDE.Window
    
      Set myVBE = Application.VBE
      Set winActive = myVBE.ActiveWindow
      Set winImm = myVBE.Windows("Immediate")
    
      ' Make sure the Immediate window is visible
      winImm.Visible = True
    
      ' Switch the focus to the Immediate window
      winImm.SetFocus
    
      ' Send the key sequence to select the window contents and delete it:
      ' Ctrl+Home to move cursor to the top then Ctrl+Shift+End to move while
      ' selecting to the end then Delete
      SendKeys "^{Home}", False
      SendKeys "^+{End}", False
      SendKeys "{Del}", False
    
      ' Return the focus to the user's original window
      ' (comment out next line if your code disappears instead!)
      'winActive.SetFocus
    
      ' Release object variables memory
      Set myVBE = Nothing
      Set winImm = Nothing
      Set winActive = Nothing
    
      ' Avoid the error handler and exit this procedure
      Exit Function
    
    ErrorHandler:
       MsgBox "Error " & Err.Number & vbCrLf & vbCrLf & Err.Description, _
          vbCritical + vbOKOnly, "There was an unexpected error."
      Resume Next
    End Function
    
    0 讨论(0)
  • 2021-01-30 00:33
    Sub ClearImmediateWindow()
        SendKeys "^{g}", False
        DoEvents
        SendKeys "^{Home}", False
          SendKeys "^+{End}", False
          SendKeys "{Del}", False
            SendKeys "{F7}", False
    End Sub
    
    0 讨论(0)
  • 2021-01-30 00:34

    I had the same problem. Here is how I resolved the issue with help from the Microsoft link: https://msdn.microsoft.com/en-us/library/office/gg278655.aspx

    Sub clearOutputWindow()
      Application.SendKeys "^g ^a"
      Application.SendKeys "^g ^x"
    End Sub
    
    0 讨论(0)
  • 2021-01-30 00:34

    If you happen to be using Autohotkey here's the script I use.

    The key command is Ctrl+Delete. It only works if the VBE is active.
    When pressed it will clear the immediate window and then activate the code editor, via F7.

    I tend to want to clear the immediate when while I'm coding so now I can just hit Ctrl-Delete and keep coding.

    0 讨论(0)
  • 2021-01-30 00:40

    Marked answer does not work if triggered via button in worksheet. It opens Go To excel dialog box as CTRL+G is shortcut for. You have to SetFocus on Immediate Window before. You may need also DoEvent if you want to Debug.Print right after clearing.

    Application.VBE.Windows("Immediate").SetFocus
    Application.SendKeys "^g ^a {DEL}"
    DoEvents
    

    For completeness, as @Austin D noticed:

    For those wondering, the shortcut keys are Ctrl+G (to activate the Immediate window), then Ctrl+A (to select everything), then Del (to clear it).

    0 讨论(0)
提交回复
热议问题