WD2000: Document Not Displayed with CreateObject ("Word.Document") (209892)



The information in this article applies to:

  • Microsoft Word 2000

This article was previously published under Q209892

SYMPTOMS

When you use the Visual Basic for Applications CreateObject function to create a "Word.Document" object, the document is not visible.

For example:
Sub CreateDocObject()
   Dim oDoc As Document
   Set oDoc = CreateObject(Class:="Word.Document")
End Sub
				

NOTE: If you are running this macro from within Word 97, the sample procedure above would create a visible document. If the sample macro were run from another application (for example, Microsoft Excel), the document would not be visible.

CAUSE

This behavior is by design. The CreateObject function should only create the server object, not the document. In Word 97, creation and visibility of a new document by using the CreateObject function is not the expected behavior while within Word 97.

MORE INFORMATION

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.
Creation and visibility of a new document, using the CreateObject function, occurs on the first use of any document object property or method.

For example, the following Visual Basic for Applications procedure creates a variable set to reference a Word document object. The document object does not become visible until the oDoc.Paragraphs.Add command line is run:
Sub CreateDocObject()
   Dim oDoc As Document
   Set oDoc = CreateObject(Class:="Word.Document")
   oDoc.Paragraphs.Add
End Sub
				


NOTE: This behavior occurs when the above sample macro is run from within Word 2000. If Word 2000 is being automated from another application (Excel 2000, for example), you can use the Application.Visible property to display the document.

To run the following macro from within Excel, you must first add a reference to Microsoft Word 9.0 Object Library.

To add a reference to the Microsoft Word 9.0 Object Library:
  1. While in the Visual Basic Editor, on the Tools menu, click References.
  2. From the list of Available References, click to check Microsoft Word 9.0 Object Library.
  3. Click OK.
Sub CreateDocObject()
   Dim oDoc As Document
   Set oDoc = CreateObject(Class:="Word.Document")
   oDoc.Paragraphs.Add
   oDoc.Application.Visible = True
End Sub
				

Modification Type:MajorLast Reviewed:6/17/2005
Keywords:kbnofix kbprb KB209892