[Solved] Print merged documents on specific printer via macro

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
StefanGT
Posts: 4
Joined: Fri Mar 03, 2023 7:57 am

[Solved] Print merged documents on specific printer via macro

Post by StefanGT »

Hi there,
I am quite new t macro programming...so I hope to get some code snippets here :-)

The task is as follows:
I need to run LibreOffice in headless mode as part of an automation process. When opening the template file (with a related database) I want to start a macro that prints all merged documents to a specific printer (which is not the default one!) without any user interaction (as LibreOffice runs in headless mode). Can anyone provide me a codesnsippet for the macro?

Thanks in advance!

Regards,
Stefan
Last edited by MrProgrammer on Fri Mar 24, 2023 4:34 am, edited 1 time in total.
Reason: Tagged ✓ [Solved] "I checked now that link and it worked" -- MrProgrammer, forum moderator
LibreOffice 7.4.5 on Windows 10
User avatar
robleyd
Moderator
Posts: 5086
Joined: Mon Aug 19, 2013 3:47 am
Location: Murbko, Australia

Re: Print merged documents on a specific printer via macro

Post by robleyd »

You should download the excellent OpenOffice.org Macros Explained by Andrew Pitonyak; sections 13.14 and 13.15 might provide you with some useful information.

You might also find the object inspection tool MRI useful - see [Tutorial] Introduction into object inspection with MRI for more information.
Cheers
David
OS - Slackware 15 64 bit
Apache OpenOffice 4.1.15
LibreOffice 24.2.2.2; SlackBuild for 24.2.2 by Eric Hameleers
StefanGT
Posts: 4
Joined: Fri Mar 03, 2023 7:57 am

Re: Print merged documents on a specific printer via macro

Post by StefanGT »

Hi Davaid, I know that document and I managed to print a single merged document on a specific printer. But when I use the "MailMergePrintDocuments"-Command I still get a window to select the documents to print... Within this window there is the correct printer selcted but I still have to click the "Print Documents"-Button... (see attachment) :(

This is how my code looks like:

Code: Select all

sub PrintAll
rem ----------------------------------------------------------------------
rem define variables
dim document   as object
dim dispatcher as object
rem ----------------------------------------------------------------------
rem get access to the document
document   = ThisComponent.CurrentController.Frame
dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")


rem ---- choose a certain printer -----------------------------------
DIM mPrinterOpts(1) AS NEW com.sun.star.beans.PropertyValue
mPrinterOpts(0).Name = "Name"
mPrinterOpts(0).Value = "DirectPrint"
ThisComponent.Printer = mPrinterOpts()

REM Set printOptions
DIM mPrintOpts(1) AS NEW com.sun.star.beans.PropertyValue
mPrintOpts(0).Name = "CopyCount"
mPrintOpts(0).Value = 1

dispatcher.executeDispatch(document, ".uno:MailMergePrintDocuments", "", 0, mPrintOpts())
end sub

After all documents have been printed I want to close the application. But that's another problem... First I need to get all the documents printed without user interaction as LibreOffie will run in headless mode...

Can anyone help me :)
Attachments
2023-03-04_153151.jpg
2023-03-04_153151.jpg (42.02 KiB) Viewed 1191 times
LibreOffice 7.4.5 on Windows 10
JeJe
Volunteer
Posts: 2784
Joined: Wed Mar 09, 2016 2:40 pm

Re: Print merged documents on a specific printer via macro

Post by JeJe »

Have you tried CreateUnoService("com.sun.star.text.MailMerge")

https://ask.libreoffice.org/t/can-libre ... ge/16900/4
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
StefanGT
Posts: 4
Joined: Fri Mar 03, 2023 7:57 am

Re: Print merged documents on a specific printer via macro

Post by StefanGT »

No, I haven't tried but it looks promising. WIll check it tomorrow. The challenge again is to set up a specific printer and print the files to that printer instead saving as a file... Will have a look...
LibreOffice 7.4.5 on Windows 10
StefanGT
Posts: 4
Joined: Fri Mar 03, 2023 7:57 am

Re: Print merged documents on a specific printer via macro

Post by StefanGT »

JeJe wrote: Sat Mar 04, 2023 5:50 pm Have you tried CreateUnoService("com.sun.star.text.MailMerge")

https://ask.libreoffice.org/t/can-libre ... ge/16900/4
I checked now that link and it worked :super: But there are two questions left:
- before starting the MailMerge I want to count the records of the connected database? How can I do that?
- With "oMailMerge.FileNamePrefix" I can set the filenameprefix from the values of a database column. Can I also combine two coloms here, for example name and surname?

Many thanks in advance :D
LibreOffice 7.4.5 on Windows 10
Post Reply