Displaying WinHelp files from VBA

Article contributed by Jonathan West

Despite Microsoft's great hoo-rah about HTMLHelp, the UserForms default to calling WinHelp files. For Word 97 and Word 2K you are therefore recommended to use WinHelp rather than HTMLHelp.

To call a help topic from an item on a UserForm, proceed as follows. In the VBA Editor, select the name of your project, right-click, and select properties. In the dialog that comes up, specify the name of your help file.

For each control on a UserForm that you want to associate a help topic with, set the HelpContextID to the appropriate topic number. The Help will come up when the user selects the control and presses F1.

To call a helpfile direct from code, paste the following Windows API calls into a separate module.

' Commands to pass WinHelp()
Public Const HELP_CONTEXT = &H1        ' Display topic in ulTopic
Public Const HELP_QUIT = &H2          ' Terminate help
Public Const HELP_INDEX = &H3        ' Display index
Public Const HELP_CONTENTS = &H3& 
Public Const HELP_HELPONHELP = &H4        ' Display help on using help
Public Const HELP_SETINDEX = &H5           ' Set current Index for multi index help
Public Const HELP_SETCONTENTS = &H5&
Public Const HELP_CONTEXTPOPUP = &H8&
Public Const HELP_FORCEFILE = &H9&
Public Const HELP_KEY = &H101       ' Display topic for keyword in offabData
Public Const HELP_COMMAND = &H102&
Public Const HELP_PARTIALKEY = &H105&
Public Const HELP_MULTIKEY = &H201&
Public Const HELP_SETWINPOS = &H203&

Public 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

To display a particular topic, call it as follows

Dim iTemp As Long
iTemp = WinHelp(0, strHelpFile, HELP_CONTEXT, iContextID)

where strHelpFile is the full path name of the help file, and iContextID is a Long containing the topic number.

If you don't want to specify the path of the Help file until runtime (because you don't know what the template's path will be), then:
  

1.

Remove the help file details from the project's Properties dialog.

2.

At runtime, set the HelpFile property of the Err object to the current full pathname of your help file. This will ensure that the F1 key works right.

If you place the help file in the same folder as the template containing your code, you can find out the path of that template by using the MacroContainer property.

Place the following line in Initialize event of your UserForm:

Err.HelpFile = WordBasic.FileNameInfo$(Application.MacroContainer, 5) _
        & "MyHelp.hlp"

If the name of your template contains spaces, the you'll need this variant:

Err.HelpFile = WordBasic.FileNameInfo$(""" & Application.MacroContainer _
        & """, 5) & "MyHelp.hlp"

Now, your HelpContextID and WhatsThisHelp properties in your userforms and controls will work right, no matter where the template and help file are installed.