How to Disable Control Menu Items Using a WordBasic Macro

ID: Q89728

The information in this article applies to:

SUMMARY

In Microsoft Word for Windows, there is no built-in functionality for disabling commands on the Control menus. This article provides a set of macros you can use to selectively disable some of these commands on the Word for Windows application and document Control menus. These macros give advanced WordBasic macro developers greater customization options when using the Word for Windows program.

MORE INFORMATION

For information on using the Control menu for Microsoft Windows-based application and document windows, see pages 18-23 of the "Microsoft Windows User's Guide," version 3.1.

Word for Windows adds two commands to the application Control menu:

   Switch To    (activates the Windows Task List)
   Run          (runs the Windows Clipboard or Control Panel)

Word for Windows also adds two commands to the document Control menu:

   Next Window  (switches to the next document window)
   Split        (splits the document window into 2 panes)

WordBasic Control Menu commands

The following commands disable or enable application or document Control menu items:

   SysMenu.DisableItem(x)
   SysMenu.EnableItem(x)
   SysMenu.DisableDocItem(x)
   SysMenu.EnableDocItem(x)

The variable "x" indicates the menu item, by number. The first menu item (Restore) is 0 (zero), the second is 1, and so forth. Menu separator lines are also counted. In the Word for Windows application Control menu, the Close command is number 6.

DisableItem(x), EnableItem(x)

These two commands disable and re-enable items on the Word for Windows application (or system) Control menu. Disabled items are unavailable, or dimmed. The following are the only Word for Windows application Control menu items that you can manipulate using these commands:

   Menu Item Number        Menu Item Name
   --------------------------------------
          6                Close
          8                Switch To
         10                Run

DisableDocItem(x), EnableDocItem(x)

These two commands disable and re-enable items on Word's document Control menu. Disabled items are still available; in other words, they are not dimmed. If you choose that menu item, however, no action occurs. The following are the only document Control menu items that you can manipulate using these commands:

   Menu Item Number        Menu Item Name
   --------------------------------------
          0                Restore
          5                Close
          7                Next Window
          9                Split

Create a Macro Library of These Macro Commands

By placing these commands in a macro library, they are globally available for use in other WordBasic macros.

1. From the Tools menu, choose Macro.

2. Type "SysMenu" (without the quotation marks) in the Macro Name box

   and choose the Edit button.

3. In the macro editing window, delete the "Sub MAIN" and "End Sub"
   lines and type the following macro text:

   Word Version 7.0
   ----------------

   Declare Function FindWindowA Lib "User32"(lpClassName$, lpWindowName As
    Long) As Integer
   Declare Function GetFocus Lib "User32"() As Integer
   Declare Function GetSystemMenu Lib "User32"(hWnd As Integer, bRevert As
    Integer) As Integer
   Declare Function ModifyMenuA Lib "User32"(hMenu As Integer, nPosition As
    Integer, wFlags As Integer, wIDNewItem As Integer, lpString$) As
    Integer
   Declare Function GetMenuItemID Lib "User32"(hMenu As Integer, nPos As
    Integer) As Integer
   Declare Function GetSystemMenu Lib "User32"(hWnd As Integer,  bRevert As
    Integer) As Integer

   Sub Main
    DisableItem("Close", 6)
   End Sub

   Sub DisableItem(item$, item)
    MF_BYPOSITION = 1024
    MF_GRAYED = 1
    hWnd = FindWindowA("OPUSAPP", 0)
    hmenu = getsystemmenu(hwnd, 0)
    hItem = getmenuitemid(hMenu, item)
    y =  ModifyMenuA(hmenu, hitem, MF_BYCOMMAND Or  MF_GRAYED, - 10, item$)
   End Sub

   Word version 6.0
   ----------------

   Declare Function FindWindow Lib "User"(lpClassName$, \
     lpWindowName As Long) As Integer
   Declare Function GetFocus Lib "User"() As Integer
   Declare Function GetSystemMenu Lib "User"(hWnd As Integer, bRevert As \
     Integer) As Integer
   Declare Function ModifyMenu Lib "User"(hMenu As Integer, nPosition As \
     Integer, wFlags As Integer, wIDNewItem As Integer, lpString$) As \
     Integer
   Declare Function GetMenuItemID Lib "User"(hMenu As Integer, nPos As \
     Integer) As Integer
   Declare Function GetSystemMenu Lib "User"(hWnd As Integer,  bRevert As \
     Integer) As Integer

   Sub Main
    DisableItem("Close", 6)
   End Sub

   Sub DisableItem(item$, item)
    MF_BYPOSITION = 1024
    MF_GRAYED = 1
    hWnd = FindWindow("OPUSAPP", 0)
    hmenu = getsystemmenu(hwnd, 0)
    hItem = getmenuitemid(hMenu, item)
    y =  ModifyMenu(hmenu, hitem, MF_BYCOMMAND Or  MF_GRAYED, - 10, item$)
   End Sub

   Word version 2.x
   ----------------

   Declare Function GetFocus Lib "User"() As Integer
   Declare Function GetParent Lib "User"(hWnd As Integer) As Integer
   Declare Function FindWindow Lib "User"(lpClassName$, \
   lpWindowName As Long) As Integer
   Declare Function EnableMenuItem Lib "User"(hMenu As Integer, \
   IDEnableItem As Integer, wEnable As Integer) As Integer
   Declare Function GetSystemMenu Lib "User"(hWnd As Integer, \
   bRevert As Integer) As Integer

   ' Disable a System Menu item
   Sub DisableItem(item)
   MF_BYPOSITION = 1024
   MF_GRAYED = 1
   hWnd = FindWindow("OPUSAPP", 0)
   hMnu = GetSystemMenu(hWnd, 0)
   y = EnableMenuItem(hMnu, item,  MF_BYPOSITION Or MF_GRAYED)
   End Sub

   ' Restore a System Menu item
   Sub EnableItem(item)
   MF_BYPOSITION = 1024
   MF_ENABLED = 0
   hWnd = FindWindow("OPUSAPP", 0)
   hMnu = GetSystemMenu(hWnd, 0)
   y = EnableMenuItem(hMnu, item,  MF_BYPOSITION Or MF_ENABLED)
   End Sub

   ' Disable a Document System Menu item
   Sub DisableDocItem(item)
   MF_BYPOSITION = 1024
   MF_DISABLED = 2
   hWnd = GetFocus
   hWnd = GetParent(hWnd)
   hMnu = GetSystemMenu(hWnd, 0)
   y = EnableMenuItem(hMnu, item,  MF_BYPOSITION Or 2)
   Bye:
   End Sub

   ' Restore a Document System Menu item
   Sub EnableDocItem(item)
   MF_BYPOSITION = 1024
   MF_ENABLED = 0
   hWnd = GetFocus
   hWnd = GetParent(hWnd)
   hMnu = GetSystemMenu(hWnd, 0)
   y = EnableMenuItem(hMnu, item,  MF_BYPOSITION Or MF_ENABLED)
   End Sub

