# VBForums CodeBank > CodeBank - Visual Basic 6 and earlier >  Debug Print Redirect (Add-In)

## Elroy

This Add-In is designed *specifically* to work with my "Persistent Debug Print Window" *found here*.  Other than academically looking at it, it's not much use without that other program.

Basically, this Add-In redirects all your Debug.Print statements to that *PersistentDebugPrint.exe* program.  No need to rename your Debug.Print statements to do it.  Here's a screenshot of the Add-In's only interface:



As a note, you can use any/all the Debug.Print formatting, inclusive of Spc(), Tab(), semicolons (;), and commas (,).  However, when redirected, the Add-In does not send out a Debug.Print line (to PersistentDebugPrint.exe) until the line is terminated.  In other words, if you're in the habit of doing things like *Debug.Print "asdfasdf";* then you won't see this line in PersistentDebugPrint.exe until a subsequent Debug.Print terminates the line.  The Debug.Print line is stored in a buffer until terminated.

Also, to note, this doesn't interfere with any Debug.Assert statements, in case you happen to use those.

--------------------------

Compile the Add-In and save the DLL to wherever your Add-Ins are, and then load it (via your Add-Ins Manager). If you move the interface form, it'll return to where you positioned it the next time.

If you close that form, the whole Add-In is unloaded. Just re-load it to get the interface form back. It's your choice as to whether you keep it loaded.

Also, before someone requests it, I thought about a toolbar button, but I don't like the fact that the clipboard gets deleted/corrupted when you do that. I sometimes have stuff in my clipboard before I fire up the IDE. So, if you want this, you're on your own.

A HUGE thanks goes out to *The Trick* for several ideas and code snippets to get this done.  Also, thanks go out to *Eduardo* and* Dz32* for their assistance.

*Update #1*:  Added two new checkboxes to the little interface form.  They're pretty self-explanatory.  Here's the new interface:



Here's what the new "Run Starting" note looks like in the PersistentDebugPrint.EXE program:




> **** IDE Running "Project1" @ Saturday 11:57:08 am ****


*Update #2*:  When "Redirect..." is unchecked, it now also disables the "Run Starting" option (which is more appropriate).

Enjoy,
Elroy

----------


## dz32

nice

I wonder if an addin can detect when a debugging session starts? 

might be cool to have an option where the addin automatically sends a clear screen message 
to the receiver on debug start.

----------


## Elroy

> nice
> 
> I wonder if an addin can detect when a debugging session starts? 
> 
> might be cool to have an option where the addin automatically sends a clear screen message 
> to the receiver on debug start.


Not a bad idea.  I'll take note and do that on my next update to this stuff.  However, there is already a "Separate" option on the PersistentDebugPrint program that's sort of already for this purpose.  It just puts a line in the text, letting you keep track.  And, there are "Clear" options there as well.

----------


## dz32

I am not seeing a direct way to detect a debug session start. 
Looks like I had once tried hooking the start button events but disabled it (see bottom)

if the user has the "Save Changes when program starts" then some code in FileEvents_AfterWriteFile
should do the trick. maybe I can find where that opt is saved to warn.
Something like should only trigger once (unless group project)



```
Private Sub FileEvents_AfterWriteFile(ByVal VBProject As VBIDE.VBProject, ByVal FileType As VBIDE.vbext_FileType, ByVal FileName As String, ByVal result As Integer)

   If FileType = vbext_ft_Project Then
        If ClearImmediateOnStart Then
            ClearImmediateWindow
            If dbgIntercept.isActive Then dbgIntercept.debugPrint "<cls>"
        End If
   End If
```





```
        'hook the run button events (side effect it will disable during debugging so you have to use f5 to continue at bp..sucky!
        'we do this to clear immediate window on start which annoyed me too..
'        If ClearImmediateOnStart = 1 Then
'            Set mcbRealStartButton = FindRunButton()
'            If Not mcbRealStartButton Is Nothing Then
'                Set mnuRealRun = VBInstance.Events.CommandBarEvents(mcbRealStartButton)
'            End If
'        End If

'Private Function FindRunButton() As Office.CommandBarControl
'
'    Dim cbToolbar As Office.CommandBar
'    Dim cbSubMenu As Office.CommandBarControl
'
'    For Each cbToolbar In VBInstance.CommandBars
'        'Debug.Print "Toolbar: " & cbToolbar.Index
'        For Each cbSubMenu In cbToolbar.Controls
'            'Debug.Print vbTab & cbSubMenu.caption
'            If cbSubMenu.caption = "&Start" Then
'                Set FindRunButton = cbSubMenu
'                Exit Function
'            End If
'        Next
'    Next
'
'End Function
```

----------


## Elroy

EbMode, and it works great in an Add-In.

In fact, that's so easy, maybe I'll add it today.

----------


## dz32

one more wrinkle there may be times when the built in immediate window is locked and not accepting keypresses for sendkeys.

during the FileEvents_AfterWriteFile event seems to be one time. EbMode is reporting 0 here but various versions ClearImmediateWindow/ClearIt
are having no effect  :Frown:

----------


## Elroy

> one more wrinkle there may be times when the built in immediate window is locked and not accepting keypresses for sendkeys.
> 
> during the FileEvents_AfterWriteFile event seems to be one time. EbMode is reporting 0 here but various versions ClearImmediateWindow/ClearIt
> are having no effect


Dz, you probably want to have these discussions over here.

----------


## Elroy

Update #1 now in the OP.

Turns out I didn't need EbMode, as there are events in the extensibility model to take care of it.

----------


## The trick

See TipCompileProject/TipCompileProjectFull functions which are called when you run a project. You could hook these functions to have an event and make redirection. See TrickAdvancedTools project for details.

----------


## dz32

Aghh never knew about the VBIDE.VBBuildEvents great find. Works perfect for the external debug.print
still no affect with the built in clear immediate window code. 

I will poke at the native functions thanks trick

I do have a dedicated command bar button to clear immediate window and start debugging, that works
but it would be cleaner if it was automatic if option set.

thanks guys  :Smilie:

----------


## georgekar

Very nice. Its like a present. This is a resized screenshot, running an activeX dll. And works fine.


Thank You Elroy. God bless you.

PS.
I have two monitors. The right one is the primary screen, so the 0,0 is in this screen. So you place the addin's form in 0,0 perhaps to be at the left top corner, but actually this isn't the most left corner.

----------


## Elroy

> I have two monitors. The right one is the primary screen, so the 0,0 is in this screen. So you place the addin's form in 0,0 perhaps to be at the left top corner, but actually this isn't the most left corner.


I really just did that so you could find it.  I save where you last placed it, and put it there next time ... so just move it where you want it, and it should stay there.

----------


## georgekar

I see it. ok

----------

