log4net •  C++ •  VB6 •  VBA •  .NET  •  Silverlight •  Windows Phone 7 •  ASP.NET

Universal Trace Monitor for Software Developers and Testers

Using DevTracer with Visual Basic® 6

DevTracer is a replacement for the Debug.Print method in Visual Basic® 6. It is more flexible, because Debug.Print output is only visible inside the development environment in the immediate window. Once the application is compiled, it cannot be used any more.

Debug output generated with DevTracer can still be used once the application is compiled. Furthermore DevTracer provides two methods Write and WriteLine. WriteLine corresponds to Debug.Print. Write is similar, except that it does not append a newline character. Therefore the output of several calls to Write will be shown on the same line. DevTracer also has some support for formatting with methods Indent and Unindent.

Because Visual Basic 6 and Visual Basic for Applications (used in Microsoft Office® products) are very similar, the example code below can be used for both.

The code encapsulates all calls to the COM-Component of DevTracer. Have a close look at method Init. It Instantiates the COM object, but tries only once. If this attempt fails, all calls to methods like WriteLine() will do nothing. Therefore the application will work on a computer without the COM-component installed, without any significant performance loss.

Remark:

The methods WriteLine and Write are mapped to DoPrintLine and DoPrint respectively because Write is reserved in Visual Basic.

Just copy the following code to a module DEVTRACER.BAS :

Option Explicit

Private dev As Object
Private isInitialized  As Boolean ' VB6 initializes this to 'false'

Private Sub Init()
    If isInitialized = True Then
        Exit Sub   ' only try once to initialize COM object
    End If
    isInitialized = True
    On Error GoTo notFound
    Set dev = CreateObject("NRSoftware.DevTracer")
    dev.Init "localhost", 12345   ' set ip-adress and port
    dev.IndentSize = 2            ' set indent size

notFound:
End Sub

Public Sub DoPrintLine(ByVal str As String)
    Init
    If dev Is Nothing Then Exit Sub
    dev.WriteLine str
End Sub

Public Sub DoPrint(ByVal str As String)
    Init
    If dev Is Nothing Then Exit Sub
    dev.Write str
End Sub

Public Sub Indent()
    Init
    If dev Is Nothing Then Exit Sub
    dev.Indent
End Sub

Public Sub Unindent()
    Init
    If dev Is Nothing Then Exit Sub
    dev.Unindent
End Sub
DevTracer.bas Download

With this module added to your project, you can use DevTracer with code like:

devTracer.PrintLine "This is line 1"
devTracer.Indent
devTracer.DoPrintLine "this is line 2 indented"
devTracer.Indent
devTracer.DoPrintLine "this is line 3 even more indented"
devTracer.Unindent
devTracer.Unindent
devTracer.DoPrintLine "this is line 4, all indenting removed"

The example here uses late binding. It is also possible to work with early binding. But then the application will depend on DevTracer Component being installed on the computer running the application.