Creating a formula for a format condition in Excel

E

elora_c

I'm having a little trouble specifying the formula for a format
condition in Excel. The problem is that the formula ends up with
quotation marks around it and so fails to evaluate problem.

Type ExcelApplicationType = Type.GetTypeFromProgID
("Excel.Application"); ;
Object excelApplicationInstance = Activator.CreateInstance
(ExcelApplicationType);
object excelWorkBook = workBooks.GetType ().InvokeMember
("Open", BindingFlags.InvokeMethod, null, workBooks, new string[]
{ fileName });
object workSheets = excelWorkBook.GetType ().InvokeMember
("Worksheets", BindingFlags.GetProperty, null, excelWorkBook, null);
object excelWorkSheet = workSheets.GetType ().InvokeMember
("Item", BindingFlags.GetProperty, null, workSheets, new object[]
{ 1 });

int columnNumber = 6;
object columns = excelWorkSheet.GetType ().InvokeMember
("Columns", BindingFlags.GetProperty, null, excelWorkSheet, null);
object column = columns.GetType ().InvokeMember ("Item",
BindingFlags.GetProperty, null, columns, new object[]
{ columnNumber });


object formatConditions = column.GetType
().InvokeMember ("FormatConditions", BindingFlags.GetProperty, null,
column, null);

object[] parameters = new object[3];
parameters[0] = 2; // xlExpression
parameters[1] = 3; // Equal
parameters[2] = "F2 < $B$9";
object formatCondition = formatConditions.GetType
().InvokeMember ("Add", BindingFlags.InvokeMethod, null,
formatConditions, parameters);

If I look at the Format Condition in Excel, it shows:
="F1 < $B$9"

If I remove the quotation marks, it works fine. How can I pass this
expression so it is treated as a formula and not a string constant?

Thanks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top