DocumentProperty

Hi Adrian


I have a vague memory of reading somewhere that it's possible to read the document properties of a file without opening the whole file - is this the case? I did come across a .NET post describing the use of virtual mode for this, but a direct read would be useful.

Thanks for the bgColor/fgColor explanation - I forgot about striped fills!

Thanks, Bob

Currently I think you would need to use Virtual mode. We could add a direct way to do this, but I am not sure the user case is big enough to justify adding another method to FlexCel (we already have thousands of methods, so I am very cautious when adding a new one that it is actually needed).


The code isn't so simple because we need to define some events to enter virtual mode, but this is how it could be implemented:



type
TDocReader = class
private
  procedure Cell_Read(const sender: TObject; const e: TVirtualCellReadEventArgs);
  procedure Cell_StartReading(const sender: TObject; const e: TVirtualCellStartReadingEventArgs);
  procedure DoOpenForProperties(const xls: TExcelFile; const FileName: string);
public
  class procedure OpenForProperties(const xls: TExcelFile; const FileName: string); static;
end;


procedure TDocReader.Cell_Read(const sender: TObject; const e: TVirtualCellReadEventArgs);
begin
  e.NextSheet := '';
end;


procedure TDocReader.Cell_StartReading(const sender: TObject; const e: TVirtualCellStartReadingEventArgs);
begin
  e.NextSheet := '';
end;


procedure TDocReader.DoOpenForProperties(const xls: TExcelFile; const FileName: string);
begin
  xls.VirtualMode := true;
  xls.VirtualCellRead := Cell_Read;
  xls.VirtualCellStartReading := Cell_StartReading;
  xls.Open(FileName);
end;


class procedure TDocReader.OpenForProperties(const xls: TExcelFile;
  const FileName: string);
var
  dp: TDocReader;
begin
  dp := TDocReader.Create;
  try
    dp.DoOpenForProperties(xls, FileName);
  finally
    dp.Free;
  end;
end;




Then just call the static method in your code:

[CODE]
var
  xls: TExcelFile;
begin
  xls := TXlsFile.Create(true);
  try
    TDocReader.OpenForProperties(xls, 'c:\myfile.xls');
     xls.DocumentProperties...
     ...
  finally
     xls.Free; 
  end;
end;
[/CODE ]

Thanks Adrian - that will do nicely.


Cheers, Bob