WD2000: VBA: Text Placed in Wrong Document Using Hidden Documents (220420)



The information in this article applies to:

  • Microsoft Word 2000

This article was previously published under Q220420

SYMPTOMS

When you try to add text to a document that was hidden using the Visible statement, you may receive unexpected results.

For example, the following procedure demonstrates this behavior.

In the following procedure, if you have a document open (MyDoc.doc) before running the procedure, when you run the procedure, a new document is created. Instead of the the text being inserted into the new document as expected, the text is inserted into MyDoc.doc:
Sub HideDoc()
    Dim lngDoc As Long
    Dim sDocName As String
    Documents.Add
    sName = ActiveDocument.Name
    lngDoc = ActiveWindow.WindowNumber
    ActiveWindow.Visible = False
    Windows(lngDoc).Activate
    Selection.TypeText sName
    ActiveWindow.Visible = True
End Sub
				

RESOLUTION

Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers 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 requirements.
To resolve this problem, you can assign a variable to the Visual Basic for Applications Document object to access and manipulate the hidden document window.

For example:
Sub HiddenDoc()
   Dim oDoc as Document
   Dim sDocName As String
   Set oDoc = Documents.Add
   sDocName = oDoc.Name
   With oDoc.ActiveWindow
      .Visible = False
      .Selection.TypeText sDocName
      .Visible = True
   End With
End Sub
				

STATUS

Microsoft has confirmed that this is a problem in the Microsoft products that are listed at the beginning of this article.

Modification Type:MajorLast Reviewed:6/17/2005
Keywords:kbbug kbnofix KB220420