VBA Array of Project Tasks

C

Chris

Hey All --

I'm trying to create a Project 2003 macro to show a list of tasks for the
ActiveProject in a form... I'm FOR EACH'ing each task into an array but the
values don't seem to accumulate - I only get 1 task in the Array.

The code below is looping correctly; I can MsgBox each Task fine... just
something wrong with my code for the array:

Code:
Private Sub UserForm_Initialize()

Dim T As Task
Dim Ts As Tasks
Dim strTasksArray() As String
Dim intCount As Integer
Dim Total As Integer

Set Ts = ActiveProject.Tasks
intCount = 0

ReDim Preserve strTasksArray(intCount)

For Each T In Ts
On Error Resume Next
If Not (T Is Nothing) Then
strTasksArray(intCount) = T.Name
intCount = intCount + 1
End If
Next T

lstTasks.List() = strTasksArray

End Sub

As said, only one task seems to populate in the array (I assume it's
overwriting the value each time) but if I put "MsgBox T.Name" in the loop, it
displays each value consecutively...

What am I missing? Any help that can be offered to this VBA newbie would be
appreciated!!!

Thanks
 
J

Jim Aksel

The problem is you initialize intCount=0 and then your Redim statement
defines a one element array. Try this for your redim statement:

ReDim Preserve strTasksArray(Ts.Count)

Remember, your strTasksArray is 0 based for index and now you should be fine.

FYI, rather than a MsgBox you might try debug.print([enter a string here])
This prints to the intermediate window and allows the loop to run without
all the annoying "OK" from the message box.

--
If this post was helpful, please consider rating it.

Jim
It''s software; it''s not allowed to win.

Visit http://project.mvps.org/ for FAQs and more information
about Microsoft Project
 
C

Chris

You rock! That was it :)

Jim Aksel said:
The problem is you initialize intCount=0 and then your Redim statement
defines a one element array. Try this for your redim statement:

ReDim Preserve strTasksArray(Ts.Count)

Remember, your strTasksArray is 0 based for index and now you should be fine.

FYI, rather than a MsgBox you might try debug.print([enter a string here])
This prints to the intermediate window and allows the loop to run without
all the annoying "OK" from the message box.

--
If this post was helpful, please consider rating it.

Jim
It''s software; it''s not allowed to win.

Visit http://project.mvps.org/ for FAQs and more information
about Microsoft Project



Chris said:
Hey All --

I'm trying to create a Project 2003 macro to show a list of tasks for the
ActiveProject in a form... I'm FOR EACH'ing each task into an array but the
values don't seem to accumulate - I only get 1 task in the Array.

The code below is looping correctly; I can MsgBox each Task fine... just
something wrong with my code for the array:

Code:
Private Sub UserForm_Initialize()

Dim T As Task
Dim Ts As Tasks
Dim strTasksArray() As String
Dim intCount As Integer
Dim Total As Integer

Set Ts = ActiveProject.Tasks
intCount = 0

ReDim Preserve strTasksArray(intCount)

For Each T In Ts
On Error Resume Next
If Not (T Is Nothing) Then
strTasksArray(intCount) = T.Name
intCount = intCount + 1
End If
Next T

lstTasks.List() = strTasksArray

End Sub

As said, only one task seems to populate in the array (I assume it's
overwriting the value each time) but if I put "MsgBox T.Name" in the loop, it
displays each value consecutively...

What am I missing? Any help that can be offered to this VBA newbie would be
appreciated!!!

Thanks
 
J

Jim Aksel

Na, I just get lucky now and then....
Thank you for the feedback.
--
If this post was helpful, please consider rating it.

Jim
It''s software; it''s not allowed to win.

Visit http://project.mvps.org/ for FAQs and more information
about Microsoft Project



Chris said:
You rock! That was it :)

Jim Aksel said:
The problem is you initialize intCount=0 and then your Redim statement
defines a one element array. Try this for your redim statement:

ReDim Preserve strTasksArray(Ts.Count)

Remember, your strTasksArray is 0 based for index and now you should be fine.

FYI, rather than a MsgBox you might try debug.print([enter a string here])
This prints to the intermediate window and allows the loop to run without
all the annoying "OK" from the message box.

--
If this post was helpful, please consider rating it.

Jim
It''s software; it''s not allowed to win.

Visit http://project.mvps.org/ for FAQs and more information
about Microsoft Project



Chris said:
Hey All --

I'm trying to create a Project 2003 macro to show a list of tasks for the
ActiveProject in a form... I'm FOR EACH'ing each task into an array but the
values don't seem to accumulate - I only get 1 task in the Array.

The code below is looping correctly; I can MsgBox each Task fine... just
something wrong with my code for the array:

Code:
Private Sub UserForm_Initialize()

Dim T As Task
Dim Ts As Tasks
Dim strTasksArray() As String
Dim intCount As Integer
Dim Total As Integer

Set Ts = ActiveProject.Tasks
intCount = 0

ReDim Preserve strTasksArray(intCount)

For Each T In Ts
On Error Resume Next
If Not (T Is Nothing) Then
strTasksArray(intCount) = T.Name
intCount = intCount + 1
End If
Next T

lstTasks.List() = strTasksArray

End Sub

As said, only one task seems to populate in the array (I assume it's
overwriting the value each time) but if I put "MsgBox T.Name" in the loop, it
displays each value consecutively...

What am I missing? Any help that can be offered to this VBA newbie would be
appreciated!!!

Thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top