Set Excel 2008 default zoom level via AppleScript

May 30, '08 07:30:03AM

Contributed by: kioarthurdane

I had a client ask how to set the zoom for new and existing documents to something larger than 100% in Excel 2008 because he finds it too difficult to read at the default size. While my vision may be a tad better, I still find the Mac version of Excel to be on the small side. Sure, the preferences allow for the setting of a default font and size for new documents, but this will alter printouts and returns us to our problem. The "simple" answer I came up with is an AppleScript "assistant" for zooming that runs in the background and quits when it senses Excel is not running.

You can also drop files on it as a replacement for the Excel icon in your Dock! You can either download the script [24KB], or read the rest of this hint to view the source.

[robg adds: I've also provided an alternative solution that's not nearly as elegant, but which works for me, in the remainder of the hint.]

Here's the code...

(*
This code was originally written by Mike Cramer on 2008-05-29.  Email me at kiodane at [Google's mail service] dot com
*)

property zoomFactor : ""
property prevWinNameList : {}

--------------------------------------------

on open theseFiles
  tell application "Microsoft Excel" to open theseFiles
  initializeZoomFactor()
end open

--------------------------------------------

on run
  initializeZoomFactor()
end run

--------------------------------------------

on idle
  
  --Find out if Excel is still running.
  tell application "System Events" to set ExcelIsRunning to (name of processes) contains "Microsoft Excel"
  
  if ExcelIsRunning then
    try
      tell application "Microsoft Excel"
        
        --Get the currently open windows.
        set currWinNameList to name of every window
        set currWinList to every window
        
        repeat with anWindow in currWinList --i from 1 to (count of currWinList)
          
          --Compare current windows to the last check  
          if prevWinNameList contains name of anWindow then
          else
            tell anWindow to set zoom to zoomFactor
          end if
          
        end repeat
        
        --The current windows are next check's old windows.        
        set prevWinNameList to currWinNameList
        
      end tell
    end try
  else
    --When Excel quits, the assistant should quit too.
    quit
  end if
  
  --repeat every 5 seconds.
  return 5
end idle

--------------------------------------------

on initializeZoomFactor()
  
  --Make sure Excel is running to begin with.
  tell application "Microsoft Excel" to activate
  
  set errMsg to ""
  set output to 0
  repeat while (output < 25 or output > 400) --The range of Excel's zoom is 25-400%
    
    activate
    
    display dialog errMsg & "Please enter a Zoom Factor to use with all documents in Excel (25-400):" default answer zoomFactor buttons {"Okay", "Nevermind"} default button 1
    copy the result as list to {text_returned, button_pressed}
    
    
    if button_pressed = "Okay" then
      
      --Verify the entered text can be converted to a number.
      set output to 0
      try
        set output to text_returned as number
      end try
      
      --Check the number is within range
      if (output ≥ 25 and output ≤ 400) then
        set zoomFactor to output as number
      else
        set errMsg to "There was an error with your last input.rr"
      end if
    else
      quit
    end if
  end repeat
  
  --For some reason Excel likes to hide at a previous step sometimes.  This line fixes that:
  tell application "Microsoft Excel" to activate
  
end initializeZoomFactor
[robg adds: I searched the net for a simpler way to make all documents appear at a higher zoom level, but didn't find an easy answer. You can modify the Normal template, I believe, to force new documents to appear at your chosen zoom level, but that won't affect existing documents. You can, of course, use the mouse to zoom the sheet -- Control-Command and scroll wheel in 10.5 will increase or decrease the zoom level. The control isn't all that precise, though, and you have to scroll very slowly to find the exact zoom level you want. As an alternative, you can create keyboard shortcuts for zoom in and zoom out, which is what I've done.

Go to Views » Customize Toolbars and Menus, then click on the Commands tab. Click on All Commands in the left-side box, then scroll down and find Zoom In in the right-hand box.

Now drag Zoom In from that dialog up to the small representation of the main menu that appears just below the actual menu bar. Drag and drop it onto a menu where you'll be able to find it -- I placed it at the bottom of the View menu. Click OK to close the dialog in Excel, then open the Keyboard Shortcuts tab of the Keyboard & Mouse System Preferences panel. Click the plus sign to add a new shortcut, set Excel 2008 as the application and Zoom In as the menu title. Enter the shortcut you'd like to use (Control-Option-plus or whatever), and (if you're running 10.5) switch back to Excel 2008 and try your new shortcut. (For previous versions of OS X, you'll need to quit and relaunch Excel to see the new shortcut.)

Repeat the above steps for Zoom Out (Control-Option-minus), and you've got handy shortcuts to precisely set the zoom levels. While this method isn't as elegant as the above AppleScript, it works pretty well for my needs.]

Comments (7)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20080530061550929