using Addin in C# To Change a Range of Cells

S

shahart

Hello,
I am Trying to Write an Addin Function in Excel using C# (.Net 2005),
that will get 2 parameters :
1. A basic data for the function, in a single cell.
2. A Range of cells, that the function will put the output into.

A Want the Range of cells that is the output of the function, to change
dynamically when I change the cell of the input.

I tryed a simple example, as following :

public object WriteArea(int start, object Range)
{
Microsoft.Office.Interop.Excel.Range r;

try
{
r = Range as Microsoft.Office.Interop.Excel.Range;
}
catch
{
return "Not A Range";
}
int startRow = -1;
int startCol = -1;
int EndRow = -1;
int EndColoum = -1;
startRow = r.Row;
startCol = r.Column;
EndRow = r.EntireRow.Count + startRow;
EndColoum = r.EntireColumn.Count + startCol;
if (!r.Cells.AllowEdit)
{
return "Not Allowed";
}
try
{
for (int i = 1; i <= r.EntireRow.Count; i++)
{
for (int k = 1; k <= r.EntireColumn.Count; k++)
{
try
{
if (!((Range)r[i, k]).AllowEdit)
{
return "Editing is not allowed !";
}
((Range)r[i, k]).Value2 = start++;
}
catch (Exception e)
{
return e.Message;
}
}
}
}
catch (Exception e)
{
return e.Message + " U9";
}
return "start Row is " + startRow.ToString() + "start Coloum is
" + startCol.ToString() +
"end row is " + EndRow.ToString() + "end coloum is " +
EndColoum.ToString();
}


It works well when I first Define the function in excel (2007), but when I
change the value of the cell that is the input (the start) The values of the
output range doesn't change accordingly.

I get the Exception :

Exception from HRESULT: 0x800A03EC
what can I do ?

thanks, shahar.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top