Search code examples
exceldelphiautomationdelphi-7excel-2010

Getting the text and not value with Delphi Ole Automation of Excel


I'm looking to extract the text of each cell in a given range for an Excel spreadsheet using Ole Automation in Delphi 7.

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

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 >= AWorkbookIndex then
  begin
   AWorkbook := ExcelApp.Workbooks[AWorkbookIndex];
   try
    if AWorkbook.Worksheets.Count >= 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;

What I would expect is I could change the line to be Result := ARange.Text but it returns a null object.

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.


Solution

  • I infer from your question that you want to read the text contents of the cell as are presented to the user in Excel. I don't think you can perform the operation on an entire range. The way I have done that in the past is like this. Note that I'm using early bound COM.

    function GetCellVariant(const Sheet: ExcelWorksheet; const Row, Col: Integer): OleVariant;
    
      function ErrorText(const Cell: ExcelRange; hr: HRESULT): string;
      const
        ErrorBase=HRESULT($800A0000);
      var
        i: Integer;
      begin
        Result := Cell.Text;
        for i := 1 to Length(Result) do begin
          if Result[i]<>'#' then begin
            exit;
          end;
        end;
        if hr=ErrorBase or xlErrDiv0 then begin
          Result := '#DIV/0!';
        end else if hr=ErrorBase or xlErrNA then begin
          Result := '#N/A';
        end else if hr=ErrorBase or xlErrName then begin
          Result := '#NAME?';
        end else if hr=ErrorBase or xlErrNull then begin
          Result := '#NULL!';
        end else if hr=ErrorBase or xlErrNum then begin
          Result := '#NUM!';
        end else if hr=ErrorBase or xlErrRef then begin
          Result := '#REF!';
        end else if hr=ErrorBase or xlErrValue then begin
          Result := '#VALUE!';
        end else begin
          Result := 'an error';
        end;
      end;
    
    var
      Cell: ExcelRange;
      hr: HRESULT;
    begin
      Cell := GetCellAsRange(Sheet, Row, Col);//effectively this is Sheet.Range
      if VarIsError(Cell.Value, hr) then begin
        raise ECellValueError.CreateFmt(
          'Cell %s contains %s.',
          [R1C1toA1(Row,Col), ErrorText(Cell, hr)]
        );
      end;
      Result := Cell.Value;
    end;
    
    function GetCellString(const Sheet: ExcelWorksheet; const Row, Col: Integer): string;
    var
      Value: Variant;
      Cell: ExcelRange;
    begin
      Value := GetCellVariant(Sheet, Row, Col);
      if VarIsNumeric(Value) then begin
        Cell := GetCellAsRange(Sheet, Row, Col);
        Result := Sheet.Application.WorksheetFunction.Text(Cell.Value, Cell.NumberFormatLocal);
      end else begin
        Result := ConvertToString(Value);//this converts a Variant to string
      end;
    end;
    

    In fact this code came out of the very first question I ever asked here on Stack Overflow: How do I read the formatted textual representation of a cell in Excel