WD97: AutoExit Macro Does Not Remove Menu Items Added by AutoExec Macro (245227)



The information in this article applies to:

  • Microsoft Word 97 for Windows

This article was previously published under Q245227

SUMMARY

If you place a template file (.dot) in the Office Startup folder (C:\Program Files\Microsoft Office\Office\Startup) and include an AutoExec macro in the template, you can add new menu items to the standard Word menus such as the File and Help menus. However, you cannot remove those menu items by resetting the menus in an AutoExit macro in the template.

MORE INFORMATION

Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements.
For more information about how to use the sample code in this article, click the article number below to view the article in the Microsoft Knowledge Base:

173707 OFF97: How to Run Sample Code from Knowledge Base Articles


When you start Word, AutoExec macros in templates that are in the Office Startup folder will run. Those macros may add new menu items to standard menus or add new menus, or both. These changes are made in the Normal.dot template.

When you quit Word, the Normal.dot template is closed before any AutoExit macros run. Therefore, when you start Word again, the menu items added by the AutoExec macro may still be there, even if the AutoExit macro includes code similar to the following to reset menus:
For Each cn In CommandBars
   cn.Reset
Next
				
The following steps illustrate how to create a template with AutoExec and AutoExit macros to add a new menu item to the File menu when Word starts and remove the menu item when Word closes.
  1. Start Word 97 and open a new bank document.
  2. On the File menu, click Save and save the document as Example.dot, a Word template. Save the template to the following folder:

    C:\Program Files\Microsoft Office\Office\Startup

  3. On the Tools menu, point to Macro and click Visual Basic Editor.
  4. In the Visual Basic Editor window, on the Insert menu, click Module.
  5. Copy the following code and paste it into the module window.
    Sub AutoExec()
        
        Set cb = CommandBars("Menu Bar").Controls("File")
    
        'Add new menu item to File menu
        TheMenuString = "New Menu Item"
        TestString = cb.Controls(9).Caption
        
        'Check to see if New Menu Item has already been added to
        'the File file.
        If TestString <> TheMenuString Then
        
            'Add menu items to New Menu Item
            Set NewMenuItem = cb.Controls.Add(Type:=msoControlPopup, _
               Before:=9, Temporary:=True)
    
            cb.Controls(9).Caption = TheMenuString
            Set NewCtrl1 = NewMenuItem.Controls.Add(Temporary:=True)
            Set NewCtrl2 = NewMenuItem.Controls.Add(Temporary:=True)
            Set NewCtrl3 = NewMenuItem.Controls.Add(Temporary:=True)
            Set NewCtrl4 = NewMenuItem.Controls.Add(Temporary:=True)
    
            NewCtrl1.Caption = "Item 1"
            NewCtrl1.OnAction = "Item1Macro"
    
            NewCtrl2.Caption = "Item 2"
            NewCtrl2.OnAction = "Item2Macro"
            
            NewCtrl3.Caption = "Item 3"
            NewCtrl3.OnAction = "Item3Macro" '
            
            NewCtrl4.Caption = "Item 4"
            NewCtrl4.OnAction = "Item4Macro"
            
        End If
    
        cb = Null
        NewMenuItem = Null
        NewCtrl1 = Null
        NewCtrl2 = Null
        NewCtrl3 = Null
        NewCtrl4 = Null
        
    End Sub
    
    Sub AutoExit()
    
        'Normal.dot has been closed by the time this macro runs.
        'Open Normal.dot.
        NormalTemplate.OpenAsDocument
        
        Set FileMenuItem = CommandBars("Menu Bar").Controls("File")
    
        'If the specified menu item is found, delete it.
        If FileMenuItem.Controls(9).Caption = "New Menu Item" Then
            FileMenuItem.Controls(9).Delete
        End If
    
        'Save Normal.dot
        Documents.Save NoPrompt:=True
        
        FileMenuItem = Null
        
    
    End Sub
    
    Sub Item1Macro()
        MsgBox "Item 1 selected"
    End Sub
    
    Sub Item2Macro()
        MsgBox "Item 2 selected"
    End Sub
    
    Sub Item3Macro()
        MsgBox "Item 3 selected"
    End Sub
    
    Sub Item4Macro()
        MsgBox "Item 4 selected"
    End Sub
    					
  6. On the File menu, click Close and Return to Microsoft Word.
  7. In Word, click Save on the File menu to save the changes to your template.
  8. On the File menu, click Close to close the file.

Modification Type:MinorLast Reviewed:10/11/2006
Keywords:kbdtacode kbhowto kbProgramming KB245227