Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

It's possible to do with ExcelDNA:

var activeCell = new ExcelReference(5, 5);
ExcelAsyncUtil.QueueAsMacro(() => XlCall.Excel(XlCall.xlcSelect, activeCell));

"ExcelAsyncUtil.QueueAsMacro" could be skipped, it's depends on context from which you call Excel command. If you call it from another Excel function - you should wrap it up with QueueAsMacro

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top