PDA

View Full Version : Hierarchical class module wrapper code



tbaker818
09-18-2015, 02:26 PM
I haven't been able to write the kind of code I'd like to using VBA. It's really all I program in, so I don't know for sure what it's actually missing, but the closes thing I've found is inheritance in .Net.

I'd like to have a class module for selected objects in the application's (in this case MS Project) object hierarchy that contains properties and methods for the types of things my company would like to do with Project. One example is updating actual hours worked for each resource within each task within each project. What I'm having trouble with is making an instance of each class module represent an actual instance of the same object in Project AND having my own class modules relate to each other in a hierarchy the same way Project's own objects do.

I've been able to make the instance of each class module represent it's counterpart in Project through the standard module code like this:

Dim myProject As New cProject
For Each Task in myProject.Project

The Project property in class cProject being:

Property Get Project() As MSProject.Project Set Project = mpjProject
End Property

where mpjProject is just the private internal class variable, also of type MSProject.Project. I have an Open method in cProject, which sets mpjProject to an actual open project so that myProject.Project (can't seem to set the default property with Attribute DefaultMember.VB_UserMemId = 0 when that property is an object) represents an actual project. The problem comes along when I try to say:


For Each myTask In myProject.Project.Tasks

where myTask is an instance of cTask, which like cProject, has a Task property of type MSProject.Task. I want myTask to represent the first Task in the actual Project, but to extend that task object with my own cTask module. So like I was able to do with Project above, my custom methods in cTask could be called from myTask, or the MS Project methods could be called from myTask.Task.

To summarize, I want 1) a hierarchy of objects that 2) extend the application's built in objects. Is there a way to do this, or is the language limited in this way?

SamT
09-18-2015, 06:19 PM
One example is updating actual hours worked for each resource within each task within each project

With Task.Resource
A = .ActualWork
With .Assignment
.ActualWork = .ActualWork + 120 'Minutes
End With
B = .ActualWork
End With
B = A + 120 = True




Do you realize that inheritance to work, you must duplicate the entire MS Project object model even though most of the object classes will merely have one Object variable, set by reference?

Have you considered creating an abbreviated custom Object Model to run parallel with the Application and have your main code refer to either as needed.


For each Tsk in Tasks
For Each Res in Tsk.Resources
MyVariable = Custom.Tasks(Tsk,Name).Resources(Res.Name).CustomProperty
HoursWorked = Res.ActualHours 'Application property
Next
Next

Shawnpurpix
09-05-2016, 05:19 AM
I haven't been able to write the kind of code I'd like to using VBA. It's really all I program in, so I don't know for sure what it's actually missing, but the closes thing I've found is inheritance in .Net.

I'd like to have a class module for selected objects in the application's (in this case MS Project) object hierarchy that contains properties and methods for the types of things my company would like to do with Project. One example is updating actual hours worked for each resource within each task within each project. What I'm having trouble with is making an instance of each class module represent an actual instance of the same object in Project AND having my own class modules relate to each other in a hierarchy the same way Project's own objects do.

I've been able to make the instance of each class module represent it's counterpart in Project through the standard module code like this:

Dim myProject As New cProject
For Each Task in myProject.Project

The Project property in class cProject being:

Property Get Project() As MSProject.Project Set Project = mpjProject
End Property

where mpjProject is just the private internal class variable, also of type MSProject.Project. I have an Open method in cProject, which sets mpjProject to an actual open project so that myProject.Project (can't seem to set the default property with Attribute DefaultMember.VB_UserMemId = 0 when that property is an object) represents an actual project. The problem comes along when I try to say:


For Each myTask In myProject.Project.Tasks

where myTask is an instance of cTask, which like cProject, has a Task property of type MSProject.Task. I want myTask to represent the first Task in the actual Project, but to extend that task object with my own cTask module. So like I was able to do with Project above, my custom methods in cTask could be called from myTask, or the MS Project methods could be called from myTask.Task.

To summarize, I want 1) a hierarchy of objects that 2) extend the application's built in objects. Is there a way to do this, or is the language limited in this way?


your problem is very confusing and what you did is something that only an expert like you can understand. i haven't worked on that before but looking forward to make that one a try too. hope you'll find a solution.