As a reference for the Excel macros (which you are calling through the C API with the xlcXXXX calls) you can find a link to the macro help file at the bottom of this page: Excel C API · Excel-DNA/ExcelDna Wiki (github.com)
The help for FORMULA.FILL refers to the FORMULA help which says:
“
If formula_text contains references, they must be R1C1-style references, such as "=RC[1]*(1+R1C1)". If you are recording a macro when you enter a formula, Microsoft Excel converts A1-style references to R1C1-style references. For example, if you enter the formula =B2*(1+$A$1) in cell C2 while recording, Microsoft Excel records that action as =FORMULA("=RC[-1]*(1+R1C1)").
“
So for your example, you’ll need to write references in the formula in R1C1 format.
Regarding the call to the macro (“Sub”) from a ribbon handler. The C API (anything called as XlCall.Excel) is not directly available in the ribbon callback context. To call the C API (or a method that calls the C API) you can transition to a macro context by calling the ExcelAsyncUtil.QueueAsMacro helper.
So this should work:
Public Sub Macro1(ByVal Control1 As IRibbonControl)
ExcelAsyncUtil.QueueAsMacro(Sub() InsertFormulas("=231+231"))
End Sub
Having said all of that, I would recommend using the COM object model for these macro-style interactions with Excel.
The COM object model is more comprehensive than the C API and you’ll find help and examples more easily.
From inside you Excel-DNA add-in you call ExcelDnaUtil.Application to get hold of the root COM Application object, and from there it mostly works like VBA.
You do need to add a reference in your project to the COM interop assembly – this can be done easily by installing the ExcelDna.Interop package from NuGet.
Then you’d have something like this:
Imports Microsoft.Office.Interop
Public Module Macros
Sub InsertFormulas()
Dim app As Excel.Application = ExcelDnaUtil.Application
app.Range("A1").Value = 7
app.Range("A2").Formula = "=123+A1"
app.Range("A2:A10").FillDown()
End Sub
End Module
-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 view this discussion on the web visit https://groups.google.com/d/msgid/exceldna/f7f3f822-e403-45f9-9910-0c922b988230n%40googlegroups.com.