I want to create a Function in my C# code, which can be called from VB as if it were a Macro.
i.e. the VB code should be able to do:
sub vb_method
csharp_method("some_parameters")
end sub
and have that call my C# method:
public object csharp_method(String parameter) {
...
}
I know this can be done in C++:
LPXLOPER retval;
LPXLOPER module; // = something
LPXLOPER parameters[] = { module,
"cpp_function", "parameter_type_info",
"MacroName", "text",
2,
... };
Excel4v(xlfRegister, retval, parameter_count, parameters);
This registers my cpp_function
so that it can be called by the name MacroName
. But it's using XLOPER stuff (which is a headache), and it's in C++.
The magic 2
tells Excel to use my function as a macro. Then my c++ code can be called from VB, or using ExecuteExcel4Macro
. I can also use this to register User-Defined Functions (UDF) - just use a magic 1
instead. (more details about xlfRegister here: http://msdn.microsoft.com/en-us/library/bb687900.aspx )
C# makes it very easy to create UDFs, but I need a way to register my functions as macros instead of formulas.
So how can I have a Macro call my C# code?