Top Banner
Using Macros in Project Jeff Fenbert MPUG Puget Sound May 26, 2011 Contact info: [email protected]
34

Using Macros in Project Jeff Fenbert MPUG Puget Sound May 26, 2011 Contact info: [email protected]@aol.com.

Dec 18, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Using Macros in Project Jeff Fenbert MPUG Puget Sound May 26, 2011 Contact info: jafenbert@aol.comjafenbert@aol.com.

Using Macros in Project

Jeff FenbertMPUG Puget SoundMay 26, 2011

Contact info: [email protected]

Page 2: Using Macros in Project Jeff Fenbert MPUG Puget Sound May 26, 2011 Contact info: jafenbert@aol.comjafenbert@aol.com.

Agenda

Why use Macros

Recording Macros

Custom Macros

Page 3: Using Macros in Project Jeff Fenbert MPUG Puget Sound May 26, 2011 Contact info: jafenbert@aol.comjafenbert@aol.com.

Macros in Project

Help automate repeated actions

Utilizes Visual Basic for Applications (VBA) – used in other Office Products

Easy to record

Fun to write

Page 4: Using Macros in Project Jeff Fenbert MPUG Puget Sound May 26, 2011 Contact info: jafenbert@aol.comjafenbert@aol.com.

Jeff’s Intro to Macros*

Send file – Sensitive Data

Wipe out task and Resource names

* I didn’t write it, just used it

Page 5: Using Macros in Project Jeff Fenbert MPUG Puget Sound May 26, 2011 Contact info: jafenbert@aol.comjafenbert@aol.com.

Scrub MacroThis To This

Copy available here: http://masamiki.com/project/macros.htm

Page 6: Using Macros in Project Jeff Fenbert MPUG Puget Sound May 26, 2011 Contact info: jafenbert@aol.comjafenbert@aol.com.

Getting Started

Set Security in Project

Page 7: Using Macros in Project Jeff Fenbert MPUG Puget Sound May 26, 2011 Contact info: jafenbert@aol.comjafenbert@aol.com.

Select Level of Security

Default High Most macros won’t

work Disable existing

Option Medium

Page 8: Using Macros in Project Jeff Fenbert MPUG Puget Sound May 26, 2011 Contact info: jafenbert@aol.comjafenbert@aol.com.

Quick Demo

Set Security

Run Scrub Macro

Page 9: Using Macros in Project Jeff Fenbert MPUG Puget Sound May 26, 2011 Contact info: jafenbert@aol.comjafenbert@aol.com.

Quick Look at the Code

Sub scrub()'This macro clears the task name, resource name and text fields.'It also resets the project name and title'Copyright Jack Dahlgren Feb. 2002

Dim t As TaskDim ts As TasksDim r As ResourceDim rs As ResourcesDim myok As Integer

myok = MsgBox("This will permanently remove tasknames, resource names and notes from your project. Are you sure you want to continue?", 257, "ERASE DATA?")

If myok = 1 ThenSet ts = ActiveProject.TasksSet rs = ActiveProject.ResourcesFor Each r In ActiveProject.Resources r.Name = r.UniqueID r.Group = "" r.Initials = r.UniqueIDNext r

For Each t In ts If Not t Is Nothing Then t.Name = t.UniqueID t.Notes = "" End IfNext t

End IfEnd Sub

Page 10: Using Macros in Project Jeff Fenbert MPUG Puget Sound May 26, 2011 Contact info: jafenbert@aol.comjafenbert@aol.com.

Need a Macro

Situation Filter for Framers Group by Floor Sort Descending Order of Hours

Page 11: Using Macros in Project Jeff Fenbert MPUG Puget Sound May 26, 2011 Contact info: jafenbert@aol.comjafenbert@aol.com.

Record A Macro

Page 12: Using Macros in Project Jeff Fenbert MPUG Puget Sound May 26, 2011 Contact info: jafenbert@aol.comjafenbert@aol.com.

Add Button to Toolbar

Select and Drag Item to Toolbar

Page 13: Using Macros in Project Jeff Fenbert MPUG Puget Sound May 26, 2011 Contact info: jafenbert@aol.comjafenbert@aol.com.

Let’s Take A Look

Sub Sample_Filter_and_Sort()

' Sample for MPUG Presentation

FilterApply

Name:="_flt_Framing_Contractor"

Sort Key1:="Duration", Ascending1:=False

GroupApply Name:="_grp_Floor"

SelectTaskField Row:=1, Column:="Name"

GotoTaskDates

End Sub

Make a similar one for a standard view

Page 14: Using Macros in Project Jeff Fenbert MPUG Puget Sound May 26, 2011 Contact info: jafenbert@aol.comjafenbert@aol.com.

A few hints

Modules vs Macros Multiple Macros in a Module

Naming No Spaces Allowed Use Underscore

Comments Lines starting with a ‘ are comments Use these to remind yourself (and

others) what the code is doing

Page 15: Using Macros in Project Jeff Fenbert MPUG Puget Sound May 26, 2011 Contact info: jafenbert@aol.comjafenbert@aol.com.

Create A Network Check

No Predecessor

No Successor

Constraints

FF or SS Links

Linked Summary Tasks

Page 16: Using Macros in Project Jeff Fenbert MPUG Puget Sound May 26, 2011 Contact info: jafenbert@aol.comjafenbert@aol.com.

The Results From Construction Template

Pretty Dull

A more typical Result

Page 17: Using Macros in Project Jeff Fenbert MPUG Puget Sound May 26, 2011 Contact info: jafenbert@aol.comjafenbert@aol.com.

Another Version – A bit more Fun

Link to Demo

Page 18: Using Macros in Project Jeff Fenbert MPUG Puget Sound May 26, 2011 Contact info: jafenbert@aol.comjafenbert@aol.com.

Elements of the Code

A form

Call to procedure

Variables

Command Buttons

Page 19: Using Macros in Project Jeff Fenbert MPUG Puget Sound May 26, 2011 Contact info: jafenbert@aol.comjafenbert@aol.com.

Relationship Between Elements

Macro to Open Form

Form calls to Macro

(Procedure)

Macro GeneratesData

Form DisplaysData

Page 20: Using Macros in Project Jeff Fenbert MPUG Puget Sound May 26, 2011 Contact info: jafenbert@aol.comjafenbert@aol.com.

Elements of a Form

Page 21: Using Macros in Project Jeff Fenbert MPUG Puget Sound May 26, 2011 Contact info: jafenbert@aol.comjafenbert@aol.com.

Call to Procedure

Open The FormSub analysis_frm()FilterApply "All Tasks"OutlineShowAllTasksfrm_project_analysis.ShowEnd Sub

Call the ProcedurePrivate Sub UserForm_Initialize()Call project_data

Page 22: Using Macros in Project Jeff Fenbert MPUG Puget Sound May 26, 2011 Contact info: jafenbert@aol.comjafenbert@aol.com.

Generate the Data

Sub project_data()Dim tsk As TaskDim tsk_sum As Task

pred = 0succ = 0fnlt = 0snlt = 0sumtasks = 0longtasks = 0lags = 0sumlink = 0

For Each tsk In ActiveProject.Tasks If Not (tsk Is Nothing) Then If tsk.Summary = False Then If tsk.Predecessors = "" Then pred = pred + 1 End If If tsk.Successors = "" Then succ = succ + 1 End If

Page 23: Using Macros in Project Jeff Fenbert MPUG Puget Sound May 26, 2011 Contact info: jafenbert@aol.comjafenbert@aol.com.

Form and Elements

Private Sub UserForm_Initialize()Call project_dataTextBox1.Value = predTextBox2.Value = succTextBox3.Value = fnltTextBox4.Value = snltTextBox5.Value =

ActiveProject.Tasks.Count - sumtasksTextBox6.Value =

ActiveProject.ProjectSummaryTask.Duration / 480 & " days"

TextBox9.Value = longtasksTextBox10.Value = sumlinkTextBox11.Value = lagsLabel12 = ActiveProject.Name

End Sub

Page 24: Using Macros in Project Jeff Fenbert MPUG Puget Sound May 26, 2011 Contact info: jafenbert@aol.comjafenbert@aol.com.

Project Status Analysis

Issues Actual Start greater than Status Date

I started that tomorrow Scheduled Start Less that Status Date

I will start that yesterday Actual Finish greater than Status Date Schedule Finish Less than Status Date Over Two Weeks behind

Page 25: Using Macros in Project Jeff Fenbert MPUG Puget Sound May 26, 2011 Contact info: jafenbert@aol.comjafenbert@aol.com.

A few more hints

Variable Names Use Caps for variable Names Type all lower case -- VB will add caps if correct (error check) See Example

Sub variabletest()Dim NewVariable As String

newwariableNewVariable

End Sub

Compile Errors

Debugging F8 – Step Into Stop – Run to that point in the code

Page 26: Using Macros in Project Jeff Fenbert MPUG Puget Sound May 26, 2011 Contact info: jafenbert@aol.comjafenbert@aol.com.

Part of the Code

If t.Summary = False Then

If t.start < ActiveProject.StatusDate And t.PercentComplete = 0 Then

t.Text13 = "Start less than Status"

ElseIf t.finish < ActiveProject.StatusDate And t.PercentComplete <> 100 Then

t.Text13 = "Finish less than Status"

ElseIf t.ActualFinish > ActiveProject.StatusDate And t.ActualFinish <> notapplicable Then

t.Text13 = "Actual > Status"

ElseIf t.ActualStart > ActiveProject.StatusDate And t.ActualStart <> notapplicable Then

t.Text13 = "Actual > Status"

ElseIf (t.RemainingDuration - 9 * 480) > Application.DateDifference(ActiveProject.StatusDate, t.finish)

Then

t.Text13 = "over 2 weeks behind"

Else

t.Text13 = "OK"

End If

End If

Page 27: Using Macros in Project Jeff Fenbert MPUG Puget Sound May 26, 2011 Contact info: jafenbert@aol.comjafenbert@aol.com.

Process to Share Views, Filters, Etc

Easy Way to Share Items

Auto Load Macro

Copy to Organizer

Page 28: Using Macros in Project Jeff Fenbert MPUG Puget Sound May 26, 2011 Contact info: jafenbert@aol.comjafenbert@aol.com.

Copy Code

Private Sub Project_Open(ByVal pj As Project)

If MsgBox("Do you wish to install the Special MPUG View?", vbYesNo) <> vbYes Then Exit Sub

Alerts False

OrganizerMoveItem Type:=2, FileName:="Construction_Template_copy_view_on_open.mpp", ToFileName:="GLOBAL.MPT", Name:="flag1to3"

OrganizerMoveItem Type:=2, FileName:="Construction_Template_copy_view_on_open.mpp", ToFileName:="GLOBAL.MPT", Name:="flt_Inspector"

OrganizerMoveItem Type:=2, FileName:="Construction_Template_copy_view_on_open.mpp", ToFileName:="GLOBAL.MPT", Name:="flt_general"

OrganizerMoveItem Type:=2, FileName:="Construction_Template_copy_view_on_open.mpp", ToFileName:="GLOBAL.MPT", Name:="flt_Frame_Contractor"

OrganizerMoveItem Type:=0, FileName:="Construction_Template_copy_view_on_open.mpp", ToFileName:="GLOBAL.MPT", Name:="special_gantt"

OrganizerMoveItem Type:=9, FileName:="Construction_Template_copy_view_on_open.mpp", ToFileName:="Global.MPT", Name:="Start1"

FileClose pjDoNotSave, True

Alerts True

End Sub

Page 29: Using Macros in Project Jeff Fenbert MPUG Puget Sound May 26, 2011 Contact info: jafenbert@aol.comjafenbert@aol.com.

Working with Other Applications

Data From Excel

Open Files

Send out Status Sheets

Page 30: Using Macros in Project Jeff Fenbert MPUG Puget Sound May 26, 2011 Contact info: jafenbert@aol.comjafenbert@aol.com.

