How do I programmatically clear the Debugging Area in Word VBA?

When debugging Word VBA code, it would be helpful to programmatically clear the Debugging Area. ("Direct", where Debug.Print goes by default)

How do I do it?
Thank you very much!
wvsternAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Chris BottomleySoftware Quality Lead EngineerCommented:
ctrl A & delete

Chris
0
wvsternAuthor Commented:
Well, I meant a VBA command I can incorporate in my code, so it will automatically delete the debugging window every time it runs and hence save me the keystrokes each time.
0
Chris BottomleySoftware Quality Lead EngineerCommented:
Sorry ... easiest way I can think of is a call to clrImmediate

Chris
Sub clrImmediate()
    Application.VBE.Windows.Item("Immediate").SetFocus
    SendKeys "^a"
    SendKeys "{Del}"
End Sub

Open in new window

0
CompTIA Network+

Prepare for the CompTIA Network+ exam by learning how to troubleshoot, configure, and manage both wired and wireless networks.

wvsternAuthor Commented:
Looks like a good idea, but I get an error (in German, I'm translating it back) "The programmatic access to the VBA Project is not trusted."

Means I have to sign my code, right?
Hm, I created a certificate with the Office self-sign tool and signed the project, but the error message remains. :-(
0
Chris BottomleySoftware Quality Lead EngineerCommented:
Which version ...

Check:
Word 2003: Word itself - tools | macro | security | Trusted publishers | Trust access to VBP needs to be checked

WOrd 2007:
Developer tab | macro security | MAcro Settings | Trust access to VBP needs to be checked

Chris
0
wvsternAuthor Commented:
Yikes, what a hidden option!

Now we get through to the point where it selects everything in the Direct Pane, but it doesn't delete it yet.
0
wvsternAuthor Commented:
Points upped
0
GrahamSkanRetiredCommented:
You could just do this
Sub ClearImmediateWindow()
Dim i As Integer
For i = 1 To 200
Debug.Print
Next i
End Sub

Open in new window

0
Chris BottomleySoftware Quality Lead EngineerCommented:
Should work fine ... what version
0
wvsternAuthor Commented:
Hmmm. 2010 beta, I know, but it, well, kinda should work the same as 2007...
0
Chris BottomleySoftware Quality Lead EngineerCommented:
Don't have access to it but try replacing
SendKeys
with application.SendKeys

Chris
0
wvsternAuthor Commented:
It's VBA.Interaction.SendKeys, but it doesn't make any difference.
First line works, text gets selected, but the delete command doesn't get through.

@Graham: Thanks, creative idea, but I'd like a real delete better
0
Chris BottomleySoftware Quality Lead EngineerCommented:
I have to admit it's bugging me, the following is some VBA off the net credit as appropriate seems ok for me, put it into a module on it's own.

http://www.dailydoseofexcel.com/archives/2004/06/09/clear-the-immediate-window/

Chris
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 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_ACTIVATE As Long = &H6
Private Const WM_KEYDOWN As Long = &H100
Private Const VK_CONTROL As Long = &H11
Private Const KEYSTATE_KEYDOWN As Long = &H80

Private m_KeyboardState(0 To 255) As Byte
Private m_hSaveKeystate As Long

Sub ClearImmediateWindow()
'http://www.dailydoseofexcel.com/archives/2004/06/09/clear-the-immediate-window/
    Dim hChild As Long
    Dim hParent As Long
    Dim strCaptionVbe As String
    
    Const CLASS_VBE As String = "wndclass_desked_gsk"
    Const CLASS_IMMEDIATE As String = "VbaWindow"
    Const CAPTION_IMMEDIATE As String = "Immediate"
    
    ' Get handle to Immediate Window
    strCaptionVbe = Application.VBE.MainWindow.Caption
    hParent = FindWindow(CLASS_VBE, strCaptionVbe)
    hChild = FindWindowEx(hParent, ByVal 0&, _
                 CLASS_IMMEDIATE, CAPTION_IMMEDIATE)
    
    If hChild = 0 Then
      MsgBox "Immediate Window not found."
      Exit Sub
    End If
    
    ' Activate Immediate Window
    PostMessage hChild, WM_ACTIVATE, 1, 0&
    
    ' Simulate depressing of CTRL key
    GetKeyboardState m_KeyboardState(0)
    m_hSaveKeystate = m_KeyboardState(VK_CONTROL)
    m_KeyboardState(VK_CONTROL) = KEYSTATE_KEYDOWN
    SetKeyboardState m_KeyboardState(0)
    DoEvents
    
    ' Send CTRL+A (select all) and Delete keystokes
    PostMessage hChild, WM_KEYDOWN, vbKeyA, 0&
    PostMessage hChild, WM_KEYDOWN, vbKeyDelete, 0&
    
    ' Schedule cleanup code to run
    Application.OnTime Now + TimeSerial(0, 0, 0), "DoCleanUp"

End Sub

Sub DoCleanUp()

    ' Restore keyboard state
    GetKeyboardState m_KeyboardState(0)
    m_KeyboardState(VK_CONTROL) = m_hSaveKeystate
    SetKeyboardState m_KeyboardState(0)

End Sub

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
wvsternAuthor Commented:
Yihaa, it works!
But yikes, what a load of code for a keystroke!

Reply to this for 500 points.

Thank you very much!
0
Chris BottomleySoftware Quality Lead EngineerCommented:
Indded it's a lot of code but on a quick trawl I didn't find any alternatives between sendkeys and that one so it was all I could think as being worth a try.  At least once impleemnted it's a simple sub call which is the main thing.

Chris
0
wvsternAuthor Commented:
Really cool that you found this, man! :-)
My fingers will thank you! :-)
0
wvsternAuthor Commented:
Hi Chris

