title: Getting the text and not value with Delphi Ole Automation of Excel
body: <p>I'm looking to extract the text of each cell in a given range for an Excel spreadsheet using Ole Automation in Delphi 7.</p>
  
  <p>Just now I have a function that (assuming the workbook is already open) selects a range from a worksheet and populates a Variant array using the .Value2 function</p>
  
  <pre><code>function GetExcelRange(const AWorkbookIndex: integer; const AWorksheetIndex: integer; const firstCol, lastCol, firstRow, lastRow: integer): Variant;
  var
   AWorkbook, ARange: OleVariant;
  begin
   Result := Unassigned;
   if not VarIsEmpty(ExcelApp) then
   begin
    if ExcelApp.Workbooks.Count &gt;= AWorkbookIndex then
    begin
     AWorkbook := ExcelApp.Workbooks[AWorkbookIndex];
     try
      if AWorkbook.Worksheets.Count &gt;= AWorksheetIndex then
      begin;
       ARange := AWorkbook.WorkSheets[AWorksheetIndex].Range[AWorkbook.WorkSheets[AWorksheetIndex].Cells[firstRow, firstCol],
                                     AWorkbook.WorkSheets[AWorksheetIndex].Cells[lastRow, lastCol]];
       Result := ARange.Value2;
      end;
     finally
      AWorkbook := Unassigned;
      ARange := Unassigned;
     end;
    end;
   end;
  end;
  </code></pre>
  
  <p>What I would expect is I could change the line to be <code>Result := ARange.Text</code> but it returns a null object.</p>
  
  <p>I would prefer to not iterate over each cell whilst the Ole object is active and stick the whole range's text into an array like I'm doing above.</p>