Using DevTracer with Visual Basic® for Applications
DevTracer is a replacement for the Debug.Print method
in Visual Basic® for Applications. 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.
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
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.
The image shows an example using DevTracer with Excel 2007 on Windows Vista, and the VBA code.
Important:
DevTracer is based on the .NET Framework version 2.0. There are some
compatibility issues with .NET
version 2.0 and some older versions of Microsoft Office (Office 2003 and before)
if the currently logged in user has administrator privileges. Normal uses
(without administrator privileges) will not have this problem.
The problem is that it is not possible to create an instance
of DevTracer Component. The sample code above (DEVTRACER.BAS) does not show any
error, because the code demonstrates how to implement using DevTracer without making
the application depending on it. Therefore the code above just will not work, i.e.
no information will be displayed in DevTracer Monitor.
In this case please try the following code:
Dim devTracer
set devTracer = CreateObject ("NRSoftware.DevTracer")
and you may get the message
If you get this message with the error number 80131700, please contact us or send a
mail to . We can provide you with a different setup, which
contains a fix from Microsoft.
This error does not occur with Office 2007 and Windows Vista.