473,394 Members | 1,671 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 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 8405
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: Allison Bailey | last post by:
Hi Folks, I'm a brand new Python programmer, so please point me in the right direction if this is not the best forum for this question.... I would like to open an existing MS Excel spreadsheet...
10
by: Maik | last post by:
Hello, I've got a problem with access to special ranges. I renamed some cells (for example "C4" in "CUSTOM.GENERAL.VPRIM"). This is necessary, because I have to read out only these cells in...
22
by: Howard Kaikow | last post by:
There's a significant problem in automating Excel from VB .NET. Reminds me of a problem I encountered almost 3 years ago that was caused by the Norton Auntie Virus Office plug-in. Can anybody...
12
by: elziko | last post by:
I'm using late binding (I must) to automate Excel. My code opens Excel after createing and poulating some sheets. My problem is that when the user finally decides to close Excel its process is...
3
by: | last post by:
I wrote a class in VB.NET to export the contents of a datagrid to Excel. It works perfectly on my machine, but it fails on my customers' PCs that have identical versions of Win XP (SP1) and Excel...
7
by: Alain \Mbuna\ | last post by:
Hi everybody. In my program I have some data that is calculated after some input from the user. I have written some code that opens an Excel workbook, with 5 worksheets and the calculated data...
9
by: Doug Glancy | last post by:
I got the following code from Francesco Balena's site, for disposing of Com objects: Sub SetNothing(Of T)(ByRef obj As T) ' Dispose of the object if possible If obj IsNot Nothing AndAlso...
13
by: chuckie_9497 | last post by:
hello all you gurus. I am struggling with releasing com objects. I have isolated the problem to the code below. Objects are released and the process ends until I use "int k = sheet.Count;" Then...
0
by: Anish G | last post by:
Hi All, I am getting the below given error while running my application in live server. In my local machine, its working fine. Please help me as it is very urgent for me. Exception from...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.