Peter,
First off you need to add context ids to the help file.
Then this code can be used to invoke the help file. I tend to use HTML help
with add-ins, so I store the .chm file with the .xla file. Thus I can use
Thisworkbook.Path & "\" & AppTitleId & ".chm"
as the file path, where AppTitleId is a constant value for the application.
Option Explicit
Declare Function HtmlHelp Lib "hhctrl.ocx" _
Alias "HtmlHelpA" _
(ByVal hWnd As Long, _
ByVal lpHelpFile As String, _
ByVal wCommand As Long, _
ByVal dwData As Long) As Long
Const HH_DISPLAY_TOPIC = &H0
Const HH_SET_WIN_TYPE = &H4
Const HH_GET_WIN_TYPE = &H5
Const HH_GET_WIN_HANDLE = &H6
Const HH_DISPLAY_TEXT_POPUP = &HE ' Display string resource ID or text in
a pop-up window.
Const HH_HELP_CONTEXT = &HF ' Display mapped numeric value in
dwData.
Const HH_TP_HELP_CONTEXTMENU = &H10 ' Text pop-up help, similar to
WinHelp's HELP_CONTEXTMENU.
Const HH_TP_HELP_WM_HELP = &H11 ' text pop-up help, similar to WinHelp's
HELP_WM_HELP.
'---------------------------------------------------------------------------
Public Sub OpenHelp(ByVal ContextId As Long)
'---------------------------------------------------------------------------
' Function: Opens the HTML help file
'---------------------------------------------------------------------------
Dim hwndHelp As Long
'The return value is the window handle of the created help window.
Dim hwndHH
hwndHH = HtmlHelp(0, Thisworkbook.Path & "\" & AppTitleId & ".chm",
HH_HELP_CONTEXT, ContextId)
End Sub
You can then open the help file with code like this, whichg uses id 1000 as
it's in itial entry point and default. Specifying another value alolows
jumping in directly.
'---------------------------------------------------------------------------
Public Function CFShowHelp(Optional HelpId As Long = 1000)
'---------------------------------------------------------------------------
' Function: Shows the Help file
'---------------------------------------------------------------------------
OpenHelp HelpId
End Function
You can monitor F1 on a userform with code like
'---------------------------------------------------------------------------
Private Sub UserForm_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, _
ByVal Shift As Integer)
'---------------------------------------------------------------------------
If KeyCode = 112 Then 'F1
OpenHelp 1000
End If
End Sub
Just beware of using the sub name OpenHelp for you app. As we found out
recently, lots of other apps use that sub name (it's the obvious one to
use), and it throws up a nasty little bug as the apps conflict.
--
HTH
RP
(remove nothere from the email address if mailing direct)