Change Printer with UNO API

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
oliveiratr
Posts: 30
Joined: Thu Dec 29, 2016 12:22 pm

Change Printer with UNO API

Post by oliveiratr »

Hello all

I trying change Printer before send print command to my OO Doc.

My code is :

Code: Select all

Function printMyDoc(aPrinterName)
   Dim oPropertyValue(0) AS New com.sun.star.beans.PropertyValue
   Dim oDoc
   Dim oSettings
   Dim oPrinterSettings(0) As New com.sun.star.beans.PropertyValue

   oDoc = ThisComponent

   oPrinterSettings(0).Name = "Name"
   oPrinterSettings(0).Value = aPrinterName
   oDoc.Printer = oPrinterSettings

   oPropertyValue(0).Name = "CopyCount"
   oPropertyValue(0).Value = 1
   oDoc.Print(oPropertyValue())
End Function
But, when pass here :

Code: Select all

 oPrinterSettings(0).Name = "Name"
   oPrinterSettings(0).Value = aPrinterName
   oDoc.Printer = oPrinterSettings
Do not anything :(

Anybody knows whats i did wrong? :knock:
OpenOffice 4.1.2
UnklDonald418
Volunteer
Posts: 1546
Joined: Wed Jun 24, 2015 12:56 am
Location: Colorado, USA

Re: Change Printer with UNO API

Post by UnklDonald418 »

You didn't specify what operating system you are using and you also didn't specify how you were running the macro. I tried your code using Windows 10 and got some interesting results. It seems to matter how you run the macro.
I added the following to call your function

Code: Select all

Sub TestPrint
   printMyDoc("")  ' these are my printers ("Brother HL-2170W series Printer")  ("Microsoft Print to PDF")  ("HP ENVY 4520 series")
End Sub
When I use Tools->Macros->Run Macro the code works perfectly. If I pass your code “” as the printer name it prints to the default printer. Likewise if I misspell the printer name.

However, if I use <Alt>F11->Run nothing happens! Likewise if I use Tools->Macros->Organize Macros->Run or I use the Run button in the IDE.
I suppose when using these methods it doesn't know what to print.
If your problem has been solved, please edit this topic's initial post and add "[Solved]" to the beginning of the subject line
Apache OpenOffice 4.1.14 & LibreOffice 7.6.2.1 (x86_64) - Windows 10 Professional- Windows 11
Post Reply