Yep, you can totally figure out what kind of old Office file it is, even if you renamed the extensions to .ole. These older .doc, .xls, and .ppt files use a special OLE format internally, kinda like a mini file system inside the file.
Each type has a unique stream name inside it:
* Word files have a stream called WordDocument
* Excel files use Workbook or Book
* PowerPoint files have PowerPoint Document
So, you can just peek inside the file and look for those names — no need to rely on the extension at all:
function GetOfficeFileType(const FileName: string): string;
var
Storage: IStorage;
EnumStat: IEnumStatStg;
StatStg: TStatStg;
NumFetched: ULONG;
FileType: string;
begin
FileType := 'Unknown';
OleCheck(StgOpenStorage(PWideChar(WideString(FileName)), nil,
STGM_READ or STGM_SHARE_DENY_WRITE, nil, 0, Storage));
if Assigned(Storage) then
begin
OleCheck(Storage.EnumElements(0, nil, 0, EnumStat));
while EnumStat.Next(1, StatStg, @NumFetched) = S_OK do
begin
if StatStg.pwcsName <> nil then
begin
if WideSameText(StatStg.pwcsName, 'WordDocument') then
FileType := 'DOC'
else if WideSameText(StatStg.pwcsName, 'Workbook') or
WideSameText(StatStg.pwcsName, 'Book') then
FileType := 'XLS'
else if WideSameText(StatStg.pwcsName, 'PowerPoint Document') then
FileType := 'PPT';
CoTaskMemFree(StatStg.pwcsName);
end;
end;
end;
Result := FileType;
end;