Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delphi 7 Excel Ole object text font color

Tags:

How can I set font color in cell (half text other color) in delphi 7 and with MS Excel 2007+ ?

Example: enter image description here

like image 814
InnerWorld Avatar asked Mar 24 '16 10:03

InnerWorld


1 Answers

You use the Characters property of a cell to apply per-character formatting. Here is a very simple example:

uses
  ComObj, ActiveX, Graphics;

var
  Excel: OleVariant;
  Book: OleVariant;
  Cell: OleVariant;

begin
  CoInitialize(nil);
  Excel := CreateOleObject('Excel.Application');
  Book := Excel.WorkBooks.Add;
  Cell := Excel.Cells[1,1];
  Cell.Value := 'I DON''T SAY BLEH BLEH BLEH!!!';
  Cell.Characters(13, 14).Font.Color := clRed;
  Book.SaveAs('temp.xlsx');
  Excel.Quit;
end.

Here 13 is the index of the first character, and 14 is the number of characters to select.

This uses late binding. If you prefer early binding then I would expect that the code would need a little adjustment.

like image 123
David Heffernan Avatar answered Oct 11 '22 18:10

David Heffernan