# Basic: printing a document not the active one

I am trying to get LO Basic to print a document it creates at run-time. As far as I know, there is no way to do this with a document object, so I am searching for another approach.

Thanks to Dr. Google, I have found two ideas I am trying to stitch together. One is the com.sun.star.system.SystemShellExecute service and one is calling openoffice from the command line with the '-pt' option. Here is wehre I have arrived so far:

Sub subThinkingAboutPrinting
Dim oSvc as Object
Dim strCommand As String
oSvc = createUnoService("com.sun.star.system.SystemShellExecute")

'openoffice -pt "PRINTER-NAME" FILENAME
strCommand = "/usr/bin/libreoffice -pt OfficeJet_6950 " & "/home/doug/ABPA/ABPAsoftware/MembershipAccounting/ABPA_Member_Payment_Receipt.odt"
oSvc.execute(strCommand, "", 0)
End Sub


This looks impressive and a solution to my problem. Unfortunately, it doesn't work!

If anyone can suggest a workable method of printing document A via a macro in document B, I would be very grateful.

Kind regards, Doug

edit retag close merge delete

Sort by » oldest newest most voted

Thanks Ratslinger. I didn't think of 'Shell' and I had only used a single - instead of -- : now it all works fine. Muchly appreciated. Cheers,Doug

more

This should be a comment on the answer, not a separate answer.

( 2017-11-30 19:55:40 +0200 )edit

To print document B when ThisComponent is document A, use the component for document B instead.

Sub PrintDocB
oDocA = ThisComponent
"file:///home/YOUR_DIR/LabelTest.odt", "_blank", 0, Array())
xPrintable = oDocB
Dim printerDesc(0) As New com.sun.star.beans.PropertyValue
printerDesc(0).Name = "Name"
printerDesc(0).Value = "5D PDF Creator"
xPrintable.setPrinter(printerDesc)
Dim printOpts(0) As New com.sun.star.beans.PropertyValue
printOpts(0).Name = "Pages"
printOpts(0).Value = "1"
xPrintable.print(printOpts)
End Sub


This code was adapted from https://wiki.openoffice.org/wiki/Docu....

If document B is already open but you do not have a reference to the component, get it like this.

oComponents = desktop.getComponents()
oDocs = oComponents.createEnumeration()
while oDocs.hasMoreElements()
oDoc = oDocs.nextElement()

more

Hello,

Just ran some tests Using Mint 18 & LO v5.4.3.2. Only needed one line of code. Here are three different tests:

Prints to a specific printer

Shell "libreoffice --pt MG2500-series /home/YOUR_DIR/LabelTest.odt"


Prints to default printer

Shell "libreoffice -p /home/YOUR_DIR/LabelTest.odt"


Prints to print file

Shell "libreoffice --print-to-file --outdir /home/YOUR_DIR  /home/YOUR_DIR/LabelTest.odt"


Pay attention to using - or -- in parameters. Here is a link to the list.

more

BTW - For me it worked using 'soffice' or 'libreoffice'.

( 2017-11-30 07:18:25 +0200 )edit
1

@owlbrudder

If this answers your question please click on the ✔ (upper left area of answer).

( 2017-11-30 16:24:12 +0200 )edit

## Stats

Asked: 2017-11-30 04:33:54 +0200

Seen: 46 times

Last updated: Nov 30 '17