I found that there's one thing missing: You must add "DoEvents" at the end of the "ClearImmediateWindow" routine, because if you don't, the keystrokes will somehow clear the debug window also *after* your code with debugging output has run. Means it first clears, then it produces your debug output - and then immediately clears again. DoEvents rectifies this.

Kind regards and many thanks again for your help
Wolfgang
0
Chris BottomleySoftware Quality Lead EngineerCommented:
Intersting I see it with a quick test ... so many thanks for taking the time to identify it.  For completion within the thread I include the amendment as follows:

Chris
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_ACTIVATE As Long = &H6
Private Const WM_KEYDOWN As Long = &H100
Private Const VK_CONTROL As Long = &H11
Private Const KEYSTATE_KEYDOWN As Long = &H80
 
Private m_KeyboardState(0 To 255) As Byte
Private m_hSaveKeystate As Long
 
Sub ClearImmediateWindow()
'http://www.dailydoseofexcel.com/archives/2004/06/09/clear-the-immediate-window/
    Dim hChild As Long
    Dim hParent As Long
    Dim strCaptionVbe As String
     
    Const CLASS_VBE As String = "wndclass_desked_gsk"
    Const CLASS_IMMEDIATE As String = "VbaWindow"
    Const CAPTION_IMMEDIATE As String = "Immediate"
     
    ' Get handle to Immediate Window
    strCaptionVbe = Application.VBE.MainWindow.Caption
    hParent = FindWindow(CLASS_VBE, strCaptionVbe)
    hChild = FindWindowEx(hParent, ByVal 0&, _
                 CLASS_IMMEDIATE, CAPTION_IMMEDIATE)
     
    If hChild = 0 Then
      MsgBox "Immediate Window not found."
      Exit Sub
    End If
     
    ' Activate Immediate Window
    PostMessage hChild, WM_ACTIVATE, 1, 0&
     
    ' Simulate depressing of CTRL key
    GetKeyboardState m_KeyboardState(0)
    m_hSaveKeystate = m_KeyboardState(VK_CONTROL)
    m_KeyboardState(VK_CONTROL) = KEYSTATE_KEYDOWN
    SetKeyboardState m_KeyboardState(0)
    DoEvents
     
    ' Send CTRL+A (select all) and Delete keystokes
    PostMessage hChild, WM_KEYDOWN, vbKeyA, 0&
    PostMessage hChild, WM_KEYDOWN, vbKeyDelete, 0&
     
    ' Schedule cleanup code to run
    DoEvents
    Application.OnTime Now + TimeSerial(0, 0, 0), "DoCleanUp"

End Sub
 
Sub DoCleanUp()
 
    ' Restore keyboard state
    GetKeyboardState m_KeyboardState(0)
    m_KeyboardState(VK_CONTROL) = m_hSaveKeystate
    SetKeyboardState m_KeyboardState(0)
 
End Sub

Open in new window

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Word

From novice to tech pro — start learning today.