Getting Started with Adobe After Effects - Part 6: Motion Blur
A collection of quick technology learning tips from what people around you learn every day

Check exception-free if an named excel range exists

Aug 21 2013 12:00AM by Lakedaimon   

Problem: Check the existence of a named range in a worksheet without exceptions. Interop-Lib: Microsoft.Office.Interop.Excel

Solution: Try to get the named range with Evaluate. This will respond the error code -2146826259 if no named cell exists or an range object and will not throw any exception. Despite running evaluate on the worksheet it could return named cells defined for the hole workbook which are not on the given worksheet. Thus you have to check if the returned range is on the desired worksheet.

Code:

public static bool NamedCellDefiend(Worksheet ws, string name)
{
     object i;
     i = ws.Evaluate(name);
     if (i.ToString().Equals("-2146826259"))
          return false;
     else
     {
         Range xlRange = (Range)i;
         bool result = xlRange.Worksheet.Equals(ws);
         Marshal.ReleaseComObject(xlRange);
         return result;
      }
}

Usage: Get the value of a named cell exception-free.

If (NamedCellDefined(ws, "Test")
{
  ws.Names("Test").RefersToRange.Value
}
Read More..   [0 clicks]

Published under: Microsoft Excel Tips ·  ·  ·  · 


Lakedaimon
1227 · 0% · 21
0
Liked
 
0
Knew
 
 
0
Incorrect
 
0
Interesting
 
0
Forgotten



Submit

Your Comment


Sign Up or Login to post a comment.

    Copyright © Rivera Informatic Private Ltd Contact us      Privacy Policy      Terms of use      Report Abuse      Advertising      [ZULU1097]