Hi Oscar,
Excel does not allow you to make changes to the sheet from a worksheet function call.
So when you try to manipulate the sheet with the Range.Insert() call, it fails.
If you change from a function to a macro (an ExcelCommand instead of an ExcelFunction, returning ‘void’) then your code will work.
I have assigned a shortcut to the macro, but you could call if from a ribbon button or the Alt+F8 run dialog too. Macros in Excel-DNA add-ins are always hidden, so you would need to types the name into the Alt+F8 dialog to run it.
The code I tried looks like this:
[ExcelCommand(ShortCut="^R")] // Shortcut is Ctrl+Shift+R
public static void CompleteRows()
{
var xlApp = (ExcelApp.Application)ExcelDnaUtil.Application;
var xlSelection = (ExcelApp.Range)xlApp.Selection;
xlSelection.EntireRow.Insert();
}
-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/CAK9YKZK9jr2OU3bD%2BLg%3DhdnsvV_pV1_dW9VUahcnZKV23o2ESg%40mail.gmail.com.
Hi Oscar,
Ctrl+Shift+R should work.
Inside a macro, you can access the Excel COM object model just like you would from VBA.
The only important part is to use the Application object that you get back from ExcelDnaUtil.Application, as you did already in your code.
Here is the object model reference:
https://docs.microsoft.com/en-us/office/vba/api/overview/excel/object-model
If you have trouble converting from VBA to C#, you’re welcome to write back.
You can also run the code from a Ribbon extension – I have a small video and tutorial about that on YouTube:
To show a message box, easiest is to add a reference to System.Windows.Forms in your project, then use
MessageBox.Show(…)
-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/CAK9YKZLTbX4q_0UVOJwiQgdggMZx0mdmqC1ffYOAoEE4DT7f6Q%40mail.gmail.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/exceldna/01cc01d6c28c%24f38862e0%24da9928a0%24%40icon.co.za.
Hi Oscar,
There is a C# ribbon sample here: https://github.com/Excel-DNA/Samples/tree/master/Ribbon
For the ComVisible, you probably have an explicit attribute like
[assembly:ComVisible(false)]
In your Properties\AssemblyInfo.cs file.
That makes the default false for all classes.
You can set it to true on the class in C# like this:
[ComVisible(true)]
public class RibbonController : ExcelRibbon
Good luck with your add-in :-)
To view this discussion on the web visit https://groups.google.com/d/msgid/exceldna/CAK9YKZ%2B0%2B6H6-pyxiXPqEuMK9V0pqHJ1uSX--nAf97Ujo72nWg%40mail.gmail.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/exceldna/02f401d6c357%241244fb10%2436cef130%24%40icon.co.za.