Recent

Author Topic: Excel COM automation, assigning event  (Read 916 times)

r_1gm

  • New Member
  • *
  • Posts: 28
Excel COM automation, assigning event
« on: October 22, 2024, 05:39:48 am »
Hi,

Code: Pascal  [Select][+][-]
  1. program open_excel;
  2.  
  3. {$IFDEF FPC}
  4.  {$MODE Delphi}
  5. {$ELSE}
  6.  {$APPTYPE CONSOLE}
  7. {$ENDIF}        
  8.  
  9.  
  10. uses
  11.   SysUtils, Variants, ComObj;
  12.  
  13.  
  14. procedure BeforeSave(SaveAsUI : OLEVariant; var Cancel: OLEVariant);
  15. begin
  16. end;
  17.  
  18. const
  19.   ServerName = 'Excel.Application';
  20. var
  21.   XLApp     : OLEVariant;
  22.   Path      : string;
  23.   Password  : string;
  24.   Workbook: OLEVariant;
  25. begin
  26.  
  27.   try
  28.     XLApp := CreateOleObject(ServerName);
  29.  
  30.     {Open existing document}
  31.     Path:= 'D:/Book.xlsx';
  32.  
  33.     Workbook:= XLApp.Workbooks.Open(Path);
  34.  
  35.     Workbook.BeforeSave:=BeforeSave;
  36.  
  37.   finally
  38.  
  39.   end;                    
  40. end.            
  41.  

anybody know how to assign event to excel com object ?

i got error when compiling
open_excel.lpr(35,36) Error: Type is not automatable: "BeforeSave(var OleVariant;OleVariant);"

rvk

  • Hero Member
  • *****
  • Posts: 7017
Re: Excel COM automation, assigning event
« Reply #1 on: October 22, 2024, 10:34:18 am »
I don't think that's going to work like that (you can't just assign an event when using automation like that, I think).

If you create a tlb from EXCEL.EXE you see how it can be done.
Install the package LazActiveX in the IDE and after rebuilding create a TLB for EXCEL.EXE.
(Tools > Import Type Library > choose EXCEL.EXE and create)

You see it works with EventSinkInvoke (in Delphi it makes an InvokeEvent).
From there you can call your own OnWorkbookBeforeSave.

Some of it is explained on this page (for Delphi but should be the same for Lazarus):
https://www.gtro.com/delphi/comevents_e.php

From excel_1_9_tlb.pas created by the type library:
Code: Pascal  [Select][+][-]
  1. procedure TEvsApplication.EventSinkInvoke(Sender: TObject; DispID: Integer;
  2.   const IID: TGUID; LocaleID: Integer; Flags: Word; Params: tagDISPPARAMS;
  3.   VarResult, ExcepInfo, ArgErr: Pointer);
  4. begin
  5.   case DispID of
  6.     1610612736: if assigned(OnQueryInterface) then
  7.           OnQueryInterface(Self, OleVariant(Params.rgvarg[1]), OleVariant(Params.rgvarg[0]));
  8. //...
  9.     1570: if assigned(OnWorkbookBeforeClose) then
  10.           OnWorkbookBeforeClose(Self, OleVariant(Params.rgvarg[1]), Params.rgvarg[0].pbool^);
  11.     1571: if assigned(OnWorkbookBeforeSave) then
  12.           OnWorkbookBeforeSave(Self, OleVariant(Params.rgvarg[2]), OleVariant(Params.rgvarg[1]), Params.rgvarg[0].pbool^);
  13.     1572: if assigned(OnWorkbookBeforePrint) then
  14.           OnWorkbookBeforePrint(Self, OleVariant(Params.rgvarg[1]), Params.rgvarg[0].pbool^);
  15. //...

I also see there is a simular topic here: https://forum.lazarus.freepascal.org/index.php?topic=27667.0

Thaddy

  • Hero Member
  • *****
  • Posts: 19147
  • Glad to be alive.
Re: Excel COM automation, assigning event
« Reply #2 on: October 22, 2024, 10:37:11 am »
Correct. Using event is better done using early binding.
objects are fine constructs. You can even initialize them with constructors.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12850
  • FPC developer.
Re: Excel COM automation, assigning event
« Reply #3 on: October 22, 2024, 11:00:43 am »
There was a post a few weeks back that eventsinks weren't working for openoffice. I haven't had time to dive into that :

https://forum.lazarus.freepascal.org/index.php/topic,68806.msg532528.html#msg532528

r_1gm

  • New Member
  • *
  • Posts: 28
Re: Excel COM automation, assigning event
« Reply #4 on: October 22, 2024, 12:40:49 pm »
OMG,

thank you all

I will try to learn it slowly, the topic pretty advance beyond my imagination :o

 

TinyPortal © 2005-2018