Recent

Author Topic: Lazarus and Libre Office  (Read 1278 times)

w click

  • Full Member
  • ***
  • Posts: 190
Lazarus and Libre Office
« on: January 10, 2026, 01:47:40 pm »
I'm trying to get Libre Writer to fire up from a Lazarus program.  I'm in Windows 10.  I tried this:
Code: Pascal  [Select][+][-]
  1. procedure OpenLibre(const FileName: string);
  2. var
  3.   Cmd : string;
  4. begin
  5.   // For Windows:
  6.   try
  7.     showmessage('Ready to Libre <'+filename+'>');
  8.     Cmd := 'C:\Program Files\LibreOffice\program\swriter.exe "' + FileName + '"';
  9.     ShellExecute(0, 'open', PChar(Cmd), nil, nil, SW_SHOWNORMAL);
  10.     showmessage('Libred.');
  11.   except
  12.     showmessage('Libre Office issue');
  13.   end;
  14.  
  15.   // For Linux (example):
  16.   // Cmd := 'libreoffice "' + FileName + '"';
  17.   // ShellExecute(0, 'open', PChar('xdg-open'), PChar('"' + FileName + '"'), nil, SW_SHOWNORMAL);
  18. end;
  19.  

I tried soffice.exe (which was the example I found online) as well to no avail.  Neither does anything.  Can anyone help?

paweld

  • Hero Member
  • *****
  • Posts: 1568
Re: Lazarus and Libre Office
« Reply #1 on: January 10, 2026, 01:59:50 pm »
Code: Pascal  [Select][+][-]
  1. uses
  2.   Process;
  3.  
  4. procedure TForm1.Button1Click(Sender: TObject);
  5. var
  6.   filename: String = 'd:\qqq.odt';
  7.   sout: String;
  8. begin
  9.   RunCommand('C:\Program Files\LibreOffice\program\swriter.exe ', [filename], sout);
  10. end;                    
Best regards / Pozdrawiam
paweld

Thaddy

  • Hero Member
  • *****
  • Posts: 18729
  • To Europe: simply sell USA bonds: dollar collapses
