How to read horizontal (or vertical) alignment of Excel.Range ?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Antonio Cabal
    New Member
    • Jan 2010
    • 2

    How to read horizontal (or vertical) alignment of Excel.Range ?

    Hi, all!

    In my program I need to read alignment of some cells in Range, but this code:

    Code:
    if (((Excel.Range)r.Cells[1, 1]).HorizontalAlignment == Excel.XlHAlign.xlHAlignCenter )
    {
       // do something ...
    }
    isn't work because "Operator '==' cannot be applied to operands of type 'object' and 'Microsoft.Offi ce.Interop.Exce l.XlHAlign'" compiler error.

    How can I do it???

    Thanks.
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    That error message is telling you that
    ((Excel.Range)r .Cells[1, 1]).HorizontalAli gnment
    . and .
    Excel.XlHAlign. xlHAlignCenter
    are not even of the same type that can be compared.

    You need to compare the same properties
    Compare the .HorizaontalAli gnment of one to the .HorizontalAlig nment of the other. You may have to cast one result to the right type before you compare.

    For example you can't compare the integer 2 to a text string of "2" until you cast the integer to a string, or the string to an integer.

    Comment

    • Antonio Cabal
      New Member
      • Jan 2010
      • 2

      #3
      Thank you, this work as I want

      Code:
      if ([B](Excel.XlHAlign)[/B]((Excel.Range)r.Cells[1, 1]).HorizontalAlignment == Excel.XlHAlign.xlHAlignCenter)
      {
         // do something ...
      }

      Comment

      Working...