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

ID: Q209892


The information in this article applies to:


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, 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 the Microsoft fee-based consulting line at (800) 936-5200. 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/

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 

Additional query words: vba


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

Last Reviewed: May 13, 1999