Dynamically change font color

J

Jane

I am having trouble changing the font color. Can someone please help? My
code is below:

Thanks in advance,
Jane

private void btnHighlight_Click(Office.CommandBarButton Ctrl, ref bool
CancelDefault)
{
try
{
if (!Utils.isCurrentScheduleBlank())
{
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(project,
Parameters.Views.vwStatusImpact);

// Select the entire sheet
project.Application.SelectSheet();

for (int taskIdx = 1; taskIdx <
project.Application.ActiveSelection.Tasks.Count; taskIdx++)
{
MsProject.Task task = project.Tasks[taskIdx];

int statusImpactValue =

Convert.ToInt32(task.GetField(UserSettings.Default.StatusImpact));

// Impacted task
if (statusImpactValue > 0)
{
MyTaskProcessing.HightlightCells(project,
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.Project project, MsProject.Task
task, StatusImpactType impactType)
{
switch (impactType)
{
case StatusImpactType.LateMilestoneActivity:

MyTaskProcessing.FormatTaskCell(project, task,
Parameters.Fields.Finish, MsProject.PjColor.pjWhite,
MsProject.PjColor.pjBlue);
break;

case StatusImpactType.EarlyMilestone:

MyTaskProcessing.FormatTaskCell(project, task,
Parameters.Fields.Finish, MsProject.PjColor.pjWhite,
MsProject.PjColor.pjBlue);
break;

case StatusImpactType.ActivityDurationIncreased:

MyTaskProcessing.FormatTaskCell(project, task,
Parameters.Fields.Duration, MsProject.PjColor.pjWhite,
MsProject.PjColor.pjBlue);
break;

case StatusImpactType.PredecessorImpactTask:

MyTaskProcessing.FormatTaskCell(project, task,
Parameters.Fields.Predecessors, MsProject.PjColor.pjWhite,
MsProject.PjColor.pjBlue);
break;

case StatusImpactType.MilestoneActivityNeedStatus:

#region Determine % Complete
string percentCompleteString = String.Empty;
if (UserSettings.Default.UsePhysicalPercentComplete)
percentCompleteString =
Parameters.Fields.PhysicalPercentComplete;
else
percentCompleteString =
Parameters.Fields.PercentComplete;
#endregion

MyTaskProcessing.FormatTaskCell(project, task,
percentCompleteString, MsProject.PjColor.pjBlack, MsProject.PjColor.pjAqua);
MyTaskProcessing.FormatTaskCell(project, task,
Parameters.Fields.Finish, MsProject.PjColor.pjWhite,
MsProject.PjColor.pjBlue);
break;

case StatusImpactType.PredecessorDurationImpactTask:

MyTaskProcessing.FormatTaskCell(project, task,
Parameters.Fields.Duration, MsProject.PjColor.pjBlack,
MsProject.PjColor.pjAqua);
MyTaskProcessing.FormatTaskCell(project, task,
Parameters.Fields.Predecessors, MsProject.PjColor.pjBlack,
MsProject.PjColor.pjAqua);
break;

}
}

public static void FormatTaskCell(MsProject.Project project, MsProject.Task
task, string columnName, MsProject.PjColor cellColor, MsProject.PjColor
fontColor)
{
project.Application.SelectTaskField(task.ID, columnName, false,
0, 0, false, false);
project.Application.ActiveCell.CellColor = cellColor;
project.Application.ActiveCell.FontColor = fontColor;
}
 
R

Rod Gill

Hi,

Try recording a macro of you changing the font color in Project VBA. You
then just have to convert to C#!!

--

Rod Gill
Microsoft MVP for Project

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



Jane said:
I am having trouble changing the font color. Can someone please help? My
code is below:

Thanks in advance,
Jane

private void btnHighlight_Click(Office.CommandBarButton Ctrl, ref bool
CancelDefault)
{
try
{
if (!Utils.isCurrentScheduleBlank())
{
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(project,
Parameters.Views.vwStatusImpact);

// Select the entire sheet
project.Application.SelectSheet();

for (int taskIdx = 1; taskIdx <
project.Application.ActiveSelection.Tasks.Count; taskIdx++)
{
MsProject.Task task = project.Tasks[taskIdx];

int statusImpactValue =

Convert.ToInt32(task.GetField(UserSettings.Default.StatusImpact));

// Impacted task
if (statusImpactValue > 0)
{
MyTaskProcessing.HightlightCells(project,
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.Project project,
MsProject.Task
task, StatusImpactType impactType)
{
switch (impactType)
{
case StatusImpactType.LateMilestoneActivity:

MyTaskProcessing.FormatTaskCell(project, task,
Parameters.Fields.Finish, MsProject.PjColor.pjWhite,
MsProject.PjColor.pjBlue);
break;

case StatusImpactType.EarlyMilestone:

MyTaskProcessing.FormatTaskCell(project, task,
Parameters.Fields.Finish, MsProject.PjColor.pjWhite,
MsProject.PjColor.pjBlue);
break;

case StatusImpactType.ActivityDurationIncreased:

MyTaskProcessing.FormatTaskCell(project, task,
Parameters.Fields.Duration, MsProject.PjColor.pjWhite,
MsProject.PjColor.pjBlue);
break;

case StatusImpactType.PredecessorImpactTask:

MyTaskProcessing.FormatTaskCell(project, task,
Parameters.Fields.Predecessors, MsProject.PjColor.pjWhite,
MsProject.PjColor.pjBlue);
break;

case StatusImpactType.MilestoneActivityNeedStatus:

#region Determine % Complete
string percentCompleteString = String.Empty;
if (UserSettings.Default.UsePhysicalPercentComplete)
percentCompleteString =
Parameters.Fields.PhysicalPercentComplete;
else
percentCompleteString =
Parameters.Fields.PercentComplete;
#endregion

MyTaskProcessing.FormatTaskCell(project, task,
percentCompleteString, MsProject.PjColor.pjBlack,
MsProject.PjColor.pjAqua);
MyTaskProcessing.FormatTaskCell(project, task,
Parameters.Fields.Finish, MsProject.PjColor.pjWhite,
MsProject.PjColor.pjBlue);
break;

case StatusImpactType.PredecessorDurationImpactTask:

MyTaskProcessing.FormatTaskCell(project, task,
Parameters.Fields.Duration, MsProject.PjColor.pjBlack,
MsProject.PjColor.pjAqua);
MyTaskProcessing.FormatTaskCell(project, task,
Parameters.Fields.Predecessors, MsProject.PjColor.pjBlack,
MsProject.PjColor.pjAqua);
break;

}
}

public static void FormatTaskCell(MsProject.Project project,
MsProject.Task
task, string columnName, MsProject.PjColor cellColor, MsProject.PjColor
fontColor)
{
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