How to: Programmatically Apply Color to Excel Ranges

 

To apply a color to text within a range of cells, use a NamedRange control or a native Excel range object.

Applies to: The information in this topic applies to document-level projects and VSTO add-in projects for Excel. For more information, see Features Available by Office Application and Project Type.

This example is for document-level customizations.

To apply color to a NamedRange control

  1. Create a NamedRange control at cell A1.

                Microsoft.Office.Tools.Excel.NamedRange rng =
                    this.Controls.AddNamedRange(this.Range["A1"], "NamedRange1");
    

  2. Set the color of the text in the NamedRange control.

                rng.Font.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red);
    

To apply color to a native Excel range object

  1. Create a range at cell A1 and then set the color of the text.

                Excel.Range rng2 = this.Application.get_Range("A1");
                rng2.Font.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red);
    

Working with Ranges
NamedRange Control
How to: Programmatically Apply Styles to Ranges in Workbooks
How to: Programmatically Refer to Worksheet Ranges in Code
Automating Excel by Using Extended Objects
Optional Parameters in Office Solutions

Show: