Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

The Custom Menu that wont Remove

Status
Not open for further replies.

theburst

Technical User
Jun 27, 2007
9
US
First of all I'm working with VBA in Microsoft Project

So I have a project file that I want to install custom menus. I have figured out that I can create a macro to install that menu at file startup. Then I can create another macro that removes it at file shutdown. However, for whatever reason if I close the file and then start up a new file the menu remains!! I'm puzzled.

I don't think my code is wrong, and it seems to work. When the file is closing the menu seems to disappear. But when I start it up another new project it persists :-(.

Thoughts?

Here is the code:

Code:
Option Explicit

[b]Private Sub Project_Open(ByVal pj As Project)[/b]
    Dim aMenuObject As Office.CommandBarControl
    Dim MenuExists As Boolean
    
    'Make sure the non standard menu items have been removed...
    Call KillMyMenu
    
    For Each aMenuObject In Application.CommandBars("Menu Bar").Controls
        If aMenuObject.Tag = "MyMenu" Then
        MenuExists = True
            Exit For
        End If
    Next aMenuObject
    
    If (Not MenuExists) Then Call AddMenuAndButtons
End Sub

[b]Private Sub AddMenuAndButtons()[/b]
[green]' Add a menu to the 'end' of MS Project's Menu Bar
' Add three commands to newly created menu
  ' These commands are Macro1, Macro2, Macro3
  ' These commands run the (public) macros Macro1, Macro2, Macro3 in the Active Project[/green]
    Dim aMenuObject As Office.CommandBarControl
    Dim aButton As Office.CommandBarButton
    
    'MsgBox "Entering AddMenuButtons()"
    
    Set aMenuObject = Application.CommandBars("Menu Bar").Controls.Add(Type:=msoControlPopup)
    aMenuObject.Caption = "Precedence Macros"
    aMenuObject.Tag = "MyMenu"
                                                                   
[green]'This is where it defines the buttons, however there
'are 8 others i have removed to keep it short(er)[/green]
    
    Set aButton = aMenuObject.Controls.Add(Type:=msoControlButton, _
                                  ID:=40001, _
                                    Parameter:="Macro " & Application.ActiveProject.Name & "!FlagTasks")
    aButton.Caption = "FlagTasks"
                                                                                                                                                                                                                                                                            
    MsgBox "P-Net Macros installed in menu next to Help"
    Set aButton = Nothing
    Set aMenuObject = Nothing
End Sub

[b]Public Sub KillMyMenu()[/b]
[green]' Remove all instances of 'MyMenu' from MS Project's Menu Bar[/green]
    Dim oneMenu As Office.CommandBarControl
    Dim oneButton As Office.CommandBarButton
    
    For Each oneMenu In Application.CommandBars("Menu Bar").Controls
        If (oneMenu.Tag = "MyMenu") Then
            For Each oneButton In oneMenu.Controls

                Call oneButton.Delete
            Next oneButton
            Call oneMenu.Delete
        End If
    Next oneMenu
    
    Set oneButton = Nothing
    Set oneMenu = Nothing
End Sub

[b]Private Sub Project_BeforeClose(ByVal pj As Project)[/b]
    Call KillMyMenu
End Sub
 
I suspect that your have somehow added your menu definition to whatever Project's version of the "normal.dot" template is.
 
Well thats what I was thinking too. The file has a Global.MPT which are the user settings.

This is what I've done to test it.

1.
-Open the file, and it installs.
-Close the file with breakpoints, and the menu is most definitely removed.
-Open a blank file and the menu is there!!! (WHY?)

2.
-Open the file, and it installs.
-Remove the menu manually, via clicking Custimize and dragging it off.
-Close file and open a blank file, the menu is not there.

So for some reason there is a difference in what my code is doing and what a user defined drag and customize is doing. But I'm not sure what!?

I'm pretty sure it doesn't have to do with Global.mpt.

Thoughts?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top