Printing
Printing is done with the print out command. Normally this is applied to a sheet, as follows:
ActiveSheet.PrintOut
print out active sheet
Make sure that you don't leave it at plain print. That will print, in a manner of speaking, because it's the obligatory print command in the Standard Suite. But what it does is bring up the standard Print dialog in the UI and wait for you to click OK. It also misses the possibility of using all of the optional parameters for the print out command that give you a lot of control.
For example, you can specify the number of copies and the page range. Leaving out the to parameter prints the remainder of the pages to the end, just like in the UI.
ActiveSheet.PrintOut From:=2, To:=2, Copies:=4
print out active sheet from 2 to 2 copies 4
If your sheets are grouped, you can print them all by applying print out to the workbook or active window. In Visual Basic for Applications (VBA), those are specified as separate methods.
ActiveWorkbook.PrintOut
print out active workbook
By default, if the header or footer has page numbers, this will cause the page numbers to print continuously from sheet to sheet. This can be changed in preferences.
There's an error in the AppleScript dictionary where sheet / window / workbook are listed as constants of the direct parameter printout options. But this is inaccurate, because you don't print out a sheet constant, you print out an actual sheet object. It's the type (class) sheet that should be listed there, along with type window and type workbook, not an enumeration. The dictionary is trying to let you know which classes it can be applied to, as it does in descriptions.
AppleScript dictionaries did not used to permit multiple classes to be listed. That's why in older dictionaries, you will often see the type marked as
reference, meaning "some application object", or even as
anything
if one of the allowable types is a basic AppleScript type such as string or number. ("Anything" is used as a catch-all type that permits objects of appropriate classes to be direct objects of the command without erroring.) It does not mean "any application object whatsoever" but just "multiple types of [unspecified] application classes."
In some cases in the Microsoft Excel and Microsoft Word dictionaries, it looks like several different copies of the same command are listed (for example, get border), in several different suites. That permits the dictionary to list one class per suite as the direct parameter. But they're cross-referenced, and it works.
One thing you cannot do in Microsoft Office 2004 is print to PDF by script. The print to file parameter for the print out command appears to do that, but it prints to paper.
Page setup
While Page Setup in VBA requires each parameter to make a separate call to the Print Driver, you can set any parameters that can be set in the GUI dialog.
With ActiveWorkbook.Sheets("Sheet1").PageSetup
.PrintTitleRows = "$1:$2"
ActiveSheet.PageSetup.PrintArea = "" 'determine at print time
.LeftHeader = ""
.CenterHeader = "My Report"
.RightHeader = ""
.LeftFooter = "Page &P of &N"
.CenterFooter = ""
.RightFooter = "&Z&F"
.LeftMargin = Application.InchesToPoints(1)
.RightMargin = Application.InchesToPoints(1)
.TopMargin = Application.InchesToPoints(1.5)
.BottomMargin = Application.InchesToPoints(1.5)
.HeaderMargin = Application.InchesToPoints(0.5)
.FooterMargin = Application.InchesToPoints(0.5)
.PrintHeadings = False
.PrintGridlines = False
.PrintComments = xlPrintNoComments
.PrintQuality = -4
.CenterHorizontally = False
.CenterVertically = False
.Orientation = xlLandscape
.Draft = False
.FirstPageNumber = xlAutomatic
.Order = xlDownThenOver
.BlackAndWhite = False
.Zoom = 100
End WithTo fit pages to a certain height or width, you must set the Zoom property to False.
With ActiveWorkbook.Sheets("Sheet1").PageSetup
.Zoom = False
.FitToPagesWide = 1
.FitToPagesTall = 2
End WithThat translates easily into AppleScript:
tell application "Microsoft Excel" tell page setup object of active sheet set print title rows to "$1:$2" set print area to "" -- determine at print time set left header to "" set center header to "My Report" set right header to "" set left footer to "Page &P of &N" set center footer to "" set right footer to "&Z&F" set left margin to inches to points inches 1.0 set right margin to inches to points inches 1.0 set top margin to inches to points inches 1.5 set bottom margin to inches to points inches 1.5 set header margin to inches to points inches 0.5 set footer margin to inches to points inches 0.5 set printheadings to false set print gridlines to false set print quality to -4 set center horizontally to false set center vertically to false set page orientation to landscape set draft to false set first page number to 1 set order to down then over set black and white to false set zoom to 100 end tell end tell
The only difference of any sort is that AppleScript does not have a documented term for the xlAutomatic constant, so the first page number is set to
1, the usual "automatic." The Long constant in VBA of xlAutomatic is -4105, and that may work if you try it in AppleScript, too.


