error handling

R

red6000

Hi I have the following error handling code, what I would like to do is add
into the msgbox, the line number of the code where it encountered the error.
Is this possible?

Thanks.


WRKITEM_ERROR:
If Err.Number = 429 Then
MsgBox "You do not appear to have outlook open. Please open and try
again" & Chr(13) & "(" & Err.Number & ") " & Err.Description, _
vbExclamation, _
"ERROR"
Else
MsgBox "(" & Err.Number & ") " & Err.Description, _
vbExclamation, _
"ERROR"
End If
Err.Clear
 
B

Bear

Red6000:

I don't know of any way to capture the line number. Perhaps you could use
custom error numbers, and set the error number at each test that fails. That
way you could group all like errors in a case test to use the same message,
but display the specific custom error number to help you troubleshoot.

Bear
 
S

Shauna Kelly

Hi

Use Erl, which still works, even though it fell out of the documentation
years ago. For example:

Public Sub CreateError()

Dim x As Integer

10 On Error Resume Next
20 x = 1 / 0
30 Debug.Print Erl 'will print 20

End Sub

Hope this helps.

Shauna Kelly. Microsoft MVP.
http://www.shaunakelly.com/word
 
E

Ed

Hi red6000 (& Bear, & Shauna)

If you want to use Erl, it's worth knowing that you don't have to number all
lines; you can mix and match - some lines numbered, some not.

I believe that what Erl reports is the number of the last numbered line that
executed, up to and including the line that failed.

Also, there is a useful addin for VBA (and for other VBs) which you can get
from this site: http://www.mztools.com.

The VBA version of the addin is freeware. One of the things MZ-Tools enables
you to do is quickly add or remove line numbers to a procedure or function.

I've used it with Word 2003 (not for numbering but for some of its other
features) but I haven't yet tried it with Word 2007.

Regards.

Ed
 
E

Ed

Hi Shauna,
Those tools really help me write better code.

On the issue of writing better code, I'll just chuck in a mention of Steve
McConnell's book, "Code Complete". For anyone who's not familiar with it,
it's not a VBA book, but it's the best book I've come across in the area of
developing good coding practices. And (for me at least) it's very readable.

Regards.

Ed
 

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