WD2000: VBA: Text Placed in Wrong Document Using Hidden Documents

ID: Q220420


The information in this article applies to:


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, 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/refguide/

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 this to be a problem in the Microsoft products listed at the beginning of this article.

Additional query words: vba


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

Last Reviewed: June 15, 1999