Programming context-sensitive WebHelp for Visual Basic applications

Note: The information in this topic is intended for developers who need to connect context-sensitive WebHelp topics to Visual Basic applications.

The sample code shown below uses a function called ShowHelp that will launch a local or remote WebHelp topic regardless of the browser type.

ShowHelp takes two arguments. The first, strTopic, is a URL or local file name. The second argument, bIsLocal, is a boolean value that indicates whether the file is a local file name (TRUE) or a remote URL (FALSE). If the file is local, ShowHelp tells the browser to find the file in the "Help" subfolder of the folder containing the application program.

Public Function ShowHelp(strTopic As String, _
  bIsLocal As Boolean) As Boolean

Dim strDir As String
If bIsLocal Then

' Get registry entry pointing to Help

strDir = App.Path + "\Help\"

End If

' Launch topic
Dim hinst As Long

hinst = ShellExecute(Me.hwnd, vbNullString, _

  strTopic, vbNullString, _

  strDir, SW_SHOWNORMAL)

' Handle less than 32 indicates failure
ShowHelp = hinst > 32

End Function


To call ShellExecute, declare the function like this:

Private Declare Function ShellExecute Lib _

"shell32.dll" Alias "ShellExecuteA" _
(ByVal hwnd As Long, _

ByVal lpOperation As String, _

ByVal lpFile As String, _

ByVal lpParameters As String, _

ByVal lpDirectory As String, _

ByVal nShowCmd As Long) As Long


ShowHelp requires an explicit file name or URL. But for context-sensitive Help, it is better to code the application to use context IDs rather than explicit topic names. Use the following sample function, ShowHelp Context, which maps integer context IDs to topic name strings, then calls ShowHelp to launch the topic.

Public Function ShowHelpContext(nContextId As Integer) As Boolean

Dim strTopic As String
Dim bIsLocal As Boolean

bIsLocal = True

Select Case nContextId

Case HH_GADGET_DIALOG

strTopic = "gadget.htm"

Case HH_WHATSIT_DIALOG

strTopic = "whatsit.htm"

Case HH_WIDGET_DIALOG

strTopic = "widget.htm"

Case HH_TECH_SUPPORT:

strTopic = "http://www.mycompany.com"
bIsLocal = False

Case Else

strTopic = "unknown-context.htm"

End Select
ShowHelpContext = ShowHelp(strTopic, bIsLocal)

End Function


Using ShowHelpContext makes maintaining context-sensitive Help much easier because if a topic name changes, only one function has to be modified. Note that the context IDs (for example, HH_GADGET_DIALOG) should be declared as constants that can be shared among the various program modules that use context-sensitive WebHelp.

Const HH_GADGET_DIALOG As Integer = 1

Const HH_WHATSIT_DIALOG As Integer = 2

Const HH_WIDGET_DIALOG As Integer = 3

Const HH_TECH_SUPPORT As Integer = 4