Hi Rajini,
You have to be a bit careful when interacting with Excel from a form (whether you use WPF or Windows Forms).
Depending on which thread is running the form, you might find that Excel is not responding to your interaction, or that your calls to Excel might fail because of user interaction in Excel.
To some extent, using a Custom Task Pane instead of an independent form can be easier.
Your VSTO line:
string currentFormula = Globals.ThisAddIn.Application.Range[cellIndex].Formula
is using the COM interface to talk to Excel.
Inside an Excel-DNA add-in you can also do this, you just have to make sure you add the right References (Microsoft.Office.Interop.Excel) and get hold of the right COM Application object (with a call to ExcelDnaUtil.Application).
Once you have the Application object, you can get the ActiveWorkbook, or the ActiveCell or whatever, and get the formula from there.
The other code you show, using XlCall.Excel(XlCall.xlfGetFormula, …), is based on the C API. This will definitely not work if you are running on a separate thread – I think that’s why you’re getting an exception when running this from your forms’ event handler.
For your use, the COM object model will probably work better.
If you want to understand different ways of showing a form from Excel, you can read and look at the code here: https://github.com/Ron-Ldn/DotNetRefEdit
Regards,
Govert
--
You received this message because you are subscribed to the Google Groups "Excel-DNA" group.
To unsubscribe from this group and stop receiving emails from it, send an email to
exceldna+u...@googlegroups.com.
To post to this group, send email to exce...@googlegroups.com.
Visit this group at https://groups.google.com/group/exceldna.
For more options, visit https://groups.google.com/d/optout.