Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

BUG: Microsoft Excel Does Not Repaint Properly with Automation

View products that this article applies to.

This article was previously published under Q187745

↑ Back to the top


When automating Microsoft Excel 97, Excel 2000, or Excel 2002, if the application window has been made visible and the user manually closes it, Excel will not repaint correctly the next time the application window is made visible again.

↑ Back to the top


This problem occurs when a user attempts to quit a running instance of Excel while an Automation client still has a reference to the application object for that instance. By design, Excel does not quit an instance of itself unless all external references are released; if a user tries to quit Excel manually, the application window is merely hidden so that the Automation client may continue working. If, however, the Automation client attempts to make Excel visible again, the application window will not be displayed properly and repainting will not occur.

↑ Back to the top


A workaround is to set the ScreenUpdating property of the application object to True after you have made the window visible. This will force Excel to repaint its client area so that it will be displayed properly.

↑ Back to the top


Microsoft has confirmed this to be a bug in the Microsoft products listed at the beginning of this article.

↑ Back to the top

More information

Steps to Reproduce Behavior

  1. Start a new Standard EXE project in Visual Basic. Form1 is created by default.
  2. On the Project menu, click References, and then select the Microsoft Excel 8.0 object library. For Excel 2000, select Microsoft Excel 9.0 object library, and for Excel 2002, select the Microsoft Excel 10.0 object library.
  3. Place a CommandButton on Form1.
  4. Copy the following code to the Code Window of Form1:
          Private oApp As Excel.Application
          Private Sub Command1_Click()
             oApp.Visible = True
          End Sub
          Private Sub Form_Load()
             Set oApp = CreateObject("Excel.Application")
             Command1.Caption = "Show Excel"
          End Sub
          Private Sub Form_Unload(Cancel As Integer)
             Set oApp = Nothing
          End Sub
  5. On the Run menu, click Start, or press the F5 key to start the program.
  6. Click on the Command button to make Excel visible. Close Excel by pressing the Close button on Excel's title bar, or by selecting Exit from the File menu. Now press the Visual Basic Command button again, and note that Excel does not paint itself correctly.
  7. Repeat the steps again with the Command button's code modified as follows:
          Private Sub Command1_Click()
             oApp.Visible = True
             oApp.ScreenUpdating = True
          End Sub

↑ Back to the top

Keywords: kbautomation, kbbug, kbinterop, kbpending, KB187745

↑ Back to the top

Article Info
Article ID : 187745
Revision : 8
Created on : 1/23/2007
Published on : 1/23/2007
Exists online : False
Views : 460