XL97: MS Excel Hangs When You Programmatically Save a Workbook

Last reviewed: February 27, 1998
Article ID: Q143452
The information in this article applies to:
  • Microsoft Excel 97 for Windows

SYMPTOMS

When you save a workbook in Microsoft Excel 97, the program may appear to stop responding (hang), and the following symptoms may occur:

  • The screen does not redraw properly.

        -and-
    
  • You are unable to select cells, menus, toolbar buttons, or other controls.

        -and-
    
  • Parts of message boxes and other windows remain on the screen on top of the Microsoft Excel window.

CAUSE

This problem occurs when all of the following conditions are true:

  • You run a Visual Basic for Applications macro that contains a command similar to the following:

          ActiveWorkbook.OnSave = "<Macro name>"
    

    where <Macro name> is the name of a macro.

        -and-
    
  • The macro name you specify in the OnSave command contains the following Visual Basic macro code:

          Application.ScreenUpdating = False
    

        -and-
    
  • The macro you specify in the OnSave command does not reset the ScreenUpdating property to True.

RESOLUTION

To prevent this problem from occurring, add the following code to all macros that you specify in the OnSave command:

   Application.ScreenUpdating = True

before the end of each macro.

NOTE: When this behavior occurs, you must quit Microsoft Excel by pressing ALT+F4.

STATUS

Microsoft has confirmed this to be a problem in the Microsoft products listed at the beginning of this article. We are researching this problem and will post new information here in the Microsoft Knowledge Base as it becomes available.

MORE INFORMATION

Microsoft provides examples of Visual Basic for Applications procedures for illustration only, without warranty either expressed or implied, including, but not limited to the implied warranties of merchantability and/or fitness for a particular purpose. The Visual Basic procedures in this article are provided 'as is' and Microsoft does not guarantee that they can be used in all situations. While Microsoft support engineers can help explain the functionality of a particular macro, they will not modify these examples to provide added functionality, nor will they help you construct macros to meet your specific needs. If you have limited programming experience, you may want to consult one of the Microsoft Solution Providers. Solution Providers offer a wide range of fee-based services, including creating custom macros. For more information about Microsoft Solution Providers, call Microsoft Customer Information Service at (800) 426-9400.

In Microsoft Excel 7.0 and Microsoft Excel 97, you can use the OnSave property to specify a macro that runs after you initiate saving a workbook, and before Microsoft Excel saves the workbook.

Example of the Problem

The following example demonstrates the use of the OnSave property:

   Sub SetOnSaveTrigger()
       ActiveWorkbook.OnSave = "WorkbookSaving"
   End Sub

   Sub WorkbookSaving(Arg As Boolean)
       Application.ScreenUpdating = False
       MsgBox "Now saving file!"
   End Sub

After you run the first macro, Microsoft Excel automatically runs the second macro each time you save the active workbook.

Turning off Screen Updating

You can turn off screen updating while a macro runs by including the following line of code in the macro:

   Application.ScreenUpdating = False

You can enable screen updating by setting the ScreenUpdating property to True. Usually, this step is not necessary: Microsoft Excel automatically enables screen updating when the macro stops running. However, there is one exception to this rule. If a macro that is run by the OnSave property contains a command to turn off screen updating, Microsoft Excel does not enable screen updating automatically when the macro is finished.

When this behavior occurs, Microsoft Excel appears to stop responding (hang). However, the program is not "frozen." The program is not redrawn on the screen correctly, which prevents you from using any part of the program.

Example of the Resolution

To prevent the problem, enable screen updating by resetting the ScreenUpdating property to True before the end of the macro. For example, change the example in the "Example of the Problem" section to the following:

   Sub WorkbookSaving(Arg As Boolean)
      Application.ScreenUpdating = False
      MsgBox "Now saving file!"
      Application.ScreenUpdating = True
   End Sub


Additional query words: XL97 hung freeze frozen locked stopped
Keywords : kbcode kbprg xlloadsave xl97vbmigrate
Version : WINDOWS:97
Platform : WINDOWS


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: February 27, 1998
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.