none
How to Create Classes in Project 2003 RRS feed

  • Question

  • I have a situation where I need to create a collection, I am trapping certain Projects that have been opened (Macro in Global.mpt) so that on Close (i.e. BeforeClose) I can manage Projects that are members of my 'new collection' prior to complete closure.  I intend to add a small piece of code to ThisProject in order to do this - the closing project can check to see if it is part of this collection and then call a final routine if it is.  I am purposely trying to keep as much code out of the Projects themselves as this is running across a corporate network and executing code seems to cause the loss of the 'ThisProject' module and often anything else. 

    The documentation I have found is not particularly helpful or well annotated and having copied some VB6 examples of simple Get/Set could not resolve the multiple errors.  So if anyone can either point me to some good examples or show me how to create a simple collection that is updated from the ActiveProject.Name property it would be appreciated.

    Thanks Nigel

    Monday, December 6, 2010 11:00 AM

Answers

  • You actually have two problems! the first is the "loss" of the ThisProject object, teh second use of Classes.

    Icreated a class in the Global.mpt file in Project 2007 (but this should be the same as for 2003) and could not access it from project files. I had to copy the class to the local project to use it. So, try:

    In a Class File caled clsSnapName have the code:

    Private SnapNameValue As String

    Public Property Get snapName() As String
        snapName = SnapNameValue
    End Property

    Public Property Let snapName(ByVal Value As String)
        SnapNameValue = Value
    End Property

     

    In a module have:

    Sub test()
    Dim clsSnap As New clsSnapName
        clsSnap.snapName = "A name"
        MsgBox clsSnap.snapName
        Set clsSnap = Nothing
    End Sub

    You can have as many variables as you like made from the class, including arrays. Each copy has its own piece of memory.

    Note that in the Let property there was a bug, you need:

    Public Property Let snapName(ByVal Value As String)
        SnapNameValue = Value          'Not SnapName = Value
    End Property

     


    Rod Gill

    The one and only Project VBA Book Rod Gill Project Management
    Tuesday, December 21, 2010 8:13 PM
    Moderator
  • Can't help with these except to say make sure you have the latest service pack for your project version and even all Cumulative Updates as well.

    I think you may need to re-design your solution. What is it you are trying to achieve? Application.Projects already provides a collection of open projects. To mark whether a project is of particular interest to your macro, try setting a flag such as Flag1 against teh project summary task.


    Rod Gill

    The one and only Project VBA Book Rod Gill Project Management
    Saturday, January 22, 2011 11:12 PM
    Moderator

All replies

  • To be specific I can create the simple class SnapClass

    Dim SnapNameValue As String


    Property Get snapName() As String

        snapName = SnapNameValue

    End Property

    Property Let snapName(ByVal Value As String)

     
        snapName = Value
      

    End Property

    But I want to be able to either assign that class to a project e.g.

    ActiveProject.SnapClass = "Fred"

    OR

    See and create SnapClass as an array and cannot seem to get my head around how to do either.


    Nigel Parsons
    Monday, December 6, 2010 2:48 PM
  • You actually have two problems! the first is the "loss" of the ThisProject object, teh second use of Classes.

    Icreated a class in the Global.mpt file in Project 2007 (but this should be the same as for 2003) and could not access it from project files. I had to copy the class to the local project to use it. So, try:

    In a Class File caled clsSnapName have the code:

    Private SnapNameValue As String

    Public Property Get snapName() As String
        snapName = SnapNameValue
    End Property

    Public Property Let snapName(ByVal Value As String)
        SnapNameValue = Value
    End Property

     

    In a module have:

    Sub test()
    Dim clsSnap As New clsSnapName
        clsSnap.snapName = "A name"
        MsgBox clsSnap.snapName
        Set clsSnap = Nothing
    End Sub

    You can have as many variables as you like made from the class, including arrays. Each copy has its own piece of memory.

    Note that in the Let property there was a bug, you need:

    Public Property Let snapName(ByVal Value As String)
        SnapNameValue = Value          'Not SnapName = Value
    End Property

     


    Rod Gill

    The one and only Project VBA Book Rod Gill Project Management
    Tuesday, December 21, 2010 8:13 PM
    Moderator
  • Thanks Rod, I will try this as soon as I get the opportunity - but it makes sense (I thought I had missed something silly in the code).  As for the 'other' problem I have been researching that for some years to no avail in standalone Project (there appears to be a fix or workaround for Enterprise).  The biggest problem is that I cannot seem to define the conditions under which the error occurs, I have begun to suspect that it may be down to momentary loss of connectivity between a project open on a server and the client that opened it.  I am certainly not the only person who has come across this but there does not appear to be any way of fixing the problem other than rebuilding the corrupted project (on the plus side I am becoming quite good at that).

     

    If you do have any light to shed on:-

    1. Why the ThisProjectClass might get stripped from a Project, and
    2. if there is any way of rebuilding/replacing that class
    I would be very grateful.

    Nigel


    Nigel Parsons
    Sunday, January 9, 2011 3:07 PM
  • Can't help with these except to say make sure you have the latest service pack for your project version and even all Cumulative Updates as well.

    I think you may need to re-design your solution. What is it you are trying to achieve? Application.Projects already provides a collection of open projects. To mark whether a project is of particular interest to your macro, try setting a flag such as Flag1 against teh project summary task.


    Rod Gill

    The one and only Project VBA Book Rod Gill Project Management
    Saturday, January 22, 2011 11:12 PM
    Moderator