MXL5: AppleScript Close Method Does Not Ask to Save Changes

ID: Q128748

The information in this article applies to:

SYMPTOMS

When you use AppleScript to control the versions of Microsoft Excel listed above, and you use a command that closes an unsaved document, Microsoft Excel does not display a message asking if changes should be saved.

This situation occurs regardless of whether you use the "close" command specified in the Microsoft Excel AppleScript Dictionary or the Microsoft Excel 4.0 macro language CLOSE() command as an argument to the "Evaluate" command.

NOTE: This problem does NOT occur in Microsoft Excel 98 Macintosh Edition.

CAUSE

This problem occurs because of a limitation in the interface between the versions of Microsoft Excel listed at the beginning of this article and AppleScript.

In the two AppleScript examples below, a command is sent to Microsoft Excel instructing it to close the Active Workbook document. The first example, which specifies the "saving" parameter, will cause the document to be saved without prompting.

NOTE: If the document has not been previously saved, the standard Save dialog box will appear, requesting a filename and folder location for the saved file. Also, note that when you use the syntax in example 1, if you use the "saving" parameter followed by any valid value (such as the words "yes," "no," "ask," or the values 0, 1, 2 and so on) Microsoft Excel will attempt to save the document.

The examples of AppleScript code in this article are provided for illustration only. This AppleScript code is provided 'as is' and Microsoft does not guarantee that it can be used in all situations. Microsoft does not support modifications of this code to suit customer requirements for a particular purpose. AppleScript is manufactured by Apple Computer, Inc., a vendor independent of Microsoft; we make no warranty, implied or otherwise, regarding this product's performance or reliability.

Example 1

   tell application "Microsoft Excel"
      Activate
      close ActiveWorkbook saving ask
   end tell

Example 2

   tell application "Microsoft Excel"
      Activate
      Evaluate "close()"
   end tell

WORKAROUND

If you want Microsoft Excel to prompt you to save changes when you close a document, you can include a short AppleScript routine that mimics this functionality. This workaround uses the "Display Dialog" scripting addition, which is available from Apple Computers, Incorporated.

The Display Dialog file must be in the following location:

   System Folder:Extensions:Scripting Additions

AppleScript Sample Workaround

   tell application "Microsoft Excel"
      Activate
      if (Saved of ActiveWorkbook) then
         Close ActiveWorkbook
      else
         display dialog "Document not saved. Save it now?"
   buttons {"Yes", "No", "Cancel "} default button 1
         if button returned of result is "Yes" then
            Close ActiveWorkbook saving yes
         else if button returned of result is "No" then
            Close ActiveWorkbook
         else
            display dialog "You canceled." buttons "OK"
   default button 1
         end if
      end if
   end tell

NOTE: The text string "Cancel " (as used above) contains a trailing space. Because Display Dialog generates an error condition that causes a script to terminate when a button named "Cancel" (no trailing space) is pressed, the addition of the space prevents this error condition.

STATUS

Microsoft has confirmed this to be a problem in the versions of Microsoft Excel listed at the beginning of this article. This problem was corrected in Microsoft Excel 98 Macintosh Edition.

Additional query words: 5.00 5.00a XL5 apple script close saving ask prompt

Keywords          : kb3rdparty xlmac 
Version           : MACINTOSH:5.0
Platform          : MACINTOSH

Last Reviewed: February 5, 1998