Three Birds One Stone

If you like the idea behind the example of displaying a form every time a specific plan was opened, but want something a little more robust, you can avoid three of its potential detractions by working with the Global file (the ProjectGlobal project) instead of a particular plan (the VBAProject project):

• To make the code frmWelcome.Show run every time Microsoft Project starts, expand the ProjectGlobal project and double-click the ThisProject object instead of the This-Project object under the VBAProject project.

• Because the StatusBar property is reset to its default when Microsoft Project closes, you don't need the BeforeClose event code.

• Because of the special nature of the Global file, users don't see the macro virus security dialog box. This means there is no chance that the user will choose to disable the code that displays your form.

A more sophisticated version of this example is one that adds a new menu item or toolbar that can be used to access the forms you have created. This method integrates your forms with the Microsoft Project interface even more closely, although you probably wouldn't bother doing so for a simple, limited-use form such as the one you just created.

Follow these steps to create two Project event procedures that add a new item to the View menu (see Figure 31-25) that, when clicked, displays the frmWelcome form: h

1 Double-click ThisProject in the Project Explorer to open the Code window.

2 In the Objects box, click Project, and then type the following code into the Project o object's Open event:

Dim objViewBar As CommandBar Dim objNewItem As CommandBarButton Dim intTotalItems As Integer

Set objViewBar = Application.CommandBars("View") intTotalItems = objViewBar.Controls.Count

If objViewBar.Controls(intTotalItems).Caption <> "Show my UserForm" Then Set objNewItem = objViewBar.Controls.Add(Type:=msoControlButton) objNewItem.Caption = "Show my UserForm" objNewItem.BeginGroup = True objNewItem.OnAction = "frmWelcome.Show" End If

Set objNewItem = Nothing Set objViewBar = Nothing

3 In the Procedures/Events box, click BeforeClose, and then type the following code into the Project object's BeforeClose event:

Dim objViewBar As CommandBar Dim intTotalItems As Integer

Application.StatusBar = False

Set objViewBar = Application.CommandBars("View") intTotalItems = objViewBar.Controls.Count

If objViewBar.Controls(intTotalItems).Caption = "Show my UserForm" Then objViewBar.Controls(intTotalItems).Delete End If

Set objViewBar = Nothing

Because code tied to the Project object is changing two application-level items, you need this additional code to "clean up" when the plan is closed by resetting the StatusBar property to its default value and removing the Show My UserForm command from the View menu. This procedure ensures that Microsoft Project is in its default state when other plans are opened.

Note Until the plan is closed, the new View menu item is visible from every other open plan. Clicking it from one of those plans, however, results in an error.

View | Insert Format Tools Calendar Gantt Chart Network Diagram Task Usage | v | Tracking Gantt Resource Graph Resource Sheet Resource Usage More Views... Table: Entry ►

View Bar

Header and Footer... Zoom... I Show my UserForm

Figure 31-25. The new Show My UserForm command appears at the bottom of the View menu.

Project Management Made Easy

Project Management Made Easy

What you need to know about… Project Management Made Easy! Project management consists of more than just a large building project and can encompass small projects as well. No matter what the size of your project, you need to have some sort of project management. How you manage your project has everything to do with its outcome.

Get My Free Ebook


Post a comment