OpenOffice Macros: Printing a Document

How to Print OpenOffice.org Documents by Using a Macro

© Mark Alexander Bain

Aug 12, 2008
Printing an OpenOffice Document from a Macro, Mark Alexander Bain
One of the most fundamental thing that any OpenOffice user will want to do is to print a document. This turorial shows exactly how to do this - automatically from a macro

Anyone writing OpenOffice.org Basic Macros will need to print documents - automatically; and when they do that they might well expect to use a simple print statement. They, of course, won't be disappointed because that's exactly what they will find. Unfortunately life (and OpenOffice Basic) is never quite as simple as that (and if it was then this would be a very short article)

Printing an OpenOffice Document from a Macro

Printing an OpenOffice.org document from a macro is simple - as long as the print function is handled correctly - and that means supplying the correct parameters; so a macro to print a document would be:

Sub PrintMe

thisComponent.Print (Array ())

End Sub

Anyone new to creating macros may wonder what this means, but it's quite simple:

  • thisComponent - the current document
  • Print - the print method for thisComponent
  • Array - this is just an empty array

And the result of running this macro? The current document is sent directly to the default printer.

Adding Printer Parameters to a Macro

In the above example an empty array is passed to the document's print method, causing the document to be sent directly to the printer; and it won't take long for any macro developer to work out that this array is the means by which they can send parameters to the printer - parameters to modify the way in which the document is to be printed, such as:

  • the number of copies to be printed
  • which pages to print
  • a filename to send the document to (rather than a printer)
  • whether multiple documents are to be collated or not

So then it's just a matter of knowing what the parameters are and how to add them to the array.

Printing More Than One Copy of a Document

The best way for a new user to understand how to add a printer parameter is to see an actual example, in this case how to print more than one copy:

Sub PrintMe

Dim PrintProperty(0) as New com.sun.star.beans.PropertyValue

PrintProperty(0).Name = "CopyCount"

PrintProperty(0).Value = 2

thisComponent.Print (PrintProperty)

End Sub

This time the code is used to:

  • define an array of data type PropertyValue - each element consisting of a "Name" and a "Value"
  • load the array with the parameters needed to print 2 copies of the document
  • pass the array to the Print method

From this it's obvious how the parameters should be manipulated:

  • create an array to contain the parameters
  • load the array with the required parameters
  • feed the array containing the parameters to the method

And all of this can be made clearer by looking at another example.

Printing Defined Pages

If more than one parameter is to be used then the size of the array must be increased accordingly:

Dim PrintProperty(1) as New com.sun.star.beans.PropertyValue

PrintProperty(0).Name = "CopyCount"

PrintProperty(0).Value = 2

PrintProperty(1).Name = "Pages"

PrintProperty(1).Value = "1;3-5;9"

thisComponent.Print (PrintProperty)

In this example,

  • the size of the array has been increased to 2 (0 and 1)
  • the additional parameter's name has been defined as Pages
  • the parameter's value has been set to "1;3-5;9" meaning that pages 1, 9 and 3 through to 5 will be printed

Printing to a File

The default when printing is to print to a printer (no surprise there then), however it is possible to print directly to a file:

  • set the name to "FileName"
  • set the value to the name of the file to use as an output

Collating the Pages to be Printed

If multiple copies need to be printed and collated then:

  • set the parameter name to "Collate"
  • set the value to True

Conclusion

The parameters available to OpenOffice.org's print method are not extensive, but that means that is very simple to use; and a user will find it easy to access its limited number of parameters:

  • Collate
  • CopyCount
  • Filename
  • Pages

Most imporantly, a user is able to print an OpenOffice document automatically - just by running a simple macro.


The copyright of the article OpenOffice Macros: Printing a Document in Office/Business Software is owned by Mark Alexander Bain. Permission to republish OpenOffice Macros: Printing a Document in print or online must be granted by the author in writing.


Printing an OpenOffice Document from a Macro, Mark Alexander Bain
       


Post this Article to facebook Add this Article to del.icio.us! Digg this Article furl this Article Add this Article to Reddit Add this Article to Technorati Add this Article to Newsvine Add this Article to Windows Live Add this Article to Yahoo Add this Article to StumbleUpon Add this Article to BlinkLists Add this Article to Spurl Add this Article to Google Add this Article to Ask Add this Article to Squidoo

Comments
Nov 28, 2008 1:35 PM
Guest :
What I almost always want to do is either print on blank paper or on our special company paper, i.e. I want to be able to choose the printer's paper tray.
Which is not discussed, and hard to find anything about.
1 Comment: