Dynamically format cell

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;
}
 
R

Rod Gill

What the!! This is amazingly lengthy and complex code for what should be a
very simple action. What exactly do you want to have happen?

--

Rod Gill
Microsoft MVP for Project

Author of the only book on Project VBA, see:
http://www.projectvbabook.com



Jane said:
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;
}
 

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

Similar Threads


Top