How to: Programmatically apply color to Excel ranges
Applies to: Visual Studio Visual Studio for Mac
Note
This article applies to Visual Studio 2017. If you're looking for the latest Visual Studio documentation, see Visual Studio documentation. We recommend upgrading to the latest version of Visual Studio. Download it here
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.
Use a NamedRange control
This example is for document-level customizations.
To apply color to a NamedRange control
Create a NamedRange control at cell A1.
Microsoft.Office.Tools.Excel.NamedRange rng = this.Controls.AddNamedRange(this.Range["A1"], "NamedRange1");
Dim rng As Microsoft.Office.Tools.Excel.NamedRange = _ Me.Controls.AddNamedRange(Me.Range("A1"), "NamedRange1")
Set the color of the text in the NamedRange control.
rng.Font.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red);
rng.Font.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red)
Use native Excel ranges
To apply color to a native Excel range object
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);
Dim rng2 As Excel.Range = Me.Application.Range("A1") rng2.Font.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red)