I want to force the focus onto a particular cell, which I have a reference for via XlCall.Excel(XlCall.xlfCaller). I know how to do this in VSTO, but is there a way to do it with ExcelDNA?
One way would be to use the COM Automation interface, as you would from VBA or VSTO. Just be sure to use the Application
object you get from ExcelDnaUtil.Application
as your root. To convert from the ExcelReference
that you received from xlfCaller
to a COM Range
object, you might try (this is the VB.NET version):
Private Function ReferenceToRange(ByVal xlRef As ExcelReference) As Object
Dim strAddress As String = XlCall.Excel(XlCall.xlfReftext, xlRef, True)
ReferenceToRange = ExcelDnaUtil.Application.Range(strAddress)
End Function
If you want to stick with the C API, you'd first have to select the right sheet, then the actual cell. So you might have:
string myCellSheet = (string)XlCall.Excel(XlCall.xlSheetNm, myCell);
XlCall.Excel(XlCall.xlcWorkbookSelect, new object[] { myCellSheet });
XlCall.Excel(XlCall.xlcFormulaGoto, myCell);
You won't be able to change the selection in a worksheet function, so I presume you are calling this from a macro or ribbon handler.