title: Excel automation: Range.Find
body: <p>I want to implement <a href="http://www.vbaexpress.com/kb/getarticle.php?kb_id=82" rel="nofollow">this</a> method in my c# program. But I am having trouble filling in the appropriate parameters in a line like</p>
  
  <pre><code>long FirstRow = myWorksheet.Cells.Find(
    What:="*", 
    After:=Range("IV65536"), 
    LookIn:=xlValues,
    LookAt:= xlPart, 
    SearchOrder:=xlByRows,
    SearchDirection:=xlNext).Row
  </code></pre>
  
  <p><a href="http://msdn.microsoft.com/en-us/library/microsoft.office.interop.excel.range.find%28VS.80%29.aspx" rel="nofollow">Here is the documentation for the Range.Find method.</a></p>
  
  <pre><code>Range Find(
      [In] object What, 
      [In, Optional] object After, 
      [In, Optional] object LookIn, 
      [In, Optional] object LookAt, 
      [In, Optional] object SearchOrder, 
      [In, Optional] XlSearchDirection SearchDirection, 
      [In, Optional] object MatchCase, 
      [In, Optional] object MatchByte, 
      [In, Optional] object SearchFormat
  );
  </code></pre>
  
  <p>So basically I don't know how to make the appropriate parameter objects.</p>
  
  <p><strong>Update</strong>
              Excel.Range range;</p>
  
  <pre><code>        object What = "*";
          object After = xlWorkSheet.get_Range("A1", "IV65536");
          object LookIn = "xlValues";
          object LookAt = "xlPart";
          object SearchOrder = "xlByRows";
          Excel.XlSearchDirection SearchDirection = Excel.XlSearchDirection.xlNext;
          object MatchCase = System.Reflection.Missing.Value;
          object MatchByte = System.Reflection.Missing.Value;
          object SearchFormat = System.Reflection.Missing.Value;
  
          range = xlWorkSheet.Cells.Find(
              What,
              After,
              LookIn,
              LookAt,
              SearchOrder,
              SearchDirection,
              MatchCase,
              MatchByte,
              SearchFormat
              );
  </code></pre>
  
  <p>Gives a "COMException was unhandled: Type mismatch. (Exception from HRESULT: 0x80020005 (DISP_E_TYPEMISMATCH))"</p>
  
  <p><strong>Update #2</strong>
  Here is the method so far. The only thing missing is to set and return the range.</p>
  
  <pre><code>    public void RealUsedRange()
      {
          int FirstRow = xlWorkSheet.Cells.Find(
              "*",
              xlWorkSheet.get_Range("IV65536", misValue),
              Excel.XlFindLookIn.xlValues,
              Excel.XlLookAt.xlPart,
              Excel.XlSearchOrder.xlByRows,
              Excel.XlSearchDirection.xlNext,
              System.Reflection.Missing.Value,
              System.Reflection.Missing.Value,
              System.Reflection.Missing.Value
              ).Row;
  
          int FirstColumn = xlWorkSheet.Cells.Find(
              "*",
              xlWorkSheet.get_Range("IV65536", misValue),
              Excel.XlFindLookIn.xlValues,
              Excel.XlLookAt.xlPart,
              Excel.XlSearchOrder.xlByColumns,
              Excel.XlSearchDirection.xlNext,
              System.Reflection.Missing.Value,
              System.Reflection.Missing.Value,
              System.Reflection.Missing.Value
              ).Column;
  
          int LastRow = xlWorkSheet.Cells.Find(
              "*",
              xlWorkSheet.get_Range("IV65536", misValue),
              Excel.XlFindLookIn.xlValues,
              Excel.XlLookAt.xlPart,
              Excel.XlSearchOrder.xlByRows,
              Excel.XlSearchDirection.xlPrevious,
              System.Reflection.Missing.Value,
              System.Reflection.Missing.Value,
              System.Reflection.Missing.Value
              ).Row;
  
          int LastColumn = xlWorkSheet.Cells.Find(
              "*",
              xlWorkSheet.get_Range("IV65536", misValue),
              Excel.XlFindLookIn.xlValues,
              Excel.XlLookAt.xlPart,
              Excel.XlSearchOrder.xlByColumns,
              Excel.XlSearchDirection.xlPrevious,
              System.Reflection.Missing.Value,
              System.Reflection.Missing.Value,
              System.Reflection.Missing.Value
              ).Column;
      }
  </code></pre>