Ask Your Question

Basic: printing a document not the active one

asked 2017-11-30 04:33:54 +0100

owlbrudder gravatar image

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 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("")

    '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 flag offensive close merge delete

3 Answers

Sort by » oldest newest most voted

answered 2017-11-30 07:13:52 +0100

Ratslinger gravatar image


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.

edit flag offensive delete link more


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

Ratslinger gravatar imageRatslinger ( 2017-11-30 07:18:25 +0100 )edit


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

Ratslinger gravatar imageRatslinger ( 2017-11-30 16:24:12 +0100 )edit

answered 2017-11-30 20:10:13 +0100

Jim K gravatar image

updated 2017-11-30 20:15:12 +0100

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

Sub PrintDocB
    oDocA = ThisComponent
    oDocB = StarDesktop.LoadComponentFromUrl(_
            "file:///home/YOUR_DIR/LabelTest.odt", "_blank", 0, Array())
    xPrintable = oDocB
    Dim printerDesc(0) As New
    printerDesc(0).Name = "Name"
    printerDesc(0).Value = "5D PDF Creator"
    Dim printOpts(0) As New
    printOpts(0).Name = "Pages"
    printOpts(0).Value = "1"
End Sub

This code was adapted from

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()
edit flag offensive delete link more

answered 2017-11-30 10:46:09 +0100

owlbrudder gravatar image

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

edit flag offensive delete link more


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

Jim K gravatar imageJim K ( 2017-11-30 19:55:40 +0100 )edit
Login/Signup to Answer

Question Tools

1 follower


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

Seen: 40 times

Last updated: Nov 30 '17