Re: Lazarus and Libre Office
« Reply #2 on: January 10, 2026, 02:04:53 pm »
Alternatively, since you are on Win10:
Code: Pascal  [Select][+][-]
  1. program LibreOfficeWriterAutomation;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. uses
  6.   Classes, SysUtils, ComObj, ActiveX,Variants;
  7.  
  8. var
  9.   ServiceManager: Variant;
  10.   Desktop: Variant;
  11.   Document: Variant;
  12.   Text: Variant;
  13.   Cursor: Variant;
  14.  
  15. begin
  16.   try
  17.     // Initialize COM
  18.     coInitialize(nil);
  19.     // Create the service manager
  20.     ServiceManager := CreateOleObject('com.sun.star.ServiceManager');
  21.    
  22.     // Get the desktop service
  23.     Desktop := ServiceManager.createInstance('com.sun.star.frame.Desktop');
  24.    
  25.     // Create a new Writer document
  26.     Document := Desktop.loadComponentFromURL('private:factory/swriter', '_blank', 0, VarArrayCreate([0, -1], varVariant));
  27.    
  28.     // Get the text object
  29.     Text := Document.getText();
  30.    
  31.     // Create a cursor
  32.     Cursor := Text.createTextCursor();
  33.    
  34.     // Insert some text
  35.     Text.insertString(Cursor, 'Hello World from FreePascal!' + #13#10, False);
  36.     Text.insertString(Cursor, 'This document was created automatically.' + #13#10, False);
  37.     Text.insertString(Cursor, 'Current date and time: ' + DateTimeToStr(Now), False);
  38.    
  39.     // Move cursor to the beginning
  40.     Cursor.gotoStart(False);
  41.    
  42.     // Format the first line (make it bold)
  43.     Cursor.goRight(28, True); // Select "Hello World from FreePascal!"
  44.     Cursor.setPropertyValue('CharWeight', 150); // Bold
  45.    
  46.     // Save the document
  47.     Document.storeAsURL('file:///C:/temp/test_document.odt', VarArrayCreate([0, -1], varVariant));
  48.    
  49.     WriteLn('Document created and saved successfully!');
  50.     WriteLn('Press Enter to close the document...');
  51.     ReadLn;
  52.    
  53.     // Close the document
  54.     Document.close(True);
  55.    
  56.   except
  57.     on E: Exception do
  58.       WriteLn('Error: ' + E.Message);
  59.   end;
  60.   coUninitialize;
  61.  
  62. end.
This starts libreOffice, writes a document, saves it and exists.
« Last Edit: January 10, 2026, 03:37:26 pm by Thaddy »
If Europe sells their USA bonds the USD will collapse. Europe can affort that given average state debts. The USA can't affort that. Just an advice...

w click

  • Full Member
  • ***
  • Posts: 190
Re: Lazarus and Libre Office
« Reply #3 on: January 11, 2026, 01:37:53 pm »
I got RunCommand to work, once I used Process.  Thank you, Paweld.

I do want to go further, so thank you, Thaddy, for that example.  However, you say, 'since you are on Win10'.  I do want to move to Linux, so will that example still work?

Thaddy

  • Hero Member
  • *****
  • Posts: 18729
  • To Europe: simply sell USA bonds: dollar collapses
Re: Lazarus and Libre Office
« Reply #4 on: January 11, 2026, 02:19:25 pm »
Well, no. But I am busy writing uno bindings, like available in Python.
If Europe sells their USA bonds the USD will collapse. Europe can affort that given average state debts. The USA can't affort that. Just an advice...

w click

  • Full Member
  • ***
  • Posts: 190
Re: Lazarus and Libre Office
« Reply #5 on: January 11, 2026, 03:31:21 pm »
But there are equivalents in Lazarus for Linux to control Libre Writer, aren't there?

I'm thinking of dumping Windows and going to Linux Mint, but some programs I've written must, absolutely must, work as they are vital for what I do.  Hence, my questions.  I don't need to do it now, but it must be possible to do in a few months.

CM630

  • Hero Member
  • *****
  • Posts: 1600
  • Не съм сигурен, че те разбирам.
    • http://sourceforge.net/u/cm630/profile/
Re: Lazarus and Libre Office
« Reply #6 on: January 11, 2026, 03:51:16 pm »
If you intend to switch to Linux, bear in mind that probably the only suite, which is able to open a MS Word file almost properly is WPS Office (the former Kingsoft Office). You can report them bugs, and sometimes they react. I have not tried to link it with Lazarus.
The online Google Writer is worse (I use it quite regularly). I have reported them a bug once, they did not respond, but after about half an year it was fixed.
(The lalst time I tried) Libre Office it was even worse.
So before switching, maybe you should try if you can find an office suite that really suits your needs.
« Last Edit: January 11, 2026, 03:55:15 pm by CM630 »
Лазар 4,4 32 bit (sometimes 64 bit); FPC3,2,2

Thaddy

  • Hero Member
  • *****
  • Posts: 18729
  • To Europe: simply sell USA bonds: dollar collapses
Re: Lazarus and Libre Office
« Reply #7 on: January 11, 2026, 03:57:36 pm »
If you intend to switch to Linux, bear in mind that probably the only suite

????
... is either LibreOffice or OpenOffice or even StarOffice and many more.
LibreOffice/OpenOffice opens any ms word format since ages (like over ~10 years or so??). Didn't you know that?
There is an industry consensus to support each others formats.

« Last Edit: January 11, 2026, 04:04:37 pm by Thaddy »
If Europe sells their USA bonds the USD will collapse. Europe can affort that given average state debts. The USA can't affort that. Just an advice...

CM630

  • Hero Member
  • *****
  • Posts: 1600
  • Не съм сигурен, че те разбирам.
    • http://sourceforge.net/u/cm630/profile/
Re: Lazarus and Libre Office
« Reply #8 on: January 11, 2026, 04:05:47 pm »
Yes, it opens .doc files and that is all. But besides that, it shall show the contents properly, which it does not.
I was thinking that it is Oracle which hindered OpenOffice for some commercial reasons. But LibreOffice did not show a significant improvent,
I used to try it once every 3 or 4 years (just open a the same file and see if there is resemblance), but finally I lost hope.
Лазар 4,4 32 bit (sometimes 64 bit); FPC3,2,2

MarkMLl

  • Hero Member
  • *****
  • Posts: 8527
Re: Lazarus and Libre Office
« Reply #9 on: January 11, 2026, 08:54:34 pm »
This isn't my fight, but I think we need to focus on asking the right questions.

But there are equivalents in Lazarus for Linux to control Libre Writer, aren't there?

I'm thinking of dumping Windows and going to Linux Mint, but some programs I've written must, absolutely must, work as they are vital for what I do.  Hence, my questions.  I don't need to do it now, but it must be possible to do in a few months.

Are you trying to automate operations performed by Libre Office (or its siblings) i.e. roughly as demonstrated by Thaddy? In that case we need to know what API it exposes, since AIUI ActiveX/COM (again, with reference to Thaddy's example) is a non-starter on Linux.

Yes, it opens .doc files and that is all. But besides that, it shall show the contents properly, which it does not.
I was thinking that it is Oracle which hindered OpenOffice for some commercial reasons. But LibreOffice did not show a significant improvent,
I used to try it once every 3 or 4 years (just open a the same file and see if there is resemblance), but finally I lost hope.

My experience- and in general the experience of numerous other people using that office suite- is that it is fairly reliable and where it knows that it might not be able to render something accurately (or where saving might change the representation) it raises a warning.

If your experience is otherwise I trust that you have engaged with the maintainers and provided them with example files demonstrating the problems you see.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

Aruna

  • Hero Member
  • *****
  • Posts: 787
Re: Lazarus and Libre Office
« Reply #10 on: January 12, 2026, 02:23:19 am »
But there are equivalents in Lazarus for Linux to control Libre Writer, aren't there?
You can bend LibreOffice to your will from Lazarus , within reason!:)

I'm thinking of dumping Windows and going to Linux Mint, but some programs I've written must, absolutely must, work as they are vital for what I do.  Hence, my questions.  I don't need to do it now, but it must be possible to do in a few months.
Have a look at the attached screenshot first. Then download teh zip file. Unzip and run using Lazarus IDE. I would keep the windows you may need it in the future if you decide one day to do cross compile stuff and need to test.

Usage Notes
Button “Open File”: Select any existing file (.txt or .odt) and open it in LibreOffice Writer.
Button “Create File”: Create a new text file with sample content. You are responsible for specifying  the filename with the correct extension <filename>.odt

I am working on a more complete demo will send your way soon. Good luck!

sydenis

  • Jr. Member
  • **
  • Posts: 55
Re: Lazarus and Libre Office
« Reply #11 on: January 12, 2026, 11:07:38 am »
If you know Java, you can use the a bridge: Lazarus app - Java app - LibreOffice/OpenOffice.
A couple of years ago, I made a bundle where Lazarus app communicates with the Java app via a local socket and then forwarded commands to LibreOffice
In Java a maximal full binding with LibreOffice is implemented.

I am busy writing uno bindings, like available in Python.

It would be wonderful and would greatly simplify all thinks!

Aruna

  • Hero Member
  • *****
  • Posts: 787
Re: Lazarus and Libre Office
« Reply #12 on: January 13, 2026, 09:26:34 pm »
As promised here is GUI module that allows you to launch any LibreOffice module of your choice.  Please do test and give some feedback. Screenshot and zip attached.

As pointed out by MarkMLl: what are you trying to automate? I saw an earlier post by you where you said something about word and wordcount. IS it along similar lines? 

I am also trying to do this without Libreoffice at all and managed to find: FPVectorial example application for writing a text document file to disk.

Are the authors  Felipe Monteiro de Carvalho  and  Mike Thompson on the forum by any chance? I have lots of questions.

MarkMLl

  • Hero Member
  • *****
  • Posts: 8527
Re: Lazarus and Libre Office
« Reply #13 on: January 14, 2026, 09:22:14 am »
Well, no. But I am busy writing uno bindings, like available in Python.

I think that this is the important thing, and am sure that anything you do will be appreciated by the community.

Meanwhile we could still usefully see an explanation from OP of what he's trying to do.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

 

TinyPortal © 2005-2018