J
Jane
I am trying to dynamically format cells for a given task and for some reason
(which is probably a stupid one) it isn't working. Here's the code:
private void highlight()
{
try
{
if (!CurrentScheduleIsBlank())
{
DialogResult result = MessageBoxChk.Show(
"Proceeding will clear the previous formatting. Are
you sure you want to do this?", "Background Cell Highlight",
MessageBoxButtons.YesNo, MessageBoxIcon.Warning,
MessageBoxDefaultButton.Button1, frmMessageBoxChk.enuLanguage.English,
"true");
if (result != DialogResult.No)
{
// Apply Status Impact View
ViewProcessing.Apply(Constants.Views.vwStatusImpact);
// Select the entire sheet
Core.Instance.ProjectApplication.SelectSheet();
for (int taskIdx = 1; taskIdx <
Core.Instance.ProjectApplication.ActiveSelection.Tasks.Count; taskIdx++)
{
MsProject.Task task =
Core.Instance.ProjectApplication.ActiveProject.Tasks[taskIdx];
int statusImpactValue =
ProjectUtils.GetTaskValue<Int32>(task,
UserSettings.Default.StatusImpact.ToString());
// Impacted task
if (statusImpactValue > 0)
{
MyTaskProcessing.HightlightCells(task,
(StatusImpactType)statusImpactValue);
}
}
}
}
else
throw new Exception("Cannot highlight cells because the
current schedule does not contain the required scheduling data.");
}
catch (Exception ex) { MessageBox.Show(ex.Message, "Error in
btnHighlightCells_Click"); }
}
public static void HightlightCells(MsProject.Task task, StatusImpactType
impactType)
{
switch (impactType)
{
case StatusImpactType.LateMilestoneActivity:
MyTaskProcessing.FormatTaskCell(task,
Constants.Fields.Finish, MsProject.PjColor.pjWhite, MsProject.PjColor.pjBlue);
break;
case StatusImpactType.EarlyMilestone:
MyTaskProcessing.FormatTaskCell(task,
Constants.Fields.Finish, MsProject.PjColor.pjWhite, MsProject.PjColor.pjBlue);
break;
case StatusImpactType.ActivityDurationIncreased:
MyTaskProcessing.FormatTaskCell(task,
Constants.Fields.Duration, MsProject.PjColor.pjWhite,
MsProject.PjColor.pjBlue);
break;
case StatusImpactType.PredecessorImpactTask:
MyTaskProcessing.FormatTaskCell(task,
Constants.Fields.Predecessors, MsProject.PjColor.pjWhite,
MsProject.PjColor.pjBlue);
break;
case StatusImpactType.MilestoneActivityNeedStatus:
#region Determine % Complete
string percentCompleteString = String.Empty;
if (UserSettings.Default.UsePhysicalPercentComplete)
percentCompleteString =
Constants.Fields.PhysicalPercentComplete;
else
percentCompleteString =
Constants.Fields.PercentComplete;
#endregion
MyTaskProcessing.FormatTaskCell(task,
percentCompleteString, MsProject.PjColor.pjBlack, MsProject.PjColor.pjAqua);
MyTaskProcessing.FormatTaskCell(task,
Constants.Fields.Finish, MsProject.PjColor.pjWhite, MsProject.PjColor.pjBlue);
break;
case StatusImpactType.PredecessorDurationImpactTask:
MyTaskProcessing.FormatTaskCell(task,
Constants.Fields.Duration, MsProject.PjColor.pjBlack,
MsProject.PjColor.pjAqua);
MyTaskProcessing.FormatTaskCell(task,
Constants.Fields.Predecessors, MsProject.PjColor.pjBlack,
MsProject.PjColor.pjAqua);
break;
}
public static void FormatTaskCell(MsProject.Task task, string columnName,
MsProject.PjColor cellColor, MsProject.PjColor fontColor)
{
MsProject.Project project =
Core.Instance.ProjectApplication.ActiveProject;
project.Application.SelectTaskField(task.ID, columnName, false,
0, 0, false, false);
project.Application.ActiveCell.CellColor = cellColor;
project.Application.ActiveCell.FontColor = fontColor;
}
(which is probably a stupid one) it isn't working. Here's the code:
private void highlight()
{
try
{
if (!CurrentScheduleIsBlank())
{
DialogResult result = MessageBoxChk.Show(
"Proceeding will clear the previous formatting. Are
you sure you want to do this?", "Background Cell Highlight",
MessageBoxButtons.YesNo, MessageBoxIcon.Warning,
MessageBoxDefaultButton.Button1, frmMessageBoxChk.enuLanguage.English,
"true");
if (result != DialogResult.No)
{
// Apply Status Impact View
ViewProcessing.Apply(Constants.Views.vwStatusImpact);
// Select the entire sheet
Core.Instance.ProjectApplication.SelectSheet();
for (int taskIdx = 1; taskIdx <
Core.Instance.ProjectApplication.ActiveSelection.Tasks.Count; taskIdx++)
{
MsProject.Task task =
Core.Instance.ProjectApplication.ActiveProject.Tasks[taskIdx];
int statusImpactValue =
ProjectUtils.GetTaskValue<Int32>(task,
UserSettings.Default.StatusImpact.ToString());
// Impacted task
if (statusImpactValue > 0)
{
MyTaskProcessing.HightlightCells(task,
(StatusImpactType)statusImpactValue);
}
}
}
}
else
throw new Exception("Cannot highlight cells because the
current schedule does not contain the required scheduling data.");
}
catch (Exception ex) { MessageBox.Show(ex.Message, "Error in
btnHighlightCells_Click"); }
}
public static void HightlightCells(MsProject.Task task, StatusImpactType
impactType)
{
switch (impactType)
{
case StatusImpactType.LateMilestoneActivity:
MyTaskProcessing.FormatTaskCell(task,
Constants.Fields.Finish, MsProject.PjColor.pjWhite, MsProject.PjColor.pjBlue);
break;
case StatusImpactType.EarlyMilestone:
MyTaskProcessing.FormatTaskCell(task,
Constants.Fields.Finish, MsProject.PjColor.pjWhite, MsProject.PjColor.pjBlue);
break;
case StatusImpactType.ActivityDurationIncreased:
MyTaskProcessing.FormatTaskCell(task,
Constants.Fields.Duration, MsProject.PjColor.pjWhite,
MsProject.PjColor.pjBlue);
break;
case StatusImpactType.PredecessorImpactTask:
MyTaskProcessing.FormatTaskCell(task,
Constants.Fields.Predecessors, MsProject.PjColor.pjWhite,
MsProject.PjColor.pjBlue);
break;
case StatusImpactType.MilestoneActivityNeedStatus:
#region Determine % Complete
string percentCompleteString = String.Empty;
if (UserSettings.Default.UsePhysicalPercentComplete)
percentCompleteString =
Constants.Fields.PhysicalPercentComplete;
else
percentCompleteString =
Constants.Fields.PercentComplete;
#endregion
MyTaskProcessing.FormatTaskCell(task,
percentCompleteString, MsProject.PjColor.pjBlack, MsProject.PjColor.pjAqua);
MyTaskProcessing.FormatTaskCell(task,
Constants.Fields.Finish, MsProject.PjColor.pjWhite, MsProject.PjColor.pjBlue);
break;
case StatusImpactType.PredecessorDurationImpactTask:
MyTaskProcessing.FormatTaskCell(task,
Constants.Fields.Duration, MsProject.PjColor.pjBlack,
MsProject.PjColor.pjAqua);
MyTaskProcessing.FormatTaskCell(task,
Constants.Fields.Predecessors, MsProject.PjColor.pjBlack,
MsProject.PjColor.pjAqua);
break;
}
public static void FormatTaskCell(MsProject.Task task, string columnName,
MsProject.PjColor cellColor, MsProject.PjColor fontColor)
{
MsProject.Project project =
Core.Instance.ProjectApplication.ActiveProject;
project.Application.SelectTaskField(task.ID, columnName, false,
0, 0, false, false);
project.Application.ActiveCell.CellColor = cellColor;
project.Application.ActiveCell.FontColor = fontColor;
}