views:

1381

answers:

3

Can you help me rewriting the following Excel VB code to C#?

Range("C9:E11").Select
Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlEqual, _
    Formula1:="=1"
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Interior
    .PatternColorIndex = xlAutomatic
    .Color = 255
    .TintAndShade = 0
End With
Selection.FormatConditions(1).StopIfTrue = False

Thanks in advance!

A: 

Here is a free website that will do conversions but not the best for use all the time. But if you just want someplace to start: http://www.developerfusion.com/tools/convert/vb-to-csharp/

Avitus
+4  A: 

What you're looking for is Excel Automation. Pretty much this means using a set of COM objects provided by Excel to remote control Excel from another application.

Anything you can do with with VBA you can achieve with Automation (OK, almost anything).

If you google for "Excel Automation C#" you'll get lots of hits. How to automate Microsoft Excel from Microsoft Visual C# .NET was the first returned to me, and looks like a good place to get started.

Hope this helps,

Binary Worrier
+1  A: 
 using Excel = Microsoft.Office.Interop.Excel;
 ...
 object mis = Type.Missing;

 Excel.FormatCondition cond =
    (Excel.FormatCondition)range.FormatConditions.Add(Excel.XlFormatConditionType.xlCellValue,
    Excel.XlFormatConditionOperator.xlEqual, "=1",
    mis, mis, mis, mis, mis);
    cond.Interior.PatternColorIndex = Excel.Constants.xlAutomatic;
    cond.Interior.TintAndShade = 0;
    cond.Interior.Color = ColorTranslator.ToWin32(Color.White);
    cond.StopIfTrue = false;