Recent

Author Topic: LibreOffice events listener  (Read 990 times)

chris_laz

  • Newbie
  • Posts: 4
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: 1663
    • 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: 1491
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: 4
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.

chris_laz

  • Newbie
  • Posts: 4
Re: LibreOffice events listener
« Reply #4 on: November 12, 2024, 03:31:45 pm »
No suggestions?
Has anyone else managed to hook LibreOffice automation events in a Lazarus app?

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11942
  • FPC developer.
Re: LibreOffice events listener
« Reply #5 on: November 12, 2024, 04:16:36 pm »
Could you also add a small project to see how you test? I can at least test it on development versions of FPC.

chris_laz

  • Newbie
  • Posts: 4
Re: LibreOffice events listener
« Reply #6 on: November 12, 2024, 05:18:32 pm »
OK, here's a very simple test project, supplied as a zip. It should compile as-is in Lazarus, all required units are included (I think), plus a sample CSV file.

A form with one button and a memo.
Click the button to open a CSV file in Calc.
Start editing the CSV, and Calc's OnModified events should be logged in the memo - but nothing happens here, so obviously my plumbing is wrong.

I have Lazarus 3.6, FPC 3.2.2



Curt Carpenter

  • Hero Member
  • *****
  • Posts: 564
Re: LibreOffice events listener
« Reply #7 on: November 12, 2024, 06:18:05 pm »
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.

I've been wrestling with some old code issues for the past two weeks too -- wonderfully frustrating isn't it?  So many things have changed in the past thirteen years (across so many different interfaces in your case -- mine is much simpler   :)).   

 

TinyPortal © 2005-2018