Recent

Author Topic: Calling LibreOffice Writer  (Read 874 times)

w click

  • Full Member
  • ***
  • Posts: 205
Calling LibreOffice Writer
« on: February 28, 2026, 12:51:56 pm »
I've used this:–
Code: Pascal  [Select][+][-]
  1.   Runcommand('C:\Program Files\LibreOffice\program\swriter.exe',['d:\Current Files\test.odt'],sout);
  2.  
And it works fine, but I think runcommand causes the program to hang.  So, I've converted to ShellExecute:–
Code: Pascal  [Select][+][-]
  1.  if ShellExecute(0,nil,PChar('C:\Program Files\LibreOffice\program\swriter.exe'),PChar('d:\Current Files\test.odt'),PChar(''),1)=0
  2.     then form1.label1.caption:='FAIL.'
  3.     else form1.label1.caption:='OK.';
It works for all programs this application opens (Word, Excel, etc), except for LibreOffice. LibreOffice Writer creates an error: it can't find 'd:\current'.  It's that space in 'Current Files'.  Is there a solution to this?  A way of passing a space without it being a parameter terminator or something?

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12706
  • FPC developer.
Re: Calling LibreOffice Writer
« Reply #1 on: February 28, 2026, 01:15:38 pm »
Note that the two ways are fundamentally different. That way, runcommand tries to keep a link 'to the program to capture its output, while shellexecute doesn't.

Shellexecute works like a commandline iirc, so you have to quote arguments with spaces in them (while runcommand does that for you).

Code: Pascal  [Select][+][-]
  1.  ShellExecute(0,nil,PChar('C:\Program Files\LibreOffice\program\swriter.exe'),PChar('"d:\Current Files\test.odt"'),PChar(''),1)=0
  2.  

As a more platform independent way, see https://wiki.freepascal.org/OpenDocument

zeljko

  • Hero Member
  • *****
  • Posts: 1870
    • http://wiki.lazarus.freepascal.org/User:Zeljan
Re: Calling LibreOffice Writer
« Reply #2 on: February 28, 2026, 03:10:27 pm »
I've used this:–
Code: Pascal  [Select][+][-]
  1.   Runcommand('C:\Program Files\LibreOffice\program\swriter.exe',['d:\Current Files\test.odt'],sout);
  2.  
And it works fine, but I think runcommand causes the program to hang.  So, I've converted to ShellExecute:–
Code: Pascal  [Select][+][-]
  1.  if ShellExecute(0,nil,PChar('C:\Program Files\LibreOffice\program\swriter.exe'),PChar('d:\Current Files\test.odt'),PChar(''),1)=0
  2.     then form1.label1.caption:='FAIL.'
  3.     else form1.label1.caption:='OK.';
It works for all programs this application opens (Word, Excel, etc), except for LibreOffice. LibreOffice Writer creates an error: it can't find 'd:\current'.  It's that space in 'Current Files'.  Is there a solution to this?  A way of passing a space without it being a parameter terminator or something?

Why not just LCLIntf.OpenURL() ? It works here (for known/default mimetypes of course).

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12706
  • FPC developer.
Re: Calling LibreOffice Writer
« Reply #3 on: February 28, 2026, 06:03:42 pm »
opendocument passes to url, if it is an uri, and otherwise uses shellexecute.

zeljko

  • Hero Member
  • *****
  • Posts: 1870
    • http://wiki.lazarus.freepascal.org/User:Zeljan
Re: Calling LibreOffice Writer
« Reply #4 on: February 28, 2026, 09:56:05 pm »
Ah, yes, typo :)

Thaddy

  • Hero Member
  • *****
  • Posts: 18764
  • To Europe: simply sell USA bonds: dollar collapses
Re: Calling LibreOffice Writer
« Reply #5 on: March 01, 2026, 12:23:53 pm »
Furthermore, put a path with spaces between double quotes.
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: 205
Re: Calling LibreOffice Writer
« Reply #6 on: March 02, 2026, 10:51:06 pm »
Code: Pascal  [Select][+][-]
  1.  ShellExecute(0,nil,PChar('C:\Program Files\LibreOffice\program\swriter.exe'),PChar('"d:\Current Files\test.odt"'),PChar(''),1)=0
  2.  

That works – thank you.

But, why does 'Current Files' have to be in double quotes to avoid the space issue and 'Program Files' does not?

 

TinyPortal © 2005-2018