View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming,microsoft.public.dotnet.framework.interop,microsoft.public.dotnet.languages.csharp,microsoft.public.excel.misc
Dirk Behnke Dirk Behnke is offline
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