pert master macro tutorials

Upload: khaled-abdelbaki

Post on 14-Apr-2018

223 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/30/2019 Pert Master Macro Tutorials

    1/11

    Macro Tutorials

    This document is an extract from the Pertmaster help file version h8.5.1.

    Pertmaster - Macro Tutorials

    Pertmaster - Macro Tutorials |

  • 7/30/2019 Pert Master Macro Tutorials

    2/11

    These tutorials take you through the steps of creating macros in Pertmaster.

    Tutorial contents:

    1. Report writing macro

    2. Global report writing macro

    3. Event driven macro: Conditional formatting

    4. Creating a form based macro

    Files used

    Pertmaster Files (installed with Pertmaster)

    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.

    In this tutorial we will design a macro that creates a simple report in MS Excel. The macro will be a Planmacro.

    About Plan Macros

    Plan macros are saved as part of the plan they were created in. If a plan is copied or moved then any Planmacros will still be available. A Plan macro can only be run from the plan it was created in.

    1. Open sample plan

    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 windowwill be displayed on the right.

    Table of Contents

    1. Macro Tutorials 1

    2. Macro Tutorial - Report writing macro 1-5

    3. Macro Tutorial - Global report writing macro 5-6

    4. Macro Tutorial - Event driven macro: Conditional formatting 6-9

    5. PDF Documentation and Printing Help 9-10

    1 Macro Tutorials

    The ability to create and run macros will depend on the version of Pertmaster you are using seePertmaster 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 PDFbooklet installed in the Pertmaster Documentation folder - see PDF Documentation and Printing

    Help (Section 5)

    2 Macro Tutorial - Report writing macro

    The macro created in this tutorial requires MS Excel to run.

    Use Globalmacros to create a macro which is available to any open plan - see the tutorial Report

    writing macro (Section 3).

    Pertmaster - Macro Tutorials | 1

  • 7/30/2019 Pert Master Macro Tutorials

    3/11

    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 themacro.

    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.

    3. Entering the macro code

    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

    Pertmaster - Macro Tutorials | 2

  • 7/30/2019 Pert Master Macro Tutorials

    4/11

    Dim m_objExcel As ObjectDim m_objWorkbook As ObjectDim objTask As Task

    Dim objTasks As Tasks

    Dim nRow As IntegerDim objSheet As Excel.Worksheet

    Set m_objExcel = New Excel.Application ' start up Excel

    m_objExcel.Visible = False ' do not display Excel yetSet m_objWorkbook = m_objExcel.Workbooks.Add 'create new workbookSet objSheet = m_objWorkbook.Sheets(1)

    ' TitlenRow = 1

    objSheet.Cells(nRow, "A") = "Pertmaster Report - " & ActivePlan.Information.TitlenRow = nRow + 1

    objSheet.Cells(nRow, "A") = "ID"objSheet.Cells(nRow, "B") = "Description "

    objSheet.Cells(nRow, "C") = "Comment"nRow = nRow + 1

    ' Populate spreadsheet with task informationSet objTasks = ActivePlan.Tasks

    For Each objTask In objTasksobjSheet.Cells(nRow, "A") = objTask.IDobjSheet.Cells(nRow, "B") = objTask.DescriptionobjSheet.Cells(nRow, "C") = objTask.Comment

    nRow = nRow + 1

    Next

    ' Adjust column widthsobjSheet.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 withinMicrosoft Visual Basic or the Pertmaster application.

    In MS Visual Basic - File | Save (filename)

    5. Running the macro

    Close the Microsoft Visual Basic application - File | Close and return to Pertmaster Risk Expert.

    In Pertmaster - Tools | Macros | Macros.

    Macros run faster if the MS Visual Basic application is closed.

    Pertmaster - Macro Tutorials | 3

  • 7/30/2019 Pert Master Macro Tutorials

    5/11

    Select 'PertmasterPlan (Tutorial_Macros_Report(v1.1).plan'from the dropdown list.

    Click on 'CreateReport'.

    Click Run.

    "This will create an Excel report. Continue?". Click OK.

    MS Excel will be opened and populated with data:

    If you receive the message "Compile error: User-defined type not defined" it is likely that thereference created in step 2 above has not been completed. Reset the macro (Run | Reset) and

    check the reference has been added correctly.

    Pertmaster - Macro Tutorials | 4

  • 7/30/2019 Pert Master Macro Tutorials

    6/11

    Good work! Move onto the next tutorial to learn about Globalmacros.

    About Global Macros

    A Globalmacro can be run from any plan.

    In the last tutorial we created a Plan macro that generated a Report in Excel. As we may want to run thisreport writing macro on other plans we will the same macro as a Globalmacro.

    1. Create a new global macro

    Start up Pertmaster.

    Close down any open plans using File | Close.

    Tools | Macros | Macro Manager | Create New Macro File.

    Enter ExcelReportin the Macro Project Name. Keep the default Macros Folder.

    Click OKand the new macro will be added.

    Click OKin the Macro Managerdialog 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 whicheverversion of MS Excel is available).

    3. Add the code

    Copy and paste the visual basic code used in the Plan macro tutorial into the open code windowreplacing any existing code: Plan macro tutorial (Section 2)

    3. Save the Global macro

    File | Save ExcelReport.vba

    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.

    3 Macro Tutorial - Global report writing macro

    Pertmaster - Macro Tutorials | 5

  • 7/30/2019 Pert Master Macro Tutorials

    7/11

    Change Macros in: to 'ExcelReport (ExcelReport.vba)'.

    Click on 'CreateReport'.

    Click Run.

    Excellent! You have created a Globalmacro that can be used with any plan. That covers the basics ofGlobal macros. To find out about macros have a look at the other macro tutorials.

    4.1 About event driven macros

    The macros created in the Globaland Plan tutorials only run when the user explicitly requests them to runvia the Macros menu.

    Event driven macros are automatically run by events that occur in the application and plan.

    In this tutorial we will create an event driven macro that colors each task bar depending on the value

    entered against a task. The event that will drive our macro isAfterPlanModify. This event occurs

    whenever a change is made to the plan.

    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 thetask bar color. To save you time the macro has already been entered in the example project so all weneed 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 useAlt + F11)

    Double-click on modFormat to display the code.

    4 Macro Tutorial - Event driven macro: Conditional formatting

    Event driven macros can be Plan or Global. When using a Global event driven macro you have theadditional option of using application events as well as plan events. For example there is an application

    event calledApplication_AfterStartthat is fired when the Pertmaster application starts.

    Pertmaster - Macro Tutorials | 6

  • 7/30/2019 Pert Master Macro Tutorials

    8/11

    In Modules you should see a sub routine called conditionalFormat() and a function called GetColor. The

    GetColorfunction is called by the conditionalFormatsub 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:

    ' --------------------------------------------------------------------

    Option Explicit

    Private Const COLORFIELD As String = "Task Color"

    Sub ConditionalFormat()

    Dim objTask As Task

    Dim objTasks As Tasks

    ' 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

    objTask.Style.BarStyle.FillStyle = -1 ' -1 indicates

    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)

    Pertmaster - Macro Tutorials | 7

  • 7/30/2019 Pert Master Macro Tutorials

    9/11

    2. Making the macro event driven

    The conditionalFormatmacro can be run at any time. However we would like this macro to be runwhenever a change is made to Task Color. An event that is triggered when a change is made to TaskColoris theAfterPlanModify.

    In this step we will add the eventAfterPlanModifyand 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 theplan is changed.

    Click on the second drop down list and chooseAfterPlanModify. This adds the event sub routine (twolines of code) to the code window.

    This code calls our macro when the plan is modified:

    Case "DBLUE"

    GetColour = RGB(0, 0, 128)

    Case"DGREEN"

    GetColour = RGB(0, 128, 0)

    Case "DCYAN"

    GetColour = RGB(0, 128, 128)

    Case "DRED"

    GetColour = RGB(128, 0, 0)

    Case "DMAGENTA"GetColour = RGB(128, 0, 128)

    Case "DYELLOW"

    GetColour = RGB(128, 128, 0) Case "GREY"

    GetColour = RGB(192, 192, 192) Case "DGREY"

    GetColour = RGB(128, 128, 128)

    Case "BLUE"GetColour = RGB(0, 0, 255)

    Case "GREEN"GetColour = RGB(0, 255, 0)

    Case "CYAN"

    GetColour = RGB(0, 255, 255)

    Case "RED"

    GetColour = RGB(255, 0, 0) Case "MAGENTA"

    GetColour = RGB(255, 0, 255)

    Case "YELLOW"

    GetColour = RGB(255, 255, 0)

    Case "WHITE"

    GetColour = RGB(255, 255, 255)

    Case Else '

    GetColour = -1

    End Select

    End Function

    This event is triggered by any change to the plan. Though we are only interested in changes to Task

    Colorwe must use this event as there are no events that are only triggered by a change to the usertext field.

    Pertmaster - Macro Tutorials | 8

  • 7/30/2019 Pert Master Macro Tutorials

    10/11

    modFormat.conditionalFormat

    Save and Close the Visual Basic Editor.

    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 theTask Colorcolumn to trigger this event and see the effect.

    Add the user Task Colorcolumn if not displayed (use Format| Columns).

    In the user Task Colorcolumn 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.

    PDF Documentation

    Some of the on-line help (e.g. tutorials) can be found in the Documentation folder that is created whenthe Pertmaster software is installed. The documentation is saved in the Adobe PDF format.

    The default installation folder for the documentation is:

    C:\Program Files\Pertmaster Software\Pertmaster8x\Documentation

    Printing an individual help topic

    1. Select the required topic.

    2. Click on the Printbutton.

    3. Choose Print the selected topic.

    5 PDF Documentation and Printing Help

    After printing a help topic, Windows can sometimes freeze the help file. If this occurs right-clickon the Pertmaster help application icon in Windows Start menu Taskbar (usually located at the

    bottom of the screen) and choose Restore.

    Pertmaster - Macro Tutorials | 9

  • 7/30/2019 Pert Master Macro Tutorials

    11/11

    Printing a chapter of the help

    1. Select the required chapter.

    2. Click on the Printbutton

    3. Choose Print the selected heading and all the subtopics .

    The example below has the Risk Tutorial - Part 1 selected. Clicking on the Printbutton and selectingPrint the selected heading and all the subtopics will print out the whole of the 'Risk Tutorial - Part 1'.

    After printing a chapter of the help, Windows can sometimes freeze the help file. If this occurs right-click on the Pertmaster help application icon in Windows Start menu Taskbar (usually located at the

    bottom of the screen) and choose Restore.

    Pertmaster - Macro Tutorials | 10