问题
Update: Answered below
I've built a Form in my MS Project VBA that has some formatting buttons on it, particularly a "Done" button that changes the font of the selected cells to Grey and Strikethrough. The subroutine below works fine, except...
Sub SetTaskNameFontDone()
Dim T As Task
If Not (ActiveSelection.Tasks Is Nothing) Then
For Each T In ActiveSelection.Tasks
' Test for blank task row
If Not (T Is Nothing) Then
SelectTaskField Row:=T.ID, Column:="Name", RowRelative:=False
Font32Ex Color:=8355711
Font32Ex Strikethrough:=True
End If
Next T
End If
End Sub
The problem I run into on this is that the Row number seems to be a physical location number, i.e position from the top of the sheet. So if you "close" a task that has subtasks, then the Row number is no longer the same as the Task ID, and the formatting is applied to the wrong row.
Case 1: Sheet when task is open:
VBA Row # TaskID Task
1 1 ParentTask1 (open)
2 2 SubTask1-1
3 3 ParentTask2
Case 2: Sheet when task is closed (note change in row number)
VBA Row # TaskID Task
1 1 ParentTask1 (closed)
2 3 ParentTask2
Two possible approaches:
1) I could do OutlineShowAllTasks at the start of the routine, but that changes the way the outline appears. I haven't found an indicator for which tasks are open, so I can't memorize and restore that, as a compensation for OutlineShowAllTasks.
2) Save the target Task IDs, and then loop through all the rows, and compare the Task ID for each row. However, I haven't found a way to loop through all the rows and get the Task ID from the row.
Any suggestions on how to associate the selected task with the proper row would be appreciated.
回答1:
Received an answer here:
https://social.msdn.microsoft.com/Forums/en-US/c99cbac1-b886-48a5-8d1b-37df2c5caaec/ms-project-vba-associate-task-with-row?forum=project2010custprog
Sub SetTaskNameFontDone()
Dim T As Task
For Each T In ActiveSelection.Tasks
Font32Ex Color:=8355711
Font32Ex Strikethrough:=True
Next T
End Sub
来源:https://stackoverflow.com/questions/33190730/ms-project-vba-associate-task-with-row