+ Reply to Thread
Results 1 to 3 of 3

Excel.Range.Name gives error an exception

  1. #1

    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. #2
    Dirk Behnke
    Guest

    Re: 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.GetActiveObject("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

    [email protected] 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. #3

    Re: 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(WorkSheetSelectionChange);

    //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.GetActiveObject("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
    >
    > [email protected] 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 to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1