Sunday, 15 April 2018

Project VBA - Storing values between re-openings of Microsoft Project

Some times it may be useful to store variable values so that when you next open Microsoft Project you can carry on from where you left off.  There are a few ways that this could be done but one of the easiest and most effective ways is to use document properties.

Reading Custom Document Properties

By default, Microsoft Project will already be storing some values in Document Properties that can be read by accessing the CustomerDocumentProperties collection.  For example, MSP stores "Scheduled Start" and "Scheduled Finish" so that it can be read by other applications e.g. SharePoint.

We can loop through these like any collection as shown below.


Dim docProps As Office.DocumentProperties
Dim docProp As Office.DocumentProperty

Set docProps = ActiveProject.CustomDocumentProperties

For Each docProp In docProps
    Debug.Print docProp.Name + ": " + CStr(docProp.Value)
Next docProp


Storing a Custom Document Property

We can store a new property by using the Add method on CustomDocumentProperty object.  We can define different types for the value that we want to store.  The different types can be used by passing the Type parameter to the Add method where the different types are from MsoDocProperties enumeration.  This enumeration values are listed below and these are common across the different Office applications (you can see from the Dim line above that these are standard Office objects).


Name Value Description
msoPropertyTypeNumber 1 Float
msoPropertyTypeBoolean 2 Boolean
msoPropertyTypeDate 3 Date
msoPropertyTypeString 4 String
msoPropertyTypeFloat 5 Float


docProps.Add Name:="A Key", LinkToContent:=False, _
             Type:=msoPropertyTypeString, Value:="A value"


If you try to add a key Name that already exists then VBA will throw an error.  As such, you will need to search for your key to make sure it doesn't exist before adding again.

Full Example

The below example sets a key MY_KEY to a value that is provided in an InputBox and can be changed.  Try setting it something, saving your MPP file and then re-opening.  You will find your previous value has been saved in the custom document properties for later retrieval.


Const MY_KEY = "MY KEY"

Sub AddDocumentVariable()

    Dim docProps As Office.DocumentProperties
    Dim docProp As Office.DocumentProperty
    Dim numProps As Integer
    Dim found As Boolean
    Dim newVal As String
    found = False
    
    Set docProps = ActiveProject.CustomDocumentProperties

    numProps = docProps.Count
    
        For Each docProp In docProps
           If (docProp.Name = MY_KEY) Then
                Debug.Print MY_KEY + ": " + docProps.Item(MY_KEY)
                newVal = docProps.Item(MY_KEY)
                found = True
             Else
                Debug.Print docProp.Name + ": " + CStr(docProp.Value)
            End If
        Next docProp
    
    newVal = InputBox("Store value for key - " + MY_KEY, _
                    "Store Value in Document Properties", newVal)
    
    If found = False Then
        docProps.Add Name:=MY_KEY, LinkToContent:=False, _
                    Type:=msoPropertyTypeString, Value:=newVal
    Else
        Debug.Print MY_KEY + " exits: No of custom document properties: " _
                    & numProps
        docProps.Item(MY_KEY).Value = newVal
    End If
    

 
End Sub



Viewing Document Properties


You can view the Custom Document Properties by selecting the Advanced Properties option on the File->Info menu.


You can then see the key you have added and manipulate it, as shown below, using the .properties box that opens.



About Me

If you like this post then please comment below.  Please also make a small contribution so that I can post more code snippets and samples here.

Bitcoin:      1MhaZvTqftdebwNihisrAbz22zaSG6tFcN
Ethereum:  0xe69b176A1A5492f19E3595389EA094C732723b69

Project VBA - Storing values between re-openings of Microsoft Project

Some times it may be useful to store variable values so that when you next open Microsoft Project you can carry on from where you left off. ...