Skip to content

Instantly share code, notes, and snippets.

@DavidPratten
Last active December 29, 2015 19:09
Show Gist options
  • Select an option

  • Save DavidPratten/7715672 to your computer and use it in GitHub Desktop.

Select an option

Save DavidPratten/7715672 to your computer and use it in GitHub Desktop.
Add an Microsoft Project automatically allocated "Permanent ID" Number field that is unique (never reused) and survives task cut and paste operations.
'
' The MIT License (MIT)
' Copyright (c) 2013 David Robert Pratten http://davidpratten.com
'
' Permission is hereby granted, free of charge, to any person obtaining a copy
' of this software and associated documentation files (the "Software"), to deal
' in the Software without restriction, including without limitation the rights
' to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
' copies of the Software, and to permit persons to whom the Software is
' furnished to do so, subject to the following conditions:
'
' The above copyright notice and this permission notice shall be included in
' all copies or substantial portions of the Software.
'
' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
' IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
' FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
' AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
' LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
' OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
' THE SOFTWARE.
'
' ===========================================================
' Add an MS Project Permanent ID Number field
' that is unique and survives task cut and paste operations.
' ===========================================================
'
' Security
' --------
' After installing this Permanent ID macro, depending on your security settings, MS Project
' may ask you to enable macros.
'
' Compatibility
' -------------
' Uses standard VBA functions available in recent versions of MS Project. Tested on versions 2007 and 2010.
'
' Installation
' ------------
' The instruction steps are illustrated here:
' http://davidpratten.com/2013/11/30/configuration-management-and-ms-project/
'
' In short
' 1) Add this code to a new "Class Module" called "clsPermanentID" in the project file.
' 2) Add the following lines of code in the "ThisProject" object under "Microsoft Project Objects"
' and uncomment the code from the bottom up. If you uncomment from the top down,
' VBA will insert unwanted code.
' --------- Code Start --------------
' Dim X As New clsPermanentID
' Sub Initialize_App()
' Set X.App = MSProject.Application
' Set X.Proj = Application.ActiveProject
' End Sub
' Private Sub Project_Open(ByVal pj As Project)
' Call Initialize_App
' End Sub
' --------- Code Finish --------------
' 3) Rename one of the custom number fields "Permanent ID", any one will do
' 4) Close and Reopen the project file.
' 5) Now all items in this project file will have a "Permanent ID" assigned that
' will survive cut and paste operations.
'
' Retrofitting to existing project
' --------------------------------
' As above, except that for existing tasks, you will need to manually copy current "Unique ID"s into
' the "Permanent ID" column.
Public WithEvents App As Application
Public WithEvents Proj As Project
Sub App_ProjectBeforeTaskChange(ByVal tsk As Task, ByVal Field As PjField, _
ByVal NewVal As Variant, Cancel As Boolean)
Dim PermanentID As Long
PermanentID = FieldIDofPermanentID()
If PermanentID <> 0 Then
If tsk.GetField(PermanentID) = 0 Then
tsk.SetField PermanentID, tsk.UniqueID
End If
End If
End Sub
Private Function FieldIDofPermanentID()
Dim i As Long
Dim PermanentID As Long
PermanentID = 0
Const PermanentIDFieldName As String = "Permanent ID"
For i = pjTaskNumber1 To pjTaskNumber5
If CustomFieldGetName(i) = PermanentIDFieldName Then
PermanentID = i
Exit For
End If
Next
If PermanentID = 0 Then
For i = pjTaskNumber6 To pjTaskNumber20
If CustomFieldGetName(i) = PermanentIDFieldName Then
PermanentID = i
Exit For
End If
Next
End If
FieldIDofPermanentID = PermanentID
End Function
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment