WD2000: Documents/Windows Collection Fails to Include Hidden Documents

ID: Q220504


The information in this article applies to:


SYMPTOMS

When you use the Visible property of the Open method for the Documents collection to open an existing document as hidden, the document count and the window count are not incremented.

For example, the following Visual Basic for Applications procedure demonstrates this problem:


Sub CountHiddenDocs()
   Documents.Close ' Close ALL open documents.
   Documents.Add   ' Add a new blank document.
   With Application.Documents
      ' Open MyDoc.Doc document as hidden.
      .Open "<Path>\MyDoc.Doc", Visible:=False
      ' Return the number of documents in the Documents collection.
      MsgBox "There are" & .Count & " documents open in Word."
      MsgBox MsgBox "There are " & Windows.Count & _
         " window(s) in the Windows collection."
   End With
End Sub 
Running the procedure results in the message box displaying 1 instead of 2 for both the Documents and Windows collection.

Furthermore, you cannot make visible a document you open as hidden using the ActiveWindow.Visible command. Attempting to do so may result in the following error message:
Run-time error '91': Object variable or With block variable not set.
-or-

Run-time error '4248': This command is not available because no documents are opened.
For example, the following Visual Basic for Applications procedure demonstrates the run-time error '91':

Sub CountHiddenDocs()
   Dim Doc As Document
   Dim Doc1 As Document
   Documents.Close ' Close ALL open documents.
   Documents.Add   ' Add a new blank document.
   With Application.Documents
      ' Open MyDoc.Doc document as hidden.
      Set Doc1 = .Open("<Path>\MyDoc.Doc", Visible:=False)
      ' Return the number of documents in the Documents collection.
      MsgBox "There are" & .Count & " documents open in Word."
   End With

   ' Using the Documents collection fails to unhide
   ' hidden documents but does not return an error.
   For Each Doc In Documents
      Doc.ActiveWindow.Visible = True
   Next
   
   ' Using the document object variable fails to unhide the
   ' hidden document and returns the error mentioned earlier
   ' in this article.
   Doc1.ActiveWindow.Visible = True
End Sub 


WORKAROUND

Microsoft provides programming examples 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. This article assumes that you are familiar with the programming language being demonstrated and the tools used to create and debug procedures. Microsoft support professionals can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific needs. If you have limited programming experience, you may want to contact a Microsoft Certified Solution Provider or the Microsoft fee-based consulting line at (800) 936-5200. For more information about Microsoft Certified Solution Providers, please see the following page on the World Wide Web:

http://www.microsoft.com/mcsp/
For more information about the support options available from Microsoft, please see the following page on the World Wide Web:

http://www.microsoft.com/support/supportnet/overview/overview.asp
To work around this problem, you must treat the hidden document, for opening and referencing, as a separate document object. The following Visual Basic for applications procedure demonstrates this work around:


Sub CountHiddenDocs()
   ' This macro opens an existing document as hidden
   ' then makes the hidden document visible.
   Dim Doc1 As Document

   ' Open MyDoc.Doc document as hidden.
   Set Doc1 = Documents.Open("<Path>\MyDoc.Doc", Visible:=False)

   ' Reset the document object variable to make visible the
   ' hidden document.
   '
   ' NOTE: The following command line will NOT open a
   ' second instance of the hidden document but instead
   ' make the currently open, hidden instance of the
   ' document visible.
   Set Doc1 = Documents.Open("<Path>\MyDoc.Doc", Visible:=True)
   Doc1.ActiveWindow.Activate
End Sub 


STATUS

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


MORE INFORMATION

For more information about the Documents collection, in the Visual Basic Editor, click Microsoft Visual Basic Help on the Help menu, type "Documents collection" in the Office Assistant or the Answer Wizard, and then click Search to view the topic.


For more information about using the sample code in this article, please see the following article in the Microsoft Knowledge Base:

Q212536 OFF2000: How to Run Sample Code from Knowledge Base Articles

Additional query words: vba invisible


Keywords          : kbdta kbwordvba wd2000 
Version           : WINDOWS:2000
Platform          : WINDOWS 
Issue type        : kbbug 

Last Reviewed: July 14, 1999