0

I have an application that creates an ExcelWorkbook. In workbook have some sheets and i want to change color of the tab color for each sheets. Example Code Below:

ExcelApplication.WorkBooks[1].WorkSheets[1].Name := 'Sheet1';
ExcelApplication.WorkBooks[1].WorkSheets[1].Name.ColorIndex := 3; // Raise in here

Is there any suggestion?

  • 1
    You could start by actually reading the code you posted. Since you clearly know `Name` is a string when you write `.Name := 'Sheet1';`, you should figure out that that `string` does not have a `ColorIndex`, because `string` doesn't have a color. Common sense would indicate that to change the **tab** color, you should look for properties of **tab** – Ken White Aug 07 '15 at 12:49

1 Answers1

4

I think you're trying to set a property which doesn't exist, i.e. the Name property of a WorkSheet's tab doesn't have a ColorIndex (or Color) sub-property.

The following code sets the tab's color to red for me:

  WorkBook.WorkSheets[1].Select;
  WorkBook.WorkSheets[1].Tab.Color := 255;
  WorkBook.WorkSheets[1].Tab.TintAndShade := 0;

Btw, code completion in the Delphi IDE should work if your unit Uses the interface unit for Excel and you are doing "early binding" against Excel objects. The one for Excel2000 which came with D7 is called Excel2000.Pas. However, a strange thing about it is that the WorkSheet interface definition in there does not include a Tab property. I don't know why. However, if you go into the Visual Basic part of Excel (via the Developer tab on the "ribbon" in Excel2007 and later), there is OLH for the Excel objects and this does include the WorkSheet Tab property. That means that you can access it, but using "late binding", i.e. using Variants, rather than early binding.

MartynA
  • 30,454
  • 4
  • 32
  • 73
  • Don't know but i can't see properties of Excel2000 methods. Delphi code completion doesn't work. But thank you for your answer MartynA ! – Can Emre Unal Aug 07 '15 at 14:49
  • I've added to my answer to mention code completion, and that this won't work in the case of the WorkSheet Tab property. – MartynA Aug 07 '15 at 16:54