473,591 Members | 2,842 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 8435
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
35519
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 utility from my PythonWin (IDE from ActiveSTate),
10
26586
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 customized XML file with the range of an Excel file. But some root names don't exist in the Excel...
22
15327
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 example of early and late binding to Excel from VB .NET. Note that there is a variable naming...
12
3212
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 action using:
3
9896
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 Microsoft.Office.Interop.Excel._Worksheet.Paste(Object Destination, Object Link) at...
7
2420
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 Excel.Application
9
4527
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
2995
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 anyone tell me how to do this? :) Thank you Excel.Workbook workbook =
0
2996
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 information about the error and where it originated in the code. Exception Details:...
0
8362
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
7992
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
8225
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6639
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
5732
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
3850
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
3891
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2378
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
0
1199
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.