Error Trapping

  • Thread starter Montana DOJ Help Desk
  • Start date
M

Montana DOJ Help Desk

Word 2000



I'm working on some error trapping routines for a set of macros that I've
created. This is the first time I've really tried to do a lot in terms of
error trapping. I've figured out how to use the properties of the Err
object to display a message box. The message box indicates to the user the
error number, description, the subroutine where the error occurred, and will
open up the Help topic on the error if the user clicks the button.



Now I want to make my dialog box a little more sophisticated. I want to
keep all the functionality that the message box currently has, and I want to
include buttons that will allow the user to keep or discard the data that
was being processed at the time the error was encountered. So I'm creating
a custom user form. I can pull in the text items (error number,
description, and subroutine name) without any problems.



But how do I pass the Help topic information to a button in my custom form?
Is there a way to place the vbMsgBoxHelpButton in my form so that I don't
have to mess with passing that information to a custom control?



Since this is my first real error trapping exercise, any general tips would
also be appreciated.



Thanks for any help that you can offer.



--Tom



State of Montana Department of Justice Help Desk



"Making the world a safer place."
 
A

Alex Ivanov

You will need to call the WinHelp API function to do that.
Here is an example. Create a new UserForm in a new document and add a
command button to it. Accept the default names. Then paste the following
code :

Option Explicit
Private Declare Function WinHelp Lib "User32" Alias "WinHelpA" (ByVal
hwndApp As Long, ByVal lpHelpFile As String, ByVal wCommand As Long, ByVal
dwData As Long) As Long

Private Sub CommandButton1_Click()
On Error GoTo ErrH
Debug.Print 0 / 0 'raise an error
Exit Sub
ErrH:
WinHelp 0, Err.HelpFile, 1, Err.HelpContext
End Sub

Now if you press the command button, division by zero error will be
generated and corresponding help topic should be displayed. If you have more
than one UserForm in the document, it may be better to put a single Declare
statement into a standard module (and change Private to Public) instead of
declaring it in each UserForm individually.

HTH
 
M

Montana DOJ Help Desk

Alex,

Thanks for the information, but I'm still having problems making it work. I
did as you instructed and created a user form, added a command button, and
then put the following code in click event for the command button:

Option Explicit

Private Declare Function WinHelp Lib "user32" Alias "WinHelpA" _
(ByVal hWnd As Long, _
ByVal lpHelpFile As String, _
ByVal wCommand As Long, _
ByVal dwData As Long) _
As Long

Private Sub CommandButton1_Click()

On Error GoTo ErrH
Debug.Print 0 / 0 'raise an error

Exit Sub

ErrH:

WinHelp 0, Err.HelpFile, 1, Err.HelpContext

End Sub

When I run the code, nothing happens. I'm wondering if I need to point it
to a help file, but I don't have a help file that I've built. I tried
modifying your code to get it working, but I accomplished nothing but
getting to give me one of the following 2 errors:

There is not enough memory available for this task.
Quit one or more programs to increase available memory, and then try
again. (2)


or

The C:\Program Files\Common Files\Microsoft
Shared\VBA\VBA6\1033\VBLR6.CHM file

is not a Windows Help file, or the file is corrupted.


The Help that I'm trying to access is the built-in VB Help. Just to make
sure that I clearly indicate the Help information that I'm trying to access,
here is my current code. If you paste this into a module and run it, then
click on the Help button, you should get the VB Help for the overflow error.

Dim Subroutine As String

Sub Test()

On Error GoTo ErrorHandler

Err.Raise 6 ' Raise the overflow error.

Exit Sub

ErrorHandler:

Subroutine = "Test"
TestErrorTrapping

End Sub

Sub TestErrorTrapping()

ErrContext = Err.HelpContext
ErrDescription = LCase(Err.Description)
ErrHelp = Err.HelpFile
ErrNumber = Err.Number
ModuleName = CodeName
Set ProjectName = ActiveDocument.VBProject
ProjectName = ProjectName.Name

mbMessage = "Run-time error '" & ErrNumber & "':" & vbCr & vbCr
mbMessage = mbMessage & "The " & Subroutine & " routine in the "
& ModuleName & " module of the "
mbMessage = mbMessage & ProjectName & " project has generated an
" & ErrDescription & " error."
mbStyle = vbExclamation + vbMsgBoxHelpButton + vbDefaultButton2
mbTitle = "Error"

Response = MsgBox(mbMessage, mbStyle, mbTitle, _
ErrHelp, ErrContext)

End Sub

The only reason that I don't want to just use this code is that I want my
dialog box to be a little more powerful and flexible than what a Msgbox can
handle, so I'm trying to find a way to get the same functionality as the
above into a user form to which I can then give additional abilities.

I hope that all this information doesn't just confuse the issue. I'm just
trying to provide as much information as possible, and be as clear as I can
about what I'm trying to accomplish, what I've done to this point, and the
results that I'm getting.

-- Tom

State of Montana
Department of Justice Help Desk

"Making the world a safer place."
 
A

Alex Ivanov

It's my oversight. Word 2000 uses HTML help, and to call html help you need
a different declare statement. Actually, what do you mean by nothing
happens? You should get an error message something about ivalis or corrupt
help file. Anyway, here is an updated code. I declared dwData as long to get
context sensitive help. You may try other options if you want. See MSDN
library's "Displaying Help by Using the HtmlHelp API" topic for more details

Option Explicit

'WinHelp
'Private Declare Function WinHelp Lib "user32" Alias "WinHelpA" _
(ByVal hWnd As Long, _
ByVal lpHelpFile As String, _
ByVal wCommand As Long, _
ByVal dwData As Long) _
As Long

'HTML help
Declare Function HtmlHelp Lib "HHCtrl.ocx" Alias "HtmlHelpA" _
(ByVal hwndCaller As Long, _
ByVal pszFile As String, _
ByVal uCommand As Long, _
ByVal dwData As Long) As Long


Private Sub CommandButton1_Click()

On Error GoTo ErrH
Debug.Print 0 / 0 'raise an error

Exit Sub

ErrH:

HtmlHelp 0, Err.HelpFile, &HF, Err.HelpContext

End Sub
 
M

Montana DOJ Help Desk

Alex,

Again, thanks for the response. I have not yet had a chance to try your new
suggestions, but will look at it tomorrow.

I wasn't totally clear when I stated that nothing happens when I ran the
original code that you posted. In fact, I did get an error message stating
that the vblr6.chm file was not a valid help file, or was corrupt. You're
correct on that point. Sorry that I didn't make that clear. What I really
meant was that in working on the problem, I got it to the point where I
could run the without any errors, but nothing would happen--the dialog box
would open, I'd step through the macro without any errors, and then the
dialog box would close.

I did try to lookup the corrupt file error, but couldn't find anything on
it. I did run across an article on MVPS.org that indicated that Word 2000
uses HTML files, but the code they had posted was pretty much the same as
yours, so it never occurred to me that HTML files might be the issue.

-- Tom

State of Montana
Department of Justice Help Desk

"Making the world a safer place."
 
M

Montana DOJ Help Desk

Alex,

I tried your new code, and now I have everything working exactly as I had
planned. Thanks for the help!

-- Tom

State of Montana
Department of Justice Help Desk

"Making the world a safer place."
 

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