I’ve searched all over but no one has had the same problem that I have.
Basically, The user clicks a cell within Excel, opens my form, fills in values, clicks insert. The program that adds those variables to an Array which then Excel reads and places.
This is my first Office App, so I’m flying purely blind here but this is my pseudo code.
My Solution Edited:
static string GetColumnLetter(int columnNumber)
{
var dividend = columnNumber;
var columnName = String.Empty;
while (dividend > 0)
{
var modulo = (dividend - 1) % 26;
columnName = Convert.ToChar(65 + modulo) + columnName;
dividend = ((dividend - modulo) / 26);
}
return columnName;
}
// Insert into Excel
public static void ExcelInsert(string mFunction, string mColor, int mQty, string mFau, string mPrice)
{
var values = new List<string>
{
mFunction,
mColor,
mQty.ToString(),
mFau,
mPrice
}.ToArray();
var rowNumber = Globals.ThisAddIn.Application.ActiveCell.Row;
var columnNumber = Globals.ThisAddIn.Application.ActiveCell.Column;
var columnLetter = GetColumnLetter(columnNumber);
for (var i = 0; i < values.Count(); i++ )
{
var range = Globals.ThisAddIn.Application.Range[String.Format("{0}{1}", columnLetter, rowNumber)];
range.Value = values[i];
columnNumber++;
columnLetter = GetColumnLetter(columnNumber);
}
}
I would try getting the location of the active cell, and going right, adding each value one at a time as you go like so:
Credit of course to Graham for the GetColumnLetter method, which is about the most genius thing I have ever seen!