WD2000: AutoExit Macro Does Not Remove Menu Items Added by AutoExec Macro

ID: Q245765


The information in this article applies to:
  • Microsoft Word 2000


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, including, but not limited to, the implied warranties of merchantability and/or fitness for a particular purpose. This article assumes that you are familiar with the programming language being demonstrated and the tools used to create and debug procedures. Microsoft support professionals 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 needs. If you have limited programming experience, you may want to contact a Microsoft Certified Solution Provider or the Microsoft fee-based consulting line at (800) 936-5200. For more information about Microsoft Certified Solution Providers, please see the following page on the World Wide Web:

http://www.microsoft.com/mcsp/
For more information about the support options available from Microsoft, please see the following page on the World Wide Web:

http://www.microsoft.com/support/supportnet/overview/overview.asp

For more information about using the sample code in this article, click the article number below to view the article in the Microsoft Knowledge Base:
Q212536 OFF2000: 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 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:


  3. C:\Program Files\Microsoft Office\Office\Startup
  4. On the Tools menu, point to Macro and click Visual Basic Editor.


  5. In the Visual Basic Editor window, on the Insert menu, click Module.


  6. Copy the following code and paste it into the module window.


  7. 
    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 
  8. On the File menu, click Close and Return to Microsoft Word.


  9. In Word, click Save on the File menu to save the changes to your template.


  10. On the File menu, click Close to close the file.


Additional query words: vba autoexec autoexit menu commandbars reset

Keywords : kbdta kbmacroexample kbwordvba wd2000
Version : WINDOWS:2000
Platform : WINDOWS
Issue type : kbhowto


Last Reviewed: November 10, 1999
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.