473,548 Members | 2,759 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Excel.Range.Nam e 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.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

Nov 17 '05 #1
4 8432
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

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.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

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 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


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 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


Nov 17 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
35500
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 and extract information from specific worksheets and cells. I'm not really sure how to get started with this process. I ran the COM Makepy...
10
26576
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 active worksheet. The method get_range("RangeName", Missing.Value) is ok as far as I have an existing RangeName. My program compares the root of a...
22
15319
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 reproduce the behavior described below? For this example, I am using Excel 2002 and VS .NET 2002 and VB 6. MSFT KB article 304661 gives a trivial...
12
3206
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 left running until my application closes. I have tried setting my Excel.Application object to Nothing. I have tried to then fore the GC into...
3
9891
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 (SP1) installed. The error is: System.Runtime.InteropServices.COMException(0x800A03EC): Exception from HRESULT: 0x800A03EC. at...
7
2418
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 (no database!)with some titles and info, is entered in the worksheet in a printable format. This is some of the code... Public exlAppl As...
9
4524
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 TypeOf obj Is IDisposable Then DirectCast(obj, IDisposable).Dispose()
13
2990
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 the process does not end. So I feel confident the problem occurrs here. It appears another reference is created that needs to be closed. Can...
0
2992
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 HRESULT: 0x800A03EC Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more...
0
7518
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7711
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7954
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7467
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7805
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5367
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5085
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
1
1932
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1054
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.