Welcome to MSDN Blogs Sign in | Join | Help

Larry's Project Dev Blog

The musings of developing for Microsoft Project.
Enterprise Custom Fields from VBA

I got an interesting question the other day.  The poster wanted to access enterprise custom fields from VBA, not the PSI.  After my recovery from someone not wanting to use the PSI  and that I needed to write some VB (just kidding) I started to look for a solution.

First thing to think about is that enterprise custom fields are now GUID based, no more pjTaskEnterpriseProjectText21.  When I wanted to get custom field information before I would use the GetField method, and pass in the appropriate constant.  I looked at GetField, it’s still there, but the signature hasn’t changed???  I was perplexed.  I went through the new calls available in the API and nothing really rang a bell for displaying custom fields.  I asked a few people who said they thought you couldn’t access enterprise custom fields from VBA, you’d be required to go to PSI.  Well that was half true.

In an earlier post I mentioned that the custom fields had been moved to the server.  No more opening the enterprise global and adding new enterprise fields.  That was the half true portion, no more changing custom field definitions from the client.  The half false portion is that you can access the custom field values from the client (and therefore VBA).  I actually needed to ask the product PM for confirmation, and he gave me the clue I needed (thanks Luke).  The key is a new method on the application object called FieldNameToFieldConstant, the signature is as follows:

expression.FieldNameToFieldConstant(FieldName, FieldType)

Return Value
PjField

expression   A variable that represents an Application object.

Parameters

Name

Required/Optional

Data Type

Description

FieldName

Required

String

The name of the field whose constant is required.

FieldType

Optional

Long.

The type of field. Can be one of the following PjFieldType constants: pjResource or pjTask. The default value is pjTask.

The call returns a pjfield value that can be used to specify the enterprise field you are looking for.  Say I wanted to get a project enterprise custom field called “Project Lifecycle”  The code in VBA would look like the following:

    LifeCycleName = ActiveProject.ProjectSummaryTask.SetField(FieldNameToFieldConstant("Project Lifecycle"))

Since ProjectSummaryTask is technically a task, you don’t need to specify a field type, pjTask is the default.  How about if we wanted to get a resource enterprise custom field called “Team Name”.  The code in VBA would look like the following:

TeamName = ActiveProject.Resources(3).SetField(FieldNameToFieldConstant("Team Name", pjResource))

Notice now we have to specify the FieldType of pjResource.  We have gotten the values of custom fields, how do we change them?  Same as before using the SetFields method, but apply the FieldNameToFieldConstant trick.  Changing the “Project Lifecycle” would give us:

    Call ActiveProject.ProjectSummaryTask.SetField(FieldNameToFieldConstant("Project Lifecycle"), "Project Lifecycle.Product Development")

Remember if you have lookup tables associated with the custom fields to pay close attention to the spelling and dot notation, or you’ll get a failure that can be hard to track down.  Also pjProject has been added to the pjFieldTypes enumeration.  I’m not quite sure where it’s used, but it doesn’t seem applicable here.

 

Posted: Wednesday, August 23, 2006 3:06 PM by lduff

Comments

Simant said:

Hi Larry,

I am trying to set and get the custom field as you have mentioned in VBA. But I am getting error message "Unexpected error occured!!!"

Following is the line where i am getting that error.

Call g_ActiveMSProject.ProjectSummaryTask.SetField(FieldNameToFieldConstant("PlanCustomFieldID", pjTask), CurrentState.PlanObjectMap.ToString())

Could you please help?

Regards,Simant.

# October 9, 2006 2:07 AM

Craig Orkins said:

Hi Larry,

Your first two examples out of the three use SetField when they should be using GetField I believe.

BTW, I enjoy your sessions at the Project Technical briefings.  Keep up the good work.  I searched high and low for this answer before finding this blog entry! Spot on for what we needed.

-Craig

# April 3, 2007 9:20 AM

Peter Balzer said:

Hi,

you explained how to access task custom fields and resource custom fields, but what about project custom fields? The Project object doesn't have a GetField method.

Peter

# October 31, 2008 10:32 AM
Leave a Comment

(required) 

(required) 

(optional)

(required) 

  
Enter Code Here: Required

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Page view tracker