Hi Govert,
Thanks for your answer.
I agree with you, there is an issue in the dependency tree in manual
calculation mode. As my function do not depend on the updated cell, it
should not be recalculated. This does not occurs in automatic mode.
I confirm that I never had any problem when the dump function was
called from vba with Application.Run (point 3).
I recently had some users starting to use =Dump(SomeFunction())
directly from Excel and noticed the function was called twice in
manual calculation mode.
I followed your advice, setting another cell value from a thread fix
the problem.
[ExcelFunction(IsMacroType = true)]
public static string DumpTest()
{
if (ExcelDnaUtil.IsInFunctionWizard())
return "Waiting for clic on wizard ok button to calculate.";
Application xlApp = (Application)ExcelDnaUtil.Application;
Type xlAppType = xlApp.GetType();
object activeCell = xlAppType.InvokeMember("ActiveCell",
BindingFlags.GetProperty, null, xlApp, null);
Type xlActiveCellType = activeCell.GetType();
Object cellBelow = xlActiveCellType.InvokeMember("Offset",
BindingFlags.GetProperty, null, activeCell, new object[] { 1, 0 });
Thread t = new Thread(DumpFromThread);
t.Start(cellBelow);
return "Dump done";
}
private static void DumpFromThread(Object range)
{
Type xlRangeType = range.GetType();
xlRangeType.InvokeMember("Value", BindingFlags.SetProperty, null,
range, new[] { Type.Missing, "Dump test from thread" });
}
I know Excel is not designed for this, but since I am using delegates
to access Excel Object properties and method, I haven't had any
crash.
Thanks again for ExcelDna, it is a fantastic project.
Bertrand