Hi Dirk and Others,
I am creating an Add-in in excel. And I am getting the range(cell) by
writing SheetSelectionC hange Event (Delegate) for Excel applicaiton. In
that function I am directly calling GetName function to get the
associate name for that range....Below is code.
It is possible that user might be select more region in Excel. So I am
doing a loop for Area of each calling range.
///Code Start///
//Declaration for delegate
(this.officeApp lication as Excel).SheetSel ectionChange += new
Excel.AppEvents _SheetSelection ChangeEventHand ler(WorkSheetSe lectionChange);
//Delegate function
protected void WorkSheetrangeC hange(object sh, Excel.Range range)
{
for(int i = 1; i <= range.Areas.Cou nt; i++)
{
for(int rowNo = 1 ; rowNo <= range.Areas[i].Rows.Count; rowNo++)
{
for(int colNo = 1 ; colNo <= range.Areas[i].Columns.Count; colNo++)
{
MessageBox.Show (GetName((Excel .Range)range.Ar eas[i].Cells[rowNo,colNo]));
}
}
}
}
public string GetName(Excel.R ange range)
{
try
{
if (range.Name != null)
{
Excel.Name name = range.Name as Name;
if (name.Name != null || name.Name.Lengt h != 0)
{
return name.Name;
}
return string.Empty;
}
return string.Empty;
}
catch(Exception e)
{
return string.Empty;
}
}
///Code End///
I know, I can do it using excelApplicaito n.Names collection. But I
don't wnat to do that, because using that method I have to look compare
with each cell locaiton. And that is very time consuming.
One thing is clear that this code is working fine for a cell, for which
name is defined.
Thanks
Rushi
Dirk Behnke wrote:
Hi,
I don't know how you got your Range object, but I made the following
test code.
The console wait just gives me an opportunity to change the active cell.
If I select a cell with a defined Name, no COMException is thrown.
[STAThread]
static void Main(string[] args)
{
object o =
System.Runtime. InteropServices .Marshal.GetAct iveObject("Exce l.Application") ;
Excel._Applicat ion excelApp = o as Excel._Applicat ion;
// connect to current Excel workBook
Excel.Workbook workBook = excelApp.Active Workbook;
if (workBook == null)
{
throw new ApplicationExce ption("No workbook is currently defined");
}
Excel.Worksheet xlsSheet= (Excel.Workshee t) workBook.Active Sheet;
Excel.Range xlsRange=(Excel .Range) xlsSheet.Applic ation.ActiveCel l ;
Console.WriteLi ne (GetName(xlsRan ge));
Console.ReadLin e();
xlsRange=(Excel .Range) xlsSheet.Applic ation.ActiveCel l ;
Console.WriteLi ne (GetName(xlsRan ge));
Console.ReadLin e();
}
By the way I have included just to control the exceptions a little better
catch (System.Runtime .InteropService s.COMException exCom)
{
System.Diagnost ics.Debug.Write Line(exCom.Mess age);
return string.Empty;
}
in your code
Hope it helps to dentify the problem
Dirk
IM***********@g mail.com wrote: Hi All,
I am trying to execute below code but it gives me an COMException
///// Code Start ////
public string GetName(Excel.R ange range)
{
try
{
if (range.Name != null)
{
Excel.Name name = range.Name as Name;
if (name.Name != null || name.Name.Lengt h != 0)
{
return name.Name;
}
return string.Empty;
}
return string.Empty;
}
catch(Exception e)
{
return string.Empty;
}
}
///// Code End ////
Now every time my very third line "if (range.Name != null)" gives me
Exception
an exception of type: {System.Runtime .InteropService s.COMException}
occurred
Any suggestions,
thanks & regards,
Rushi