Change Printer with UNO API

Creating a macro - Writing a Script - Using the API

Change Printer with UNO API

Postby oliveiratr » Fri Jul 21, 2017 7:40 pm

Hello all

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

My code is :

Code: Select all   Expand viewCollapse view
Function printMyDoc(aPrinterName)
   Dim oPropertyValue(0) AS New
   Dim oDoc
   Dim oSettings
   Dim oPrinterSettings(0) As New

   oDoc = ThisComponent

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

   oPropertyValue(0).Name = "CopyCount"
   oPropertyValue(0).Value = 1
End Function

But, when pass here :
Code: Select all   Expand viewCollapse view
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
Posts: 30
Joined: Thu Dec 29, 2016 12:22 pm

Re: Change Printer with UNO API

Postby UnklDonald418 » Mon Jul 24, 2017 12:17 am

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   Expand viewCollapse view
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.5 & LibreOffice - Windows 10 Professional
Posts: 852
Joined: Wed Jun 24, 2015 12:56 am
Location: Colorado, USA

Return to Macros and UNO API

Who is online

Users browsing this forum: No registered users and 3 guests