Pert Master Macro Tutorials
Pert Master Macro Tutorials
Table of Contents
1. 2. 3. 4. 5. Macro Tutorials Macro Tutorial - Report writing macro Macro Tutorial - Global report writing macro Macro Tutorial - Event driven macro: Conditional formatting PDF Documentation and Printing Help 1 1-5 5-6 6-9 9-10
Macro Tutorials
These tutorials take you through the steps of creating macros in Pertmaster.
The ability to create and run macros will depend on the version of Pertmaster you are using see Pertmaster Products (on-line documentation) If you are reading this is in the on-line help you may want to print out this tutorial or use the PDF booklet installed in the Pertmaster Documentation folder - see PDF Documentation and Printing Help (Section 5)
Tutorial contents: 1. 2. 3. 4. Report writing macro Global report writing macro Event driven macro: Conditional formatting Creating a form based macro
Tutorial_Macros_Report(v1.1).plan Plan that is used in the Report writing macro tutorials. Tutorial_Macros_ConditionalFormat(v1.2).plan Plan that is used in the Event driven macro tutorial.
Open the sample plan installed with the Pertmaster named "Tutorial_Macros_Report(v1.1).plan" Tools | Macros | Visual Basic Editor - You will be taken to Microsoft Visual Basic application. Expand "PertmasterPlan(Tutorial_Macros_Report(v1.1).Expand "PertmasterPlan (Tutorial_Macros_Report(v1.1).plan)". Right-click on ThisPlan and select View Code. The code window will be displayed on the right.
2. Referencing MS Excel The macro will use MS Excel to create our report. To use MS Excel a reference to it must be added to the macro.
In Visual Basic Editor: Select "PertmasterPlan(Tutorial_Macros_Report(v1.1).plan)" Tools | References | Find and check Microsoft Excel 9.0 Object Library (or if not 9.0 then whichever version of Excel is available). Click OK .
Copy and paste the following code in into the code window .
Sub CreateReport() ' menutitle=Excel Task Report If MsgBox("This will create an Excel report. Continue?", vbOKCancel) = vbOK Then
Set m_objExcel = New Excel.Application ' start up Excel m_objExcel.Visible = False ' do not display Excel yet Set m_objWorkbook = m_objExcel.Workbooks.Add 'create new workbook Set objSheet = m_objWorkbook.Sheets(1) ' Title nRow = 1 objSheet.Cells(nRow, nRow = nRow + 1 objSheet.Cells(nRow, objSheet.Cells(nRow, objSheet.Cells(nRow, nRow = nRow + 1
"A") = "Pertmaster Report - " & ActivePlan.Information.Title "A") = "ID" "B") = "Description " "C") = "Comment"
' Populate spreadsheet with task information Set objTasks = ActivePlan.Tasks For Each objTask In objTasks objSheet.Cells(nRow, "A") = objTask.ID objSheet.Cells(nRow, "B") = objTask.Description objSheet.Cells(nRow, "C") = objTask.Comment nRow = nRow + 1 Next ' Adjust column widths objSheet.Columns("B:C").EntireColumn.AutoFit m_objExcel.Visible = True End If End Sub
4. Saving the macro Plan macros are saved with with the plan file (.plan). The save command can be initiated from within Microsoft Visual Basic or the Pertmaster application.
Close the Microsoft Visual Basic application - File | Close and return to Pertmaster Risk Expert . Macros run faster if the MS Visual Basic application is closed.
Select 'PertmasterPlan (Tutorial_Macros_Report(v1.1).plan' from the dropdown list. Click on 'CreateReport'. Click Run . If you receive the message "Compile error: User-defined type not defined" it is likely that the reference created in step 2 above has not been completed. Reset the macro ( Run | Reset) and check the reference has been added correctly.
"This will create an Excel report. Continue?". Click OK . MS Excel will be opened and populated with data:
Good work! Move onto the next tutorial to learn about Global macros.
Start up Pertmaster. Close down any open plans using File | Close . Tools | Macros | Macro Manager | Create New Macro File. Enter ExcelReport in the Macro Project Name. Keep the default Macros Folder .
Click OK and the new macro will be added. Click OK in the Macro Manager dialog and the Microsoft Visual Basic editor will be opened.
2. Reference MS Excel The macro will use MS Excel to create the report. To use MS Excel a reference to it must be added to the macro.
Tools | References | Find and check Microsoft Excel 9.0 Object Library (or if not 9.0 then whichever version of MS Excel is available).
Copy and paste the visual basic code used in the Plan macro tutorial into the open code window replacing any existing code: Plan macro tutorial (Section 2)
4. Run the Global Macro A global macro can be run from any plan that is open. We will now open up a plan and run the macro.
File | Close and return to Pertmaster Risk Expert. Open any plan using File | Open. Tools | Macros | Macros.
Excellent! You have created a Global macro that can be used with any plan. That covers the basics of Global macros. To find out about macros have a look at the other macro tutorials.
1. Open the conditional formatting sample plan The first step is to create a macro that will perform the function we require, in this case formatting the task bar color. To save you time the macro has already been entered in the example project so all we need to do is make the macro event driven.
Start up the Pertmaster application. Open the sample file installed with Pertmaster 'Tutorial_Macros_ConditionalFormat(v1.3).plan' Click Enable when the macro warning message is displayed. Tools | Macros | Visual Basic Editor (or use Alt + F11 ) Double-click on modFormat to display the code.
In Modules you should see a sub routine called conditionalFormat() and a function called GetColor . The GetColor function is called by the conditionalFormat sub routine. The code is shown below:
' -------------------------------------------------------------------' ' ' ' ' ' ' ' ' ' ' ' Title: Tutorial Conditional formatting Version: 1.04 Description: Task bars are coloured automatically based on values entered user text 1. Change values in user text 1 to see effect. Created by: Tim Stevens (Pertmaster Team) Created in: Pertmaster Project Risk Expert 8.0.99 Modifications: Values converted from v7 Text fields to use v8 Task codes Notes: --------------------------------------------------------------------
' Set up objTasks as a collection of all the tasks in the plan ' objTasks can then be used to loop through all the tasks Set objTasks = ActivePlan.Tasks
For Each objTask In objTasks If objTask.UserFields.UserField(COLORFIELD) = "" Then objTask.Style.BarStyle.Color = -1 ' -1 indicates <default> objTask.Style.BarStyle.FillStyle = -1 ' -1 indicates <default> Else objTask.Style.BarStyle.Color = GetColour(objTask.UserFields.UserField(COLORFIELD)) objTask.Style.BarStyle.FillStyle = efsSolid End If Next End Sub
Private Function GetColour(strColour As String) As Long strColour = UCase(strColour) 'make it non case sensitive Select Case strColour Case "" GetColour = -1 Case "BLACK" GetColour = RGB(0, 0, 0)
2. Making the macro event driven The conditionalFormat macro can be run at any time. However we would like this macro to be run whenever a change is made to Task Color . An event that is triggered when a change is made to Task Color is the AfterPlanModify. This event is triggered by any change to the plan. Though we are only interested in changes to Task Color we must use this event as there are no events that are only triggered by a change to the user text field. In this step we will add the event AfterPlanModify and enter a line of code that runs the conditionalFormat macro.
In the tree structure find and double-click on ThisPlan. This opens the code window. In the code window, click on the the first drop down list and select Plan - this populates the second drop down list with the available events.
In this tutorial the event we wish to use is the AfterPlanModify. This event occurs after any value in the plan is changed.
Click on the second drop down list and choose AfterPlanModify. This adds the event sub routine (two lines of code) to the code window. This code calls our macro when the plan is modified:
3. Triggering the event driven macro As discussed event driven macros do not need to be run explicitly by you as they are driven by an event. In our tutorial the event that triggers our macro is AfterPlanModify. We will change the value into the Task Color column to trigger this event and see the effect.
Add the user Task Color column if not displayed (use Format | Columns ). In the user Task Color column click on a cell and choose a color from the dropdown, BLACK, BLUE, RED, MAGENTA, etc. as per code above. The task bar color should change. Clear text to reset to the default color.
3.
Printing a chapter of the help After printing a chapter of the help, Windows can sometimes freeze the help file. If this occurs rightclick on the Pertmaster help application icon in Windows Start menu Taskbar (usually located at the bottom of the screen) and choose Restore . 1. 2. 3. Select the required chapter. Click on the Print button Choose Print the selected heading and all the subtopics .
The example below has the Risk Tutorial - Part 1 selected. Clicking on the Print button and selecting Print the selected heading and all the subtopics will print out the whole of the 'Risk Tutorial - Part 1'.