Clumsy code for Excel

I use the “page setup” object to determine print instructions but I cannot get a couple of instructions to work and have a clumsy workaround. Wondered if anyone had any idea on what I am doing incorrectly.

This code works.

tell application "System Events"
    tell process "Excel"
        click menu item "Page Setup..." of menu 1 of menu bar item "File" of menu bar 1
        tell application "Microsoft Excel" to activate
        click radio button "Landscape" of tab group 1 of window "Page Setup"
        click radio button "Fit to:" of radio group 1 of tab group 1 of window "Page Setup"
        set value of text field 2 of tab group 1 of window "Page Setup" to "1"
        set value of text field 3 of tab group 1 of window "Page Setup" to "99"
        click button "OK" of window "Page Setup"
    end tell
end tell
[/AppleScript]

This changes the orientation to landscape but does not change the the pages to wide or high.


tell application “Microsoft Excel”
activate
tell page setup object of active sheet of active workbook
set page orientation to landscape
set fit to pages wide to 1
set fit to pages tall to 99
end tell
end tell
[/AppleScript]

Thanks Peter

You need to set the zoom property to tell Excel that you want to use the two fit to pages properties.


tell application "Microsoft Excel"
	activate
	tell page setup object of active sheet of active workbook
		set page orientation to landscape
		set zoom to false
		set fit to pages wide to 1
		set fit to pages tall to 99
	end tell
end tell

Thanks so simple I had deleted zoom instead of setting it to false, it takes a while for a slow learner.

Forgive me for asking another question same subject different part. Can I set the print area to used range . Sometimes I have to print a subset of the used range and then the used range. What happens is that having set the area to print it remains. At present the only way I can find of clearing the previous section is

[AppleScript]
[tell application “System Events”
tell process “Excel”
click menu item “Clear Print Area” of menu 1 of menu item “Print Area” of menu 1 of menu bar item “File” of menu bar 1
end tell
end tell
[/AppleScript]

Peter

Sorry too quick “set print area to “”” does the job under page object setup.

However if I may can I set the rows and columns to be repeated on page object setup?

Thank you

You can do it like this:


tell application "Microsoft Excel"
	
	tell page setup object of active sheet of active workbook
		
		set print title rows to "$A$1:$Q$1" -- set to "" to clear
		set print title columns to "$A:$A" -- set to "" to clear
		
	end tell
	
end tell

Hi MitchBVI.

Don’t forget that MacScripter’s [applescript] and [/applescript] tags are lower case. You can see that mixed case doesn’t work when you read your own posts.

https://macscripter.net/viewtopic.php?pid=205231#p205231

Thank you both very much.