[Solved] Cannot print from a macro

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
Radagast
Posts: 2
Joined: Sat Oct 17, 2009 7:34 pm

[Solved] Cannot print from a macro

Post by Radagast »

I am new to macros in OpenOffice.
I have tried to learn something about macros by using the "Record Macro" facility to save some keystrokes or mouse activity and then look at the code to see how it does it. This seems to work for everything I have tried except printing, and I need a macro that will automate some printing.
Imagine I recorded the following:
  • simply select a range of cells,
    click File->Print,
    tick the radio button "selected cells"
    press OK.
and then cease recording and save the macro with the name prt.
If I then try to replay the macro, it selects the correct cells but then does nothing. It does not even present the Print dialogue box. Why not? The code, if it helps, that is saved is shown below.

Code: Select all

sub prt
rem ----------------------------------------------------------------------
rem define variables
dim document   as object
dim dispatcher as object
rem ----------------------------------------------------------------------
rem get access to the document
document   = ThisComponent.CurrentController.Frame
dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")

rem ----------------------------------------------------------------------
dim args1(1) as new com.sun.star.beans.PropertyValue
args1(0).Name = "Copies"
args1(0).Value = 1
args1(1).Name = "Collate"
args1(1).Value = true

dispatcher.executeDispatch(document, ".uno:Print", "", 0, args1())


end sub
I would be very grateful for any illumination on this problem.

Thanks

Radagast The Brown
Last edited by Radagast on Tue Oct 20, 2009 10:01 pm, edited 1 time in total.
OOo 3.1.1
Windows XP
FJCC
Moderator
Posts: 9280
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: Cannot print from a macro

Post by FJCC »

Learning about writing macros using the record function is not a good way to go. Code from the recorder doesn't look anything like code written by people using the API. Here is some code I cribbed from Andrew Pitonyak's Macro Document, which is a good place to start learning. I haven't fully tested the code, since I don't have access to a printer, but it does set the print area to the specified range on Sheet1

Code: Select all

Dim PrintArea(0) as New com.sun.star.table.CellRangeAddress 'Define an array to contain cell ranges addresses to print
oDoc = ThisComponent
Sheets = oDoc.Sheets 'get the collection of sheets
SheetCount = Sheets.Count  'get the count of sheets
For i = 0 to SheetCount - 1  'This loop removes any existing print areas
	oSheet = Sheets.getByIndex(i)
	oSheet.setPrintAreas(Array())  'The Array() is an empty array. setPrintAreas expects an array of parameters, even an empty array.
Next i

PrintSheet = Sheets.getByName("Sheet1")  'get the sheet that has the range you want to print
Cellrange = PrintSheet.getCellRangeByName("B2:C6")  'get the desired range
PrintArea(0) = Cellrange.RangeAddress  'set the first element of the array to be the RangeAddress of the range to print
PrintSheet.setPrintAreas(PrintArea())  
oDoc.Print(Array())
OpenOffice 4.1 on Windows 10 and Linux Mint
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
Radagast
Posts: 2
Joined: Sat Oct 17, 2009 7:34 pm

Re: [solved] Cannot print from a macro

Post by Radagast »

Thanks, FJCC. That seems to print OK. Now I just need to modify the code you supplied to fit my own purpose.

Radagast
OOo 3.1.1
Windows XP
Post Reply