4. Press CTRL+F4 to close the macro window. Choose Yes when prompted
   to save changes to SysMenu.

Using the SysMenu Macros

The following macro disables items on the Word for Windows application, or system Control menu. After you run this macro, open the Word for Windows application Control menu and notice that the Close, Switch To and Run commands are unavailable.

   Sub MAIN
   SysMenu.DisableItem(6)   ' disable Close
   SysMenu.DisableItem(8)   ' disable Switch To
   SysMenu.DisableItem(10)  ' disable Run
   End Sub

The following macro enables the previously-disabled items on the Word for Windows application Control menu. After you run this macro, open the Word for Windows application Control menu and notice that the commands are once again available.

   Sub MAIN
   SysMenu.EnableItem(6)   ' enable Close
   SysMenu.EnableItem(8)   ' enable Switch To
   SysMenu.EnableItem(10)  ' enable Run
   End Sub

The following macro disables the Restore, Close, Next Window and Split items on the current Word for Windows document Control menu. After you run this macro, open the document Control menu. The disabled items appear available, but if you choose them, nothing happens.

   Sub MAIN
   SysMenu.DisableDocItem(0)   ' disable Restore
   SysMenu.DisableDocItem(5)   ' disable Close
   SysMenu.DisableDocItem(7)   ' disable Next Window
   SysMenu.DisableDocItem(9)   ' disable Split
   End Sub

The following macro enables the previously-disabled items on the document Control menu. After you run this macro, open the Word for Windows document Control menu. If you choose the re-enabled menu items, the commands function correctly.

   Sub MAIN
   SysMenu.EnableDocItem(0)   ' enable Restore
   SysMenu.EnableDocItem(5)   ' enable Close
   SysMenu.EnableDocItem(7)   ' enable Next Window
   SysMenu.EnableDocItem(9)   ' enable Split
   End Sub

WARNING: ANY USE BY YOU OF THE CODE PROVIDED IN THIS ARTICLE IS AT YOUR OWN RISK. Microsoft provides this macro code "as is" without warranty of any kind, either express or implied, including but not limited to the implied warranties of merchantability and/or fitness for a particular purpose.

Reference(s):

"Microsoft Word for Windows User's Guide," version 2.0, pages 51, 56

"Microsoft Windows User's Guide," version 3.1, pages 18-23

"Microsoft Windows User's Guide," version 3.0, pages 28-29, 32, 60

"Microsoft Windows Software Development Kit (SDK)"

KBCategory: kbusage kbmacro kbhowto KBSubcategory: Additional query words: winword2 6.0 6.0a 6.0c 2.0 2.0a 2.0a-CD winword 7.0 word95 word7 word6 2.0b win31 winword api winapi

Version           : 2.0 2.0a 2.0a-CD 2.0b 6.0 6
Platform          : WINDOWS

Last Reviewed: February 5, 1998