Andrew

Andrew

  • NA
  • 2
  • 0

Changing Font of Individual Characters in Excel 2002/2003

Nov 1 2005 9:02 PM

I have been trying to loop through the Selection object and change the font of an individual character. The code freezes at the get_Characters line. Does anyone have a suggestion?

Regards
Andrew


In VBA:

Dim r As Range
For each r in Selection
  if r.value<>"" then
    'change fontof first character of cell to superscript
    r.characters(1,1).font.superscript=true
  end if
next r


Unfortunately C# does not allow a foreach on Selection object.
I got around that another way, by getting the areas of the Selection, and
doing:

in C#

//this is a Extensibility Project: COM Addin , using Excel 2002,
//so the excelApp object comes from the application object in the Connect class

excelApp = (Excel.Application)application;

excel.range Selection =(Excel.Range)excelApp.Selection;

for (int i=1;i<=Selection.Areas.count;i++)

  Excel.Range area=Selection.Areas[i];
  for (int j=1;j<=area.rows.count;j++)
  {
    for (int k=1;k<=area.columns.count;k++)
    {
      Excel.Range theCell=area.cells[j,k];
     
      //change font - simple illustration
      if (!theCell.value2=="")
      {
        //change font of first character of cell to superscript
        //C# freezes at this point
        //is it because get_Characters is read-only?

        theCell.get_Characters(1,1).font.superscript=true;
      }
 
    }
  }
}