Recent

Author Topic: [Solved] change details Office file !  (Read 520 times)

majid.ebru

  • Hero Member
  • *****
  • Posts: 508
[Solved] change details Office file !
« on: April 25, 2024, 06:36:57 am »
Hi

please help and guide me  :-[

how can i change details office file ?
« Last Edit: April 27, 2024, 06:41:19 am by majid.ebru »

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2268
  • Fifty shades of code.
    • Delphi & FreePascal
Re: change details Office file !
« Reply #1 on: April 25, 2024, 07:05:44 am »
Hi

please help and guide me  :-[

how can i change details office file ?
By using Microsoft Office COM abilities.
Code: Pascal  [Select][+][-]
  1. program project1;
  2.  
  3. {$APPTYPE CONSOLE}
  4.  
  5. uses
  6.  SysUtils, ComObj, ActiveX;
  7.  
  8. procedure ExcelMetadata;
  9. var
  10.   ExcelApp, Workbook, Worksheet: OLEVariant;
  11. begin
  12.   ExcelApp := CreateOleObject('Excel.Application');
  13.   try
  14.     ExcelApp.Visible := False;
  15.     // path MUST be absolute
  16.     Workbook := ExcelApp.Workbooks.Open(ExtractFilePath(ParamStr(0)) + 'demo.xlsx');
  17.     Worksheet := Workbook.Worksheets[1];
  18.     WriteLn('Author: ' + Workbook.BuiltInDocumentProperties('Author').Value);
  19.     WriteLn('By hitting return, Author name will be "Lazarus COM" !');
  20.     ReadLn;
  21.     Workbook.BuiltInDocumentProperties('Author').Value := 'Lazarus COM';
  22.     Workbook.Save;
  23.     Workbook.Close;
  24.  finally
  25.     ExcelApp.Quit;
  26.  end;
  27. end;
  28.  
  29. begin
  30.   CoInitialize(nil);
  31.   try
  32.     ExcelMetadata;
  33.   finally
  34.     CoUninitialize;
  35.   end;
  36.   ReadLn;
  37. end.
If you got no Excel, I can't help.
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

dseligo

  • Hero Member
  • *****
  • Posts: 1246
Re: change details Office file !
« Reply #2 on: April 25, 2024, 12:53:50 pm »
If you use fpspreadsheet, then you can set metada like this:
Code: Pascal  [Select][+][-]
  1. uses ... fpspreadsheet, laz_fpspreadsheet, ... ;
  2.  
  3. procedure TForm1.Button1Click(Sender: TObject);
  4. var excel: TsWorkbook;
  5.     sheet: TsWorksheet;
  6. begin
  7.   excel := TsWorkbook.Create;
  8.   try
  9.     excel.MetaData.CreatedBy := 'Your name';
  10.     excel.MetaData.Subject := 'Your subject';
  11.     excel.MetaData.Title := 'Your title';
  12.  
  13.     sheet := excel.AddWorksheet('sheet1');
  14.     sheet.WriteText(0, 0, 'test A1');
  15.  
  16.     excel.WriteToFile('test.xlsx', True);
  17.   finally
  18.     excel.Free;
  19.   end;
  20. end;

majid.ebru

  • Hero Member
  • *****
  • Posts: 508
Re: [Solved] change details Office file !
« Reply #3 on: April 27, 2024, 06:41:58 am »
thanks  @KodeZwerg  & @dseligo :)

 

TinyPortal © 2005-2018