471,599 Members | 1,114 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,599 software developers and data experts.

Excel.Range.Name gives error an exception

Hi All,

I am trying to execute below code but it gives me an COMException

///// Code Start ////
public string GetName(Excel.Range range)
{
try
{
if (range.Name != null)
{
Excel.Name name = range.Name as Name;

if (name.Name != null || name.Name.Length != 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.InteropServices.COMException}
occurred

Any suggestions,
thanks & regards,
Rushi

Nov 17 '05 #1
4 8216
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.GetActiveOb ject("Excel.Application");

Excel._Application excelApp = o as Excel._Application;

// connect to current Excel workBook
Excel.Workbook workBook = excelApp.ActiveWorkbook;
if (workBook == null)
{
throw new ApplicationException("No workbook is currently defined");
}
Excel.Worksheet xlsSheet= (Excel.Worksheet) workBook.ActiveSheet;
Excel.Range xlsRange=(Excel.Range) xlsSheet.Application.ActiveCell ;
Console.WriteLine (GetName(xlsRange));
Console.ReadLine();
xlsRange=(Excel.Range) xlsSheet.Application.ActiveCell ;
Console.WriteLine (GetName(xlsRange));
Console.ReadLine();
}
By the way I have included just to control the exceptions a little better

catch (System.Runtime.InteropServices.COMException exCom)
{
System.Diagnostics.Debug.WriteLine(exCom.Message);
return string.Empty;
}

in your code
Hope it helps to dentify the problem
Dirk

IM***********@gmail.com wrote:
Hi All,

I am trying to execute below code but it gives me an COMException

///// Code Start ////
public string GetName(Excel.Range range)
{
try
{
if (range.Name != null)
{
Excel.Name name = range.Name as Name;

if (name.Name != null || name.Name.Length != 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.InteropServices.COMException}
occurred

Any suggestions,
thanks & regards,
Rushi

Nov 17 '05 #2
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.GetActiveOb ject("Excel.Application");

Excel._Application excelApp = o as Excel._Application;

// connect to current Excel workBook
Excel.Workbook workBook = excelApp.ActiveWorkbook;
if (workBook == null)
{
throw new ApplicationException("No workbook is currently defined");
}
Excel.Worksheet xlsSheet= (Excel.Worksheet) workBook.ActiveSheet;
Excel.Range xlsRange=(Excel.Range) xlsSheet.Application.ActiveCell ;
Console.WriteLine (GetName(xlsRange));
Console.ReadLine();
xlsRange=(Excel.Range) xlsSheet.Application.ActiveCell ;
Console.WriteLine (GetName(xlsRange));
Console.ReadLine();
}
By the way I have included just to control the exceptions a little better

catch (System.Runtime.InteropServices.COMException exCom)
{
System.Diagnostics.Debug.WriteLine(exCom.Message);
return string.Empty;
}

in your code
Hope it helps to dentify the problem
Dirk

IM***********@gmail.com wrote:
Hi All,

I am trying to execute below code but it gives me an COMException

///// Code Start ////
public string GetName(Excel.Range range)
{
try
{
if (range.Name != null)
{
Excel.Name name = range.Name as Name;

if (name.Name != null || name.Name.Length != 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.InteropServices.COMException}
occurred

Any suggestions,
thanks & regards,
Rushi

Nov 17 '05 #3
Hi Dirk and Others,

I am creating an Add-in in excel. And I am getting the range(cell) by
writing SheetSelectionChange 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.officeApplication as Excel).SheetSelectionChange += new
Excel.AppEvents_SheetSelectionChangeEventHandler(W orkSheetSelectionChange);

//Delegate function
protected void WorkSheetrangeChange(object sh, Excel.Range range)
{
for(int i = 1; i <= range.Areas.Count; 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.Areas[i].Cells[rowNo,colNo]));
}
}
}
}
public string GetName(Excel.Range range)
{
try
{
if (range.Name != null)
{
Excel.Name name = range.Name as Name;

if (name.Name != null || name.Name.Length != 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 excelApplicaiton.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.GetActiveOb ject("Excel.Application");

Excel._Application excelApp = o as Excel._Application;

// connect to current Excel workBook
Excel.Workbook workBook = excelApp.ActiveWorkbook;
if (workBook == null)
{
throw new ApplicationException("No workbook is currently defined");
}
Excel.Worksheet xlsSheet= (Excel.Worksheet) workBook.ActiveSheet;
Excel.Range xlsRange=(Excel.Range) xlsSheet.Application.ActiveCell ;
Console.WriteLine (GetName(xlsRange));
Console.ReadLine();
xlsRange=(Excel.Range) xlsSheet.Application.ActiveCell ;
Console.WriteLine (GetName(xlsRange));
Console.ReadLine();
}
By the way I have included just to control the exceptions a little better

catch (System.Runtime.InteropServices.COMException exCom)
{
System.Diagnostics.Debug.WriteLine(exCom.Message);
return string.Empty;
}

in your code
Hope it helps to dentify the problem
Dirk

IM***********@gmail.com wrote:
Hi All,

I am trying to execute below code but it gives me an COMException

///// Code Start ////
public string GetName(Excel.Range range)
{
try
{
if (range.Name != null)
{
Excel.Name name = range.Name as Name;

if (name.Name != null || name.Name.Length != 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.InteropServices.COMException}
occurred

Any suggestions,
thanks & regards,
Rushi


Nov 17 '05 #4
Hi Dirk and Others,

I am creating an Add-in in excel. And I am getting the range(cell) by
writing SheetSelectionChange 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.officeApplication as Excel).SheetSelectionChange += new
Excel.AppEvents_SheetSelectionChangeEventHandler(W orkSheetSelectionChange);

//Delegate function
protected void WorkSheetrangeChange(object sh, Excel.Range range)
{
for(int i = 1; i <= range.Areas.Count; 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.Areas[i].Cells[rowNo,colNo]));
}
}
}
}
public string GetName(Excel.Range range)
{
try
{
if (range.Name != null)
{
Excel.Name name = range.Name as Name;

if (name.Name != null || name.Name.Length != 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 excelApplicaiton.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.GetActiveOb ject("Excel.Application");

Excel._Application excelApp = o as Excel._Application;

// connect to current Excel workBook
Excel.Workbook workBook = excelApp.ActiveWorkbook;
if (workBook == null)
{
throw new ApplicationException("No workbook is currently defined");
}
Excel.Worksheet xlsSheet= (Excel.Worksheet) workBook.ActiveSheet;
Excel.Range xlsRange=(Excel.Range) xlsSheet.Application.ActiveCell ;
Console.WriteLine (GetName(xlsRange));
Console.ReadLine();
xlsRange=(Excel.Range) xlsSheet.Application.ActiveCell ;
Console.WriteLine (GetName(xlsRange));
Console.ReadLine();
}
By the way I have included just to control the exceptions a little better

catch (System.Runtime.InteropServices.COMException exCom)
{
System.Diagnostics.Debug.WriteLine(exCom.Message);
return string.Empty;
}

in your code
Hope it helps to dentify the problem
Dirk

IM***********@gmail.com wrote:
Hi All,

I am trying to execute below code but it gives me an COMException

///// Code Start ////
public string GetName(Excel.Range range)
{
try
{
if (range.Name != null)
{
Excel.Name name = range.Name as Name;

if (name.Name != null || name.Name.Length != 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.InteropServices.COMException}
occurred

Any suggestions,
thanks & regards,
Rushi


Nov 17 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

13 posts views Thread by Allison Bailey | last post: by
10 posts views Thread by Maik | last post: by
22 posts views Thread by Howard Kaikow | last post: by
12 posts views Thread by elziko | last post: by
7 posts views Thread by Alain \Mbuna\ | last post: by
9 posts views Thread by Doug Glancy | last post: by
13 posts views Thread by chuckie_9497 | last post: by
2 posts views Thread by XIAOLAOHU | last post: by
reply views Thread by Anwar ali | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.