BUG: Word 2000 Does Not Quit After Saving Hidden Document Using Automation (266078)



The information in this article applies to:

  • Microsoft Word 2000, Service Release 1
  • Microsoft Visual Basic Professional Edition for Windows 6.0
  • Microsoft Visual Basic Enterprise Edition for Windows 6.0

This article was previously published under Q266078

SYMPTOMS

When you are automating Microsoft Word 2000, the Quit method does not successfully shut down Word. Word is left in the task list even after your Automation client has called the Quit method. To quit Word, you must manually shut down the application by using the Windows Task Manager.

CAUSE

This problem occurs when:
  • The Automation client code is running in Debug mode (stepping through the code),

    -and-
  • you call the Add or Open method of the Documents collection and specify False for the Visible argument to open a hidden document,

    -and-
  • the hidden document is saved.

RESOLUTION

You can avoid this problem by using one of the following workarounds:
  • Programmatically open and close a blank document after saving the hidden document.
    -or-
  • Open a blank document before calling the Quit method to quit Word.

    -or-
  • Make the hidden document visible before calling the Quit method (requires Microsoft Word 2000 SR1 or SR1a).

STATUS

Microsoft has confirmed that this is a bug in the Microsoft products that are listed at the beginning of this article. This problem was corrected in Microsoft Word 2002.

MORE INFORMATION

Steps to Reproduce Behavior

  1. Start Visual Basic and create a new Standard EXE Project. Form1 is created by default.
  2. Place a CommandButton control on Form1.
  3. On the Project menu, select References, and then add a reference to the Microsoft Word 9.0 Object Library.
  4. Paste the following code in the code window for Form1:
    Private Sub Command1_Click()
      Dim oWord As Word.Application
      Dim oDoc As Word.Document
    
      Stop
      Set oWord = CreateObject("Word.Application")
      oWord.Visible = True
      Set oDoc = oWord.Documents.Open("C:\test.doc", Visible:=False)
      oDoc.Save
      oWord.Quit
      Set oDoc = Nothing
      Set oWord = Nothing
    End Sub 
  5. Press the F5 key to run the project and click the CommandButton. Note that Visual Basic enters Debug mode at the Stop statement. Press F8 to step through the remaining lines of code. Note that after the Quit method, Word does not shut down but does report high CPU usage.

Workaround

To work around the problem in the preceding sample, you can do one of the following:
  • After you save the hidden document, open and close a blank document, as follows:
    oDoc.Save
    Dim temp as Word.Document
    Set temp = oWord.Documents.Add
    temp.Close
    Set temp = Nothing
  • Before you quit Word, add a blank document:
    oWord.Documents.Add
    oWord.Quit
  • Before you quit Word, unhide the hidden document:
    oDoc.ActiveWindow.Visible = True
    oWord.Quit
    To unhide the hidden document, you must have installed the Microsoft Office 2000 SR-1/SR-1a as explained in the following Microsoft Knowledge Base article:

    220504 WD2000: Documents/Windows Collection Fails to Include Hidden Doc

    If you do not have Microsoft Office 2000 SR-1/SR-1a installed, you receive a run-time error 91 when you attempt to unhide the document.

Modification Type:MajorLast Reviewed:11/4/2002
Keywords:kbAutomation kbbug kbDSupport kbProgramming KB266078 kbAudDeveloper