Hello,
Here is an example F# code that runs simpleCommand function immediately.
[<ExcelCommand()>]
let simpleCommand () =
ExcelDnaUtil.Application?Range("A1")?Value2 <- "example value"
[<ExcelFunction(IsMacroType=true)>]
let schedule () =
ExcelDnaUtil.Application?OnTime(DateTime.Now, "simpleCommand")
The problem that I have, is that I can't pass arguments to ExcelCommand.
According to MSDN, it isn't possible to pass arguments to VBA Application.OnTime:
Procedure must take no arguments and cannot be declared in a custom class or form.
However, people do pass parameters with OnTime:
I have created a small example in VBA according to link above and it worked:
Sub Test()
Application.OnTime Now, "'CallMe ""test""'"
End Sub
Public Sub CallMe(str As String)
MsgBox (str)
End Sub
I tried to use similar approach in F#:
[<ExcelCommand()>]
let simpleCommand
([<ExcelArgument()>] s: string) =
ExcelDnaUtil.Application?Range("A1")?Value2 <- s
[<ExcelFunction(IsMacroType=true)>]
let schedule () =
ExcelDnaUtil.Application?OnTime(DateTime.Now, @"'simpleCommand ""test""'")
, but I get an excel error:
Cannot run the macro "'simpleCommand ""test""'". The macro may not be available in this workbook or all macros may be disabled.
I can even try to run previously defined macro CallMe, but it works only when I set a default parameter:
Public Sub CallMe(Optional str As String = "Default")
MsgBox (str)
End Sub
I can then call it from F#:
[<ExcelFunction(IsMacroType=true)>]
let schedule () =
ExcelDnaUtil.Application?OnTime(DateTime.Now, @"'CallMe ""Test""'")
However, I can't replicate this with ExcelDNA.
I've tried to create static method with default optional argument, but it doesn't work:
[<AbstractClass; Sealed>]
type Commands private () =
[<ExcelCommand()>]
static member CommandWithArg([<Optional; DefaultParameterValue(""); ExcelArgument()>]s: string) =
ExcelDnaUtil.Application?Range("A1")?Value2 <- s
Do you have any idea if I can't make it work for ExcelCommands?
Best,
Rafal