error handler with error line number

T

TomS

Hi,
I have a function in Word-VBA with error handler writing some info to log
file. There are strange errors occuring from time to time which I can't
trace. It would be very helpful for me to have information about line in
which error has occured. Is this possible?

For example:

sub foo()
on error goto ErrorHandler
'some code here

exit sub
ErrorHandler:
ErrNumber = Err.Number
ErrDesc = Err.Description
LineNumber = ???
AddToLog "ERROR:foo:" & LineNumber & ":" & ErrNumber & " " & ErrDesc
'resume or resume next - doesn't matter now...
end sub


I don't want to examine my specific function, I just would like to now in
general, if it is possible to read line number in which last error
occured.

Regards,
Tomek
 
J

Jezebel

VB still supports the erl instruction which reports the line number at which
the error occurred, but only if you use old-fashioned line numbering --

sub foo()

10 on error goto ErrorHandler
20 'some code here
30


exit sub
ErrorHandler:
ErrNumber = Err.Number
ErrDesc = Err.Description

LineNumber = erl <<=======

AddToLog "ERROR:foo:" & LineNumber & ":" & ErrNumber & " " & ErrDesc
'resume or resume next - doesn't matter now...



Note that you don't have to number *every* line. erl reports the last line
number up to and including the line that caused the error, so it might be
sufficient for debugging to insert one number for each 'section' of code.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top