Recent

Author Topic: LibreOffice events listener  (Read 293 times)

chris_laz

  • Newbie
  • Posts: 2
LibreOffice events listener
« on: October 07, 2024, 10:13:55 am »
Hi all,
I am attempting to port a Delphi app which runs LibreOffice by automation and listens for document save and close events, but I can't quite get it to work in Lazarus.

I am using Lazarus 3.6/FPC 3.22 on Windows 10, with LibreOffice 7.4 (all 64bit).

The Delphi code implements an event listener for LO by implementing a TAutoObject with an IConnectionPointContainer, but I can't get this to work in Lazarus, as FPC does not appear to include any implementations of the IConnectionPointContainer interface (Delphi has a TConnectionPoints class).
I have tried reimplementing this, but with no success so far.

I can run LO and open a doc in Calc successfully, but my event listener does nothing (but doesn't crash).

Has anyone managed to implement an event listener for LO in Lazarus?
I can share the code I've tried so far if that's any help, or if there is a pre-built event listener somewhere, that would also be a great help!

Thanks
« Last Edit: October 07, 2024, 06:34:49 pm by chris_laz »

cdbc

  • Hero Member
  • *****
  • Posts: 1571
    • http://www.cdbc.dk
Re: LibreOffice events listener
« Reply #1 on: October 07, 2024, 10:20:07 am »
Hi
Yup, let's see the code you have so far...  ;)
Crystal balls are hard to program with  :D
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 2.2.6 up until Jan 2024 from then on it's: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 3.0

egsuh

  • Hero Member
  • *****
  • Posts: 1468
Re: LibreOffice events listener
« Reply #2 on: October 07, 2024, 10:33:01 am »
Yes, I'd like to see your codes as well... (to learn more... ^^)

chris_laz

  • Newbie
  • Posts: 2
Re: LibreOffice events listener
« Reply #3 on: October 07, 2024, 11:10:26 am »
OK here goes -

In main unit

Code: Pascal  [Select][+][-]
  1. var
  2.       libreMgr, libreDsk, libreDoc: OleVariant;
  3.       libreEvents: ILibreOfficeEvents;
  4.  
  5. ...
  6.  
  7.       libreMgr := CreateOleObject('com.sun.star.ServiceManager');
  8.       libreDsk := LibreMgr.createInstance('com.sun.star.frame.Desktop');
  9.  
  10.       libreDoc := libreDsk.loadComponentFromFile(filename etc...);
  11.  
  12.       libreEvents := TLibreOfficeEvents.Create;
  13.       libreEvents.OnPrepareUnload := event handler method
  14.  
  15.       libreDoc.addEventListener(libreEvents);
  16.  

See attached units
LibreOfficeEventListener - implementation of TLibreOfficeEvents with internal ILibreOfficeEvents declaration
LibreOfficeEventListener_TLB - imported LO type library, declaration of LO ILibreOfficeEventListener interface
ActiveXConnectionPoints - custom implementation of IConnectionPointContainer
ComServNoRegisterLaz - override default FPC TComServer implementation to remove registry updates

I am not sure where my original Delphi implementation came from (back in 2011), I must have found some hints online, as I am pretty sure I did not work this all out for myself.
The LO type library must be loaded as a resource in the exe, but does not need to be added to the registry. This allows the app to run without admin privileges, hence the overrides of the default TComServer and TAutoObjectFactory to remove registry updates.

This all compiles OK in Lazarus, but the events do not fire at all after the LO document is opened and the event listener is added, and there are no runtime errors.

 

TinyPortal © 2005-2018