Open Files in File ListSub File_list()

Dim xcel As ObjectSet xcel = CreateObject("Excel.Application")

xcel.Workbooks.Open FileName:= _ "C:\users\jeff\Desktop\MPA Nov 13\MPUG\MPUG_Demo\Mpug_Demo_File_List.xls", _ ReadOnly:=True 'xcel.Visible = TrueFor i = 2 To 10 If xcel.cells(i, 1) = "" Then Exit For FileName = "C:\users\jeff\Desktop\MPA Nov 13\MPUG\MPUG_Demo\" & xcel.cells(i,

1).Value _ & ".mpp" FileOpen FileName, ReadOnly:=TrueNext i

xcel.Quit ' When you finish, use the Quit method to closeSet xcel = Nothing ' the application, then release the reference.

End Sub

Open MPUG Demo File

Page 31: Using Macros in Project Jeff Fenbert MPUG Puget Sound May 26, 2011 Contact info: jafenbert@aol.comjafenbert@aol.com.

Status and Update

Build Status Report for each group

Page 32: Using Macros in Project Jeff Fenbert MPUG Puget Sound May 26, 2011 Contact info: jafenbert@aol.comjafenbert@aol.com.

Last Code (Partial)

Sub Save_Reports()' For Each r In ActiveProject.Resources filtername = "Tasks_for_" + r.Name mapname = filtername + "_" FilterEdit Name:=filtername, TaskFilter:=True, Create:=True, OverwriteExisting:=True, FieldName:="Start", test:="is less than", Value:=filterdate,

ShowInMenu:=False, ShowSummaryTasks:=False FilterEdit Name:=filtername, TaskFilter:=True, FieldName:="", NewFieldName:="Resource Names", test:="contains", Value:=r.Name,

Operation:="And", ShowSummaryTasks:=False FilterEdit Name:=filtername, TaskFilter:=True, FieldName:="", NewFieldName:="% Complete", test:="Does not equal", Value:="100",

Operation:="And", ShowSummaryTasks:=False MapEdit Name:=mapname, Create:=True, OverwriteExisting:=True, _ DataCategory:=pjMapTasks, CategoryEnabled:=True, TableName:="Task_Table", _ FieldName:="Text15", ExternalFieldName:="Project", ExportFilter:=filtername 'MapEdit Name:=mapname, DataCategory:=pjMapTasks, _ 'FieldName:="ID" MapEdit Name:=mapname, DataCategory:=pjMapTasks, _ FieldName:="Unique ID", ExternalFieldName:="Unique ID" MapEdit Name:=mapname, DataCategory:=pjMapTasks, _ FieldName:="Name", ExternalFieldName:="Task Name" MapEdit Name:=mapname, DataCategory:=pjMapTasks, _ FieldName:="Start", ExternalFieldName:="Start_Date" MapEdit Name:=mapname, DataCategory:=pjMapTasks, _ FieldName:="Finish", ExternalFieldName:="Finish_Date" MapEdit Name:=mapname, DataCategory:=pjMapTasks, _ FieldName:="Work", ExternalFieldName:="Hours" MapEdit Name:=mapname, DataCategory:=pjMapTasks, _ FieldName:="Resource Names", ExternalFieldName:="Resources"

savename = mapname + statdate FileSaveAs Name:=Location + savename + ".xls", FormatID:="MSProject.XLS5", Map:=mapname Next r

End Sub

Page 33: Using Macros in Project Jeff Fenbert MPUG Puget Sound May 26, 2011 Contact info: jafenbert@aol.comjafenbert@aol.com.

Resources

Websites with Macros http://masamiki.com/project/macros.htm http://www.brighthub.com/office/project-manag

ement/articles/52620.aspx http://visualbasic.about.com/od/learnvba/Learn

_to_program_using_Visual_Basic_for_Applications_VBA.htm

http://zo-d.com/blog/archives/programming.html

Page 34: Using Macros in Project Jeff Fenbert MPUG Puget Sound May 26, 2011 Contact info: jafenbert@aol.comjafenbert@aol.com.

Questions