PerfectScript Journal

September 1999: Adding a Menu item Via a Macro

Many people have asked if they can add a menu item to WordPerfect using macro commands. The answer is Yes. There are several commands that allow you to manipulate menu items. We will cover three of them here.

When you add a menu item, it is added to the current menu in the current template. Thus, when you exit WordPerfect, then come back in, the menu item should still be available.

Adding a menu item to play a macro

PerfectScript contains a command that allows you to add a macro to the menu. This command is MenuAddMacro, and it has five parameters:
MenuAddMacro (Menu; Name; MacroName; Hint; Offset)

All of the menu commands use the parameters below, or similar parameters:

Menu

The string that will go in the first parameter, Menu, is a pre-defined region name representing the the particular menu you wish to add the item to. The possible values are:
  • FileMenu
  • EditMenu
  • ViewMenu
  • InsertMenu
  • FormatMenu
  • ToolsMenu
  • WindowMenu
  • HelpMenu

Name

In the Name parameter, specify the text of the menu item as you want it to appear on the menu.

MacroName or ProgramName

In the MacroName parameter, specify the exact name of the macro you wish to trigger with the new menu item. You shoud specify path as well as filename. If the macro does not exist, the MenuAddMacro command will fail. The same is true if you are adding an external program with the ProgramName parameter.

Hint

In the Hint parameter, specify the text for the Tool Tip.

Offset

In the Offset parameter, specify the position for the new menu item. If you specify 0, the item will be placed at the top of the menu. If you specify -1, the item will be placed at the bottom of the menu. If you specify a value that is out of range, an error will occur when the command runs.

Here's an example of a MenuAddMacro command:

MenuAddMacro (Menu:"HelpMenu"; Name:"Count word"; 
   MacroName:?PathMacros+"countword.wcm"; 
   Hint:"Count the number of times a word appears in the current document"; 
   Offset:-1)

Adding a program to the menu

Adding an external executable program is similar to add in a macro. Here's an example:
MenuAddProgram (Menu: "HelpMenu"; Name: "Calculator"; 
   ProgramName:"calc.exe"; Hint:"Calculator"; Offset:-1)

Adding a separator line

Here's how to add a separator line:

MenuAddSeparator (Menu:"HelpMenu";Offset:3)

Tips for adding menu items

Use these commands with care. Each time you run a macro that adds a menu item, it will be added again. You can end up with the same menu item on the menu multiple times.

In some versions of WordPerfect, when the user edits the menu, any menu items added with these menu commands will be automatically deleted without warning. Make sure you thoroughly test your macro before distribution.

It is not an easy thing to determine if a particular menu item has been added, or to delete a particular menu item once it has been added. You will most likely need to use DLL calls to the Windows API to accomplish these tasks.


                           
Copyright Notice: The information included in the PerfectScript Journal is protected by US Copyright. The author grants you the right to use the routines in your own macros as needed. You may not sell, distribute, or publish them in any form.
If you choose to use the information here, you do so entirely at your own risk. No representations are made regarding the fitness of this information for your particular purpose, or for your ability or inability to use the information. You are advised to make backups of all relevant files before implementing any suggestion or technique.
© Copyright 1999 by J. Jeppson.