Writing a Macro in the Editor

Writing macros directly in the editor can seem a little daunting if you don't have much experience with programming or if you aren't comfortable with the object model. There are few tools to help you start (other than the Add Procedure window, which you can find by clicking Insert, Procedure), so one way to begin is to simply record a macro and then edit it in the Visual Basic Editor.

For the purposes of this discussion, though, you can simply follow these steps to create a macro in the editor directly:

1 Create a new project and then open the Visual Basic Editor by pressing Alt+F11.

2 Double-click the ThisProject object in the Project Explorer.

The Code window opens with the cursor in the upper-left corner.

3 Type the following code in the Code window:

Function strNewTask(strName As String, intBefore As Integer) As String ActiveProject.Tasks.Add strName, intBefore

If ActiveProject.Tasks(intBefore).Name = strName Then strNewTask = "Success!"

Else strNewTask = "Failed to create task!" End If End Function

Note Be sure to capitalize variable names exactly as they are shown here.

As you enter the code, you will notice some things that the editor does:

■( As you type strName As in the function definition, a list displays classes and data types. As you start typing Stri ng, the list scrolls until String is selected (see Figure 31-12). You can continue typing the word, select String from the list, or press Ctrl+Space to complete the word.

■( When you press Enter at the end of the line containing the function definition, several things happen at once: As the word "function" is capitalized, a blank line and End Function are inserted, and Visual Basic keywords (Function, String, Integer, and so on) are colored blue.

■( When you press Enter at the end of a line containing an equals sign (=), the editor puts spaces around the equals sign.

B

31PrdDev.mpp - ThisProject (Code)

| | (General) » | (Declarations)

—t

function strNewTask: (strName as Str|

String

^ Subproject

^ Subprojects

® Sync

® Table

tîiTableField

tîiTableFields

V

I II

Figure 31-12. The Auto List Members feature scrolls through an alphabetical list of possible matches for data types and constants as you type.

4 Type the following code in the Code window:

Sub AddTask()

Dim strTaskName As String Dim intTaskNum As Integer strTaskName = InputBox$("Type a name for the new task:") intTaskNum = CInt(InputBox$("Type the position for the new " & _ "task in the task list:"))

MsgBox strNewTask(strTaskName, intTaskNum) End Sub

As you enter this code, the same actions occur as when you entered code for the function, but something really interesting also happens: Typing strnewtask( causes the Quick Info feature to list information about the arguments and return value for strNewTask (see Figure 31-13).

End Suto : . : SlitSm/H'As S!nn<! intBeforeAs integer As String |

Figure 31-13. The Quick Info feature lists details about procedures you create and about items in the object model.

Returning to the Microsoft Project interface and opening the Macros dialog box (click Tools, Macro, Macros) shows the AddTask procedure as a new macro.

Tip Switch between Microsoft Project and the Visual Basic Editor

After the Visual Basic Editor has been opened, Alt+F11 toggles between Microsoft Project and the editor.

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