View Single Post
  #1   Report Post  
Posted to microsoft.public.excel.programming
Fabz[_2_] Fabz[_2_] is offline
external usenet poster
 
Posts: 4
Default Range.Value2 in C# (Excel 2007, VSTO 2008)?

After doing intense research on the net, I finally found out that I cannot
simply use VSTO 2008 ThisAddIn to create UDFs (for Excel 2007), at least not
without writing any further VBA code to call the UDF defined. I thought this
was possible, but read that actually to write UDFs without VBA I either have
to use COM AddIns or Automation AddIns. I'd be happy to be proved I'm wrong.

So I wrote an Automation AddIn for Excel 2007 (using C#). It again took me a
long time to understand that unlike with VB.NET in C# I simply cannot use
myRange.Value but must use myRange.Value2 instead. Fact is, myRange.Value
simply doesn't exist as a public accessible field in
Microsoft.Office.Interop.Excel. (Of course all the code examples on the net
fail to mention this fact.) How do I then access and modify the value of a
cell?

The following code example works fine:

public String Foo(object Range)
{
Type type = Range.GetType();
return type.ToString();
}

However, the next example does not work:

public String Foo2(object Range)
{
Excel.Range rng = (Excel.Range) Range;
Object obj = (Object) rng.Value2; // (Object)rng.Cells[1,1].Value2 does
not work either
Type type = obj.GetType();
return type.ToString();
}

The result in Excel is #VALUE! independent of what type the method parameter
actually is. I have no clue whatsoever why I cannot access rng.Value2. Can
anyone explain? Thanks.

Fabz