Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming,microsoft.public.dotnet.framework.interop,microsoft.public.dotnet.languages.csharp,microsoft.public.excel.misc
external usenet poster
 
Posts: 2
Default 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

  #2   Report Post  
Posted to microsoft.public.excel.programming,microsoft.public.dotnet.framework.interop,microsoft.public.dotnet.languages.csharp,microsoft.public.excel.misc
external usenet poster
 
Posts: 1
Default Excel.Range.Name gives error an exception

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

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

  #3   Report Post  
Posted to microsoft.public.excel.programming,microsoft.public.dotnet.framework.interop,microsoft.public.dotnet.languages.csharp,microsoft.public.excel.misc
external usenet poster
 
Posts: 2
Default Excel.Range.Name gives error an exception

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

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


Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
MS Excel Error - Win32 exception Jahrzee Setting up and Configuration of Excel 0 May 28th 07 03:38 AM
Excel.Range.Name gives error an exception [email protected] Excel Discussion (Misc queries) 2 August 23rd 05 08:14 AM
Excel causing exception error upon opening the document dougmcc1[_8_] Excel Programming 1 October 8th 04 12:21 AM
Excel causing exception error upon opening the document dougmcc1[_4_] Excel Programming 1 September 28th 04 11:12 PM
Excel causing exception error upon opening the document dougmcc1[_6_] Excel Programming 0 September 28th 04 04:16 PM


All times are GMT +1. The time now is 09:21 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"