Macros: Automating tasks you perform frequently



Macros: Automating tasks you perform frequently

If you perform a task repeatedly in MS Project, you can automate it with a macro. A Macro is a small program that consists of a recorded series of keystrokes or mouse clicks to help automate a repetitive task.

The macro is stored in a Visual Basic module and can be run whenever you need to perform the task. You record a macro just as you record music with a tape recorder. Then run the macro to repeat, or "play back," the commands.

Before you record or create a macro, plan the steps and commands you want the macro to perform. If you make a mistake when you record the macro, corrections you make will also be recorded. Each time you record a macro, the macro is stored in a module attached to your project.

With Visual Basic for Applications (VBA), you can create more complete macros. You can also edit macros, copy macros from one module to another, copy macros between different projects, rename the modules that store the macros, or rename the macros.

Benefits of Macros

• Automate time-consuming, everyday tasks

• Enhance MS Project to suit the skill level of your users

• Simply creating and distributing project information

Recording a Macro

1. Decide first what it is you want to automate

2. Write down the steps of your macro

• Choose View, Zoom

• Select Entire Project

• Press the OK Button

3. Record the macro

How to Record a Macro

1. On the Tools menu, point to Macro, and then click Record New Macro

2. In the Macro name box, enter a name for the macro

Note: The first character of the macro name must be a letter. Other characters can be letters, numbers, or an underscore. Spaces are not allowed so use an underscore character as a word separator.

3. To run the macro by keyboard shortcut, enter a letter in the Shortcut key box. Use CTRL+ letter for lowercase or CTRL+SHIFT+ letter for uppercase, but not a number or special character

4. In the Store macro in box, choose to store the macro in the active project file or Global file. To make the macro available to the other projects that use the Global file, click Global File

5. Type a description of the macro, its purpose or other information, in the Description box

6. Row References

• Relative: Begins the macro relative to the row that is selected when you run the macro. For example, you can record a macro that puts information into three adjacent rows in the Task Name field of the Gantt Chart view. If you select Relative, the information is placed into three rows, beginning with the selected row, when you run the macro.

• Absolute (ID): Always begins the macro at the same row that you specified when you recorded the macro. For example, if you record a macro that puts information into rows 1, 2, and 3 of the Task Name field of the Gantt Chart view, then the information is always put into rows 1, 2, and 3 when you run the macro, regardless of which row is selected when you run the macro.

7. Column References

• Absolute (Field): Always operates the macro on the same fields that you specified when you recorded the macro. For example, you can record a macro that puts information into the Name, Start, and Duration fields. If this option is selected, the information is always put into the Name, Start, and Duration fields when you run the macro. This occurs even if the fields are arranged differently than when you recorded the macro, and even if these fields are not currently viewable in the Gantt Chart.

• Relative: Runs the macro relative to the column position specified when you recorded the macro. For example, if you record a macro that puts information into the second, third, and fourth columns on the Gantt Chart, then the information is always put into the second, third, and fourth columns when you run the macro, regardless of which column is selected and of the fields displayed in the columns.

• Project will continue to record macros with these settings until you change them or quit Project.

8. Click OK and then carry out the actions you want to record

9. On the Tools menu, point to Macro, and then click Stop Recorder

Run a macro in Microsoft Project

1. On the Tools menu, point to Macro, and then click Macros

2. Select the macro you want to run from the Macro name list

3. Click Run

Note: To interrupt a macro before it finishes, press CTRL+BREAK.

Viewing a macro from the Visual Basic Editor

The Visual Basis Editor allows you to look at in words the steps captured using the Macro Recorder.

1. On the Tools menu, point to Macro, and then click Macros

2. Select the macro you want to run from the Macro name list

3. Click Edit

Assigning a Macro to a Toolbar Button

You can run a macro from a button on a built-in toolbar or a custom toolbar.

1. On the Tools menu, point to Customize, and then click Toolbars

2. If the toolbar that contains the button is not visible, click the Toolbars tab, and then select the check box next to the toolbar name

3. If the button you want to run the macro from is not on a toolbar, click the Commands tab, and then click All Macros in the Categories list. In the Commands list, drag the macro name onto the toolbar

4. If you want to assign a macro to a button already on a toolbar, click the Commands tab, select the button, click Modify Selection, and then click Assign Macro

Macros Available within Microsoft Project 2000

• Adjust_Dates – if you want to change the project start date run this macro to have all constraint dates in the schedule updated with the same difference as well

• Analyze_Timescaled_Data – to create Earned Value graphs in Excel. This macro is also on the Analysis toolbar

• Format_Durations – Allows you to change the time unit in the Duration field from Days to Minutes, Hours or Weeks

• Install_PERT_Analysis

• Calculate_PERT

• PERT_Analysis

• Set_PERT_Weights

• Toggle_PERT_Toolbar

• ResMgnt_TaskEntry – changes the view Gantt Chart and Task Form in a detail format for resource loading

• Rollup_Formatting – will display the names of the tasks marked to be rolled up on the summary task alternatively above and below the task bar

• Toggle_Read_Only – will toggle the files between Read_Only and Read_Write mode

• Update_File – updates a read-only file to the most recently saved version

Web Sites to Obtain Macros



• project/

• scramsoftware.downloads.htm

• office.downloads

Web Sites with Information on Visual Basic

• msdn.scripting

• msdn.microsoft/vba

Macros Available on the MPUG Web Site

• Date Range Filter – Filters out activities within a certain date range. Allows you to specify the range you wish. This will open a window which allows you to input the number of days before and the number of days after time "now" and then filters the information based on what you input.

• Notify Macro – Uses Outlook to notify team members of project tasks that have reached a threshold so that resources assigned to successors can be notified prior to their tasks start.

• Outlook Application Macro – Customizes Outlook application. Allows Microsoft Project team members to email and discuss their project with posts to a common folder.

• HTML Converter – Allows Microsoft Project users to communicate project data across the Internet. Easily converts data tables into web pages.

• Search and Replace Macro – Gives Microsoft users access to advanced Search and Replace functionality.

VISUAL BASIC

Code Basics

This section presents information on code writing mechanics, including breaking and combining lines of code, adding comments to your code, using numbers in code, and following naming conventions in Visual Basic.

Breaking a Single Statement into Multiple Lines

You can break a long statement into multiple lines in the Code window using the line-continuation character (a space followed by an underscore). Using this character can make your code easier to read, both online and when printed. The following code is broken into three lines with line-continuation characters ( _):

Data1.RecordSource = _

"SELECT * FROM Titles, Publishers" _

& "WHERE Publishers.PubId = Titles.PubID" _

& "AND Publishers.State = 'CA'"

Combining Statements on One Line

There is usually one Visual Basic statement to a line. However, you can place two or more statements on a line if you use a colon (:) to separate them:

Text1.Text = "Hello" : Red = 255 : Text1.BackColor = Red

In order to make the code more readable, however, it's better to place each statement on a separate line.

Adding Comments to Your Code

This symbol ( ' ) tells Visual Basic to ignore the words that follow it. Such words are remarks placed in the code for the benefit of the developer, and other programmers who might examine the code later. For example:

' This is a comment beginning at the left edge of the screen.

Text1.Text = "Hi!" ' Place friendly greeting in text box.

Comments can follow a statement on the same line or can occupy an entire line.

Naming Conventions in Visual Basic

While writing Visual Basic code, you declare and name many elements (Sub and Function procedures, variables, constants, and so on). The names of the procedures, variables, and constants that you declare in your Visual Basic code must follow these guidelines:

• They must begin with a letter

• They can't contain embedded periods or type-declaration characters (special characters that specify a data type

• They can be no longer than 255 characters. The names of controls, forms, classes, and modules must not exceed 40 characters

• They can't be the same as restricted keywords

• Auto_Open – A macro named Auto_Open will execute automatically when the project file is first opened.

• Auto_Close – A macro named Auto_Close will execute automatically when the project file is closed.

A restricted keyword is a word that Visual Basic uses as part of its language. This includes predefined statements (If and Loop), functions (Len and Abs), and operators (Or and Mod).

Code Commenting Conventions

All procedures should begin with a brief comment describing what they do. This description should not describe the implementation details (how it does it) because these often change over time, resulting in unnecessary comment maintenance work, or worse, erroneous comments. The code itself and any necessary inline comments describe the implementation.

Arguments passed to a procedure should be described when their purpose is not obvious and when the procedure expects the arguments to be in a specific range. Return values for functions and variables that are changed by a procedure, especially through reference arguments, should also be described at the beginning of each procedure.

Procedure header comments should include the following section headings. For examples, see the "Formatting Your Code" section that follows.

|Section Heading |Comment Contents |

|Purpose |What the procedure does (not how). |

|Assumptions |List of any external variable, control, or other element whose state affects this procedure. |

|Effects |List of the procedure's effect on each external variable, control, or other element. |

|Inputs |Explanation of each argument that is not obvious. Each argument should be on a separate line with inline|

| |comments. |

|Return Values |Explanation of the value returned. |

Remember the following points:

Every important variable declaration should include an inline comment describing the use of the variable being declared

Variables, controls, and procedures should be named clearly to ensure that inline comments are only needed for complex implementation details

At the beginning of your script, you should include an overview that describes the script, enumerating objects, procedures, algorithms, dialog boxes, and other system dependencies. Sometimes a piece of pseudocode describing the algorithm can be helpful

Formatting Your Code

Screen space should be conserved as much as possible, while still allowing code formatting to reflect logic structure and nesting. Here are a few suggestions:

Indent standard nested blocks four spaces

Indent the overview comments of a procedure one space

Indent the highest level statements that follow the overview comments four spaces, with each nested block indented an additional four spaces

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download