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:
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