Home |
Search |
Today's Posts |
|
#1
![]() |
|||
|
|||
![]()
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
![]() |
|||
|
|||
![]()
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
![]() |
|||
|
|||
![]()
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 |
Display Modes | |
|
|
![]() |
||||
Thread | Forum | |||
formula w exception | Excel Discussion (Misc queries) | |||
Exception report in excel | Excel Worksheet Functions | |||
exception generated was c0000005 | Charts and Charting in Excel | |||
Price rounding - exception formula required | Excel Worksheet Functions |