Share via


Displaying Help for Custom Error Messages

This content is no longer actively maintained. It is provided as is, for anyone who may still be using these technologies, with no warranties or claims of accuracy with regard to the most recent product version or service release.

The Raise method of the Err object has optional helpfile and helpcontext arguments that set the HelpFile and HelpContext properties of the Err object to the specified Help file name and context ID when your custom error is triggered. If the error trap in your procedure then passes these values to the MsgBox function, you can add a Help button to call additional Help from your error message dialog box. The following code example shows how to do this:

Sub ShowErrorHelp()
   Dim strAppPath As String
   
   strAppPath = ActiveDocument.Path
   
   On Error GoTo ShowErrorHelp_Err
   Err.Raise Number:=vbObjectError + 1234, _
             Source:="Sub ShowErrorHelp", _
             Description:="This error has a reason.", _
             HelpFile:=strAppPath & "\sample.chm", _
             HelpContext:=2003

ShowErrorHelp_End:
   Exit Sub

ShowErrorHelp_Err:
   MsgBox Prompt:=Err.Description, _
          Buttons:=vbMsgBoxHelpButton, _
          HelpFile:=Err.HelpFile, _
          Context:=Err.HelpContext
   Resume ShowErrorHelp_End
End Sub

The helpfile and helpcontext arguments of the Raise method support using a compiled HTML Help file in all Office applications, including Word and Access. There is no need to use the HtmlHelp API to display a Help topic in a .chm file when you are using the Raise method to return a Help file name and context ID in Word and Access.

See Also

Displaying Help from VBA Code | Displaying Help by Using the Help Method | Displaying Help by Using the HtmlHelp API | Displaying Help by Using the InputBox and MsgBox Functions | Displaying Help for Properties and Methods in Class Modules