Direct print configured cell range

Hello everyone! new here.

I’m trying to write a code to print a pre-configured page… i tryed Chat GPT and it gave me some codes, although, none worked, i need to find out how to print a specific selection, or cell range, directly in a defined printer, i do not want to open the print configuration screen, just wanna click and print, acording to what i want…
I need the code to work on the oldest vertion, only with the native basic language of the libreoffice… where i wanna implement it does not have access to internet, i only have the basic and the libreoffice calc…

Does anybody have any clue of how doing this? it would make a huge diference in the way my spreadsheet will work…


In the cod bellow i receive a error message in the line code printArgs(0).Value.setPropertyValue(“Name”, printerName) that says : that the variable of the object is not defined…

Sub Main
’ Define a impressora pré-definida
Dim printerName As String
printerName = “Nome da Impressora” ’ Substitua pelo nome da sua impressora

' Obter o objeto Document
Dim document As Object
document = ThisComponent

' Obter o objeto Dispatcher
Dim dispatcher As Object
dispatcher = createUnoService("")

' Define as células a serem impressas
Dim args1(0) As New
args1(0).Name = "ToPoint"
args1(0).Value = "$A$1:$E$18"

' Obter o objeto DispatchProvider
Dim dispatchProvider As Object
dispatchProvider = document.CurrentController.Frame

' Executa a ação de seleção de células
dispatcher.executeDispatch(dispatchProvider, ".uno:GoToCell", "", 0, args1())

' Define as opções de impressão
Dim printArgs(0) As New
printArgs(0).Name = "PrintSettings"
printArgs(0).Value = createUnoService("")
printArgs(0).Value.setPropertyValue("Name", printerName)

' Executa a impressão

End Sub

Really could use more specifics here.
What operating system, version of LibreOffice etc.

I would start with some sort of automation software such as AutoHotKey.
In windows you would need to send keys ctl-p, alt-p, alt-p, enter to opend print dialog and use short cut keys manage the dialog.

There are other tools such a s ooo-dev-tools-gui-win but my guess this is overkill for what you are trying to accomplish.

If these kinds of options do not work for you please be more specific.

1 Like

Also: It may be possible with a dispatch command.
I found .uno:PrintOut on Development/DispatchCommands
And a link: 181: The Specifications of the Application Foundations '.uno:PrintOut'|T.B.P.
That explains it a little.

1 Like

Did you ask ChatGPT if she (m/f) knows the results shown when having entered XPrint into the search field of LibreOffice: com Module Reference ? Did he (f/m) read Andrew Pitonyaks texts? The dev guides?


1 Like

i added the code generated by Chat GPT in the question

@Clif4, see this chapter from Mauricio Baeza Servín’s Book…

AprendiendoOOoBasic_baixado_10_06_2020_Imprimiendo.odt (121.0 KB)

What is a “page”? How did you pre-configure a page?

EDIT: attached a spreadsheet document with 3 sheets, 2 user-defined properties, a toolbar button and a macro triggered by the toolbar button. The macro determines the used area of the currently active sheet and prints this area to the specified printer or specified file.
Call menu:File>Properties, tab “Custom Properties” and set either property “PrinterName” or “PrintToFile” (e.g. C:\path\name.pdf) in order to specify the output device. If both properties are empty or none of them exists, the macro prints to the default printer.
print_used_range.ods (14.8 KB)

@Villeroy Great! it did work. i would like to know if its even possible to print a page that i am no currently in, for example: i am in the sheet number one and i wanna print sheet’s number two content.
using the sabe logic u did.


@schiavinatto great info in this doc, i will reserv a time to study it.
Thankyou for the material :smile:

1 Like

Added another user defined property “SheetNum” (number) where you specify the sheet index. If that property does not exist or if it is smaller than 1, the active sheet will be printed.
print_used_range2.ods (15.4 KB)

P.S. notice that a sheet’s used area may include many print pages (sheets of paper). In order to print only the first page, add a property value “Pages” to array aOptions with text value “1”.

1 Like

@Villeroy i tried replacing the “PrintSheet” for a number to represent the number of the sheet i wanted to print, but when doing this the code started printing the page i was at, diferently when i maintain the name “PrintSheet”, doing this the code sends me to the sheet2 and print it. Const cSheetNum = “PrintSheet”

i think i did any wrong but dont know what, in add, i want it to print the sheet i want without the need of going to that

Do not touch the code! I use three user defined properties as a user interface for macro configuration.
Call menu:File>Properties… tab “Custom Properties”.
I defined 2 text properties “PrinterName”, “PrintToFile” and one numeric property “PrintSheet”.
Since the 2 text properties are empty, the sample file prints to your default printer queue. Define a printer name in order to print to a different printer. Clear or delete the printer name and define a file name in order to print a pdf file. If both are given, the printer name has a higher precedence.
I set “PrintSheet” to 2, so you get the second sheet printed when you print the downloaded file as is.
The constants on top of the code do not define the names nor the sheet index. They define the names of the user defined properties to read from.

In order to use my code with your document(s), copy the code to some library under “My Macro” and add the required custom properties to your document(s). Call the macro any way you want.

1 Like

I found this thread intresting.
I added Calc.print_sheet() to the OOO Development Tools (OooDev) project that covers the ideas presented here.

worked perfectly! Thankyou :smile:

I think that a lot can be done with this language…
Will keep studying it for sure.

Thankyou everyone for the material and info!

You may be interested in as well.