0

IS it possible to write a function that accepts =getContent("keyword",TODAY()-30,TODAY()) what should be the type of parameter then?

tried public static object test(object date) { return DateTime.FromOADate(Convert.ToDouble(date)); } // output 41180

user1618820
  • 109
  • 1
  • 2
  • 11

2 Answers2

1

Yes - Excel will evaluate the TODAY() function before passing your function the value. You can declare parameters as DateTime if you want Excel-DNA to interpreted the Excel doubles as Date/Time values. So you might write:

public static object getContent(string keyword, DateTime startDate, DateTime endDate)
{
    // do the work...
    return result;
}
Govert
  • 16,387
  • 4
  • 60
  • 70
  • I need to use object datatype I tried DateTime.FromOADate(Convert.ToDouble(date)); but it doesn't help:( Any Idea ? – user1618820 Sep 28 '12 at 09:21
  • If you call DateTime.FromOAData(41108) you get a DateTime (2012/07/18 00:00:00). If you return this value to Excel and format the cell as a Date/Time, Excel will display the corresponding date. – Govert Sep 28 '12 at 20:00
0

In the more recent versions of Excel DNA, 'DateTime' parameters are not supported. You will need to use double, and then in your C# method body convert the double to DateTime using DateTime.FromOADate().

public static object getContent(string keyword, double startDate, double endDate)
{
    DateTime firstDate = DateTime.FromOADate(startDate);
    DateTime lastDate = DateTime.FromOADate(endDate);

    // do the work...
    return result;
}
SF Lee
  • 1,767
  • 4
  • 17
  • 32