|< Day Day Up >|
The macro you recorded in the preceding section will work in every situation because the objects it operates on are not specific to any one project. Zooming for an entire project will work for any project. With a simple change in the options, however, you can create a macro that can be recorded with no problems but does not work in all cases. This section walks through a similar example and shows how to correct the problems that occur when you record a specific macro.
This time, rather than using the Zoom, Entire Project command, you will use Zoom, Selected Tasks. This might be a more useful option when you want to look at a set of particular tasks you are working on and the duration of those tasks is short compared to the entire project.
The process is the same as recording the previous macro:
To test the new macro, reset the timescale so that the whole project is showing, and then select a few tasks (select different ones from the ones you selected while recording the macro) and run the new macro. Project zooms, but it zooms to show the tasks you selected when recording the macro and ignores the tasks you have just selected. Because this macro is expected to zoom the timescale for the selected tasks and it ignores your current selection, it is clearly not working correctly.
The problem with this macro is what the recorder is recording. To see what is recorded, open the Visual Basic Editor (VBE). To open the VBE, press Alt+F11 or select Tools, Macros, Visual Basic Editor.
When you open the VBE, you see something similar to Figure 1. In the upper-left corner is the Project Explorer. Because you saved your macro in the GLOBAL.MPT file, you need to expand that file in Project Explorer and look in the Modules folder. Project creates a module for each macro you record. The highest-numbered module is the last one you recorded. If this is your first time recording macros, you should see Module1 , which contains the first version, and Module2 , which contains the second version. Double-click the highest-numbered module (it is likely to be Module2 ) to open it to display the code window.
Figure 1. The default view of the Visual Basic editing screen is divided into three main areasthe Project window and Properties window on the left and the Code window on the right.
Project does not do a good job of naming modules for you. It supplies names such as Module1 and Module5 . If you have some macros you want to use or work on later, you should change the name of the module to something more appropriate so that you can easily find it. You can't change the name in the Project Explorer window, but you can change it in the Properties window. It opens by default just under the Project Explorer window, but if you don't see it, you can display it by pressing F4 or selecting it in the View menu. To rename the module, just type over the existing name.
When a module opens, you see what was recorded by the recorder. A number of separate actions are grouped together as a macro or, more precisely, a procedure. Each line in that procedure is a comment, a single statement, or both. Every macro you record begins with a line stating that it is a subroutine (that is, a type of procedure), followed by the name of the macro and any parameters that the macro operates on. In the case of the ZoomSelected macro, you see the following code:
Sub ZoomSelected() ' This macro will zoom the timescale to show all the selected tasks SelectRow Row:=1 ZoomTimescale Selection:=True End Sub
The first line contains the name you gave the macro. Immediately after this line is the description that you entered or that Project entered for you. The description is in the form of a comment, so it begins with an apostrophe ( ' ). Everything following on that line is interpreted as a comment. You can create as many comments as you like, and a comment can be on the same line as another statement, but it must follow the statement. Following this are the actions to occur and at the end: A line states end sub , which indicates the end of the macro. You can see from this code that you selected a task and then performed an action on the timescale based on that selected task.
The problem here is that you made the selection of the task part of the macro, so whenever the macro is run, that task is again selected. Although you could go back and rerecord the macro, the VBE enables you to simply delete the parts of the macro you don't want. In this case, you need to delete the line that starts with SelectRow .
After you make changes in the VBE, switch back to your project, select a few tasks, and run the macro again. At this point it should work fine. Try not selecting any tasks, and then run the macro again. Project generates an error (see Figure 2). In this case, click End. Debugging is covered later in this chapter.
Figure 2. Error windows present you with some detail about what the error is and allow you to end, debug, or go to help.
The problem is that you are asking Project to perform an action, but you haven't selected anything that it can use to perform that action.
|< Day Day Up >|