I am currently struggling to assign functions to keyboard shortcuts in Excel DNA through the C API integration (xlCall).
Please note that in the code below, the "GW_Addon.CellNumberFormat" calls return a property that is a string that represents the number formatting I would like to use.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ExcelDna.Integration;
using Excel = Microsoft.Office.Interop.Excel;
namespace GW_Addons
{
class NumberFormatingHotkeys : IExcelAddIn
{
public void AutoOpen()
{
XlCall.Excel(XlCall.xlcOnKey, "+^2", "CleanThousandsForamt");
XlCall.Excel(XlCall.xlcOnKey, "+^3", "CleanDateFormat");
XlCall.Excel(XlCall.xlcOnKey, "+^6", "CleanNumberFormat");
}
public void AutoClose()
{
XlCall.Excel(XlCall.xlcOnKey, "+^2", "CleanThousandsForamt");
XlCall.Excel(XlCall.xlcOnKey, "+^3", "CleanDateFormat");
XlCall.Excel(XlCall.xlcOnKey, "+^6", "CleanNumberFormat");
}
/// <summary>
/// Formats dates to show Month - Year. Example Feb-17
/// </summary>
[ExcelCommand]
public static void CleanThousandsForamt()
{
Excel.Range selection = GW_Addons.ActiveExcelRefs.GetActiveSelection();
selection.NumberFormat = GW_Addons.CellNumberFormats.CleanNumberFormat;
}
/// <summary>
/// Formats numbers to show no decimals and commas. Example: 1,234
/// </summary>
[ExcelCommand]
public static void CleanDateFormat()
{
Excel.Range selection = GW_Addons.ActiveExcelRefs.GetActiveSelection();
selection.NumberFormat = GW_Addons.CellNumberFormats.CleanDateFormat;
}
/// <summary>
/// Scales numbers to thousands. 1,234,567 becomes 1,235 (rounding)
/// </summary>
[ExcelCommand]
public static void CleanNumberFormat()
{
Excel.Range selection = GW_Addons.ActiveExcelRefs.GetActiveSelection();
selection.NumberFormat = GW_Addons.CellNumberFormats.CleanNumberFormat;
}
}
}
I believe that Excel is trying to reference a macro that would be contained either in a VBA add-in file or in the book its self. Is it possible to execute a c# subroutine on a hotkey using the C API integration?