Associating a Help Topic with a VBA Function

If you create custom worksheet functions with VBA, you might want to associate a Help file and context ID with each function. After these items are assigned to a function, the Help topic can be displayed from the Insert Function dialog box by pressing F1.

To specify a context ID for a custom worksheet function, follow these steps:

1. Create the function as usual.

2. Make sure that your project has an associated Help file (refer to the preceding section).

3. In the VBE, press F2 to activate the Object Browser.

4. Select your project from the Project/Library drop-down list.

5. In the Classes window, select the module that contains your function.

6. In the Members Of window, select the function.

7. Right-click the function and then select Properties from the shortcut menu. This displays the Member Options dialog box, as shown in Figure 24-9.

Member Options

1 1

Name: | Squared

« 1


Cancel |

| Returns the square of an argument ^

Help File: Help Context ID:

C:\Documents and 1 2000


i i

Figure 24-9: Specify a context ID for a custom function in the Member Options dialog box.

Figure 24-9: Specify a context ID for a custom function in the Member Options dialog box.

8. Enter the context ID of the Help topic for the function. You can also enter a description of the function.

The Member Options dialog box does not let you specify the Help file. It always uses the Help file associated with the project.

You might prefer to write VBA code that sets up the context ID and Help file for your custom functions. You can do this by using the MacroOptions method. The following procedure uses the MacroOptions method to specify a description, Help file, and context ID for two custom functions (AddTwo and Squared):

Sub SetOptions()

' Set options for the AddTwo function

Application.MacroOptions Macro:="AddTwo", _

Description:="Returns the sum of two numbers", _ HelpFile:=ThisWorkbook.Path & "\myfuncs.chm", _ HelpContextID:=1000

' Set options for the Squared function

Application.MacroOptions Macro:="Squared", _

Description:="Returns the square of an argument", _ HelpFile:=ThisWorkbook.Path & "\myfuncs.chm", _ HelpContextID:=2000

End Sub

0 0

Post a comment