How to Set Printer Properties with a Macro

Use an OpenOffice Macro to Change Printer Options

© Mark Alexander Bain

Nov 29, 2008
Setting Printer Options with a Calc Macro, Mark Alexander Bain
Rather than waste masses of incorrectly printed paper use a single OpenOffice macro to set up the printer correctly.

Every year there are thousands of sheets of paper wasted because of reports being printed:

  • on A3 paper instead of A4
  • portrait instead of landscape
  • from the printer tray with company stationary instead of plain paper
  • no header
  • no footer
  • without a page number or page count

Fortunately all of that paper can be saved just by using OpenOffice.org Calc and a single macro - a macro that can:

  • set the page size
  • set the page orientation
  • set the printer tray
  • add a header
  • add a footer
  • add the page numbers and a page count

Setting the Page Size

The page size can be set very easily by:

  • obtaining the page style
  • setting the page width (in millimeters)
  • setting the page height (in millimeters)

Sub FormatPrinter

pageStyles = thisComponent.StyleFamilies.getByName("PageStyles")

page = pageStyles.getByName("Default")

page.width = 21000
page.height = 29700

Setting the Page Orientation

The page orientation is handled a little differently to the page size - for that an OpenOffice property value for the printer must be set:

Dim printerOption(0) As New com.sun.star.beans.PropertyValue
printerOption(0).Name = "PaperOrientation"
printerOption(0).Value = com.sun.star.view.PaperOrientation.LANDSCAPE
thisComponent.Printer = printerOption()

Choosing the paper tray

Changing the paper tray is very simple:

page.PrinterPaperTray = "Tray Two"

It's worth noting that an error will occur at this point if the wrong printer tray name is entered.

Adding a Header

Adding the header is the most complicated part of the process seen so far, but is by no means difficult - the macro must:

  • access the page header content
  • update the left, center or right panels
  • refresh the page header

page.HeaderIsOn = True

header = page.RightPageHeaderContent

header.LeftText.String = Date()

header.CenterText.String = "My Report"

header.RightText.String = "Author: Mark Alexander Bain"

page.RightPageHeaderContent = header

Adding a Footer

The footer is handled in exactly the way as the header:

page.FooterIsOn = True
footer = page.RightPageFooterContent
footer.CenterText.String = "~*~ OpenOffice Secrets ~*~"
page.RightPageFooterContent = footer

Adding a Page Number and Page Count

The page numbering and page count functionality is, of course, built into OpenOffice - the only difficult thing is knowing how to access it and, most importantly, how to update the header or the footer with the information:

pageNumber = _
thisComponent.createInstance ("com.sun.star.text.TextField.PageNumber")
textCursor = footer.RightText.createTextCursor
textCursor.gotoEnd (False)
textCursor.String = "Page "
textCursor.gotoEnd (False)
footer.RightText.insertTextContent (textCursor, pageNumber, True)
pageCount = _
thisComponent.createInstance ("com.sun.star.text.TextField.PageCount")
textCursor.gotoEnd (False)
textCursor.String = " of "
textCursor.gotoEnd (False)
footer.RightText.insertTextContent (textCursor, pageCount, True)
page.RightPageFooterContent = footer
End Sub

Summary

Formatting printer outputs by using a macro in OpenOffice Calc is only difficult because a few different techniques have to be used:

  • the page style is used to change the:
    • page width
    • page height
    • paper tray
  • a printer property must change for the paper orientation
  • text cursors are needed to add page numbers and page counts

However, combining them into a single macro means that a document can always be printed correctly - with no waste of paper.


The copyright of the article How to Set Printer Properties with a Macro in Computer Programming Tutorials is owned by Mark Alexander Bain. Permission to republish How to Set Printer Properties with a Macro in print or online must be granted by the author in writing.


Setting Printer Options with a Calc Macro, Mark Alexander Bain
An Automatically Formatted Print Page View, 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