OFF97: Can't Dimension Word as Application from Other Program

ID: Q165926


The information in this article applies to:
  • Microsoft Visual Basic for Applications version 5.0, included with:
    • Microsoft Word 97 for Windows
    • Microsoft PowerPoint 97 For Windows
    • Microsoft Excel 97 for Windows


SYMPTOMS

When you create a Visual Basic for Applications macro that calls Visual Basic for Applications commands for Word from another program, such as Microsoft Excel or Microsoft PowerPoint, you may receive the following error message:

Run-time error '13': Type mismatch


CAUSE

This problem may occur when you dimension the Word Object variable as Application.

For example, the following example Visual Basic for Applications macro causes the error message:


   Sub GetWordObject()
      ' Dimension oWord97 as an Application object.
      Dim oWord97 As Application
      ' Type mismatch error occurs at this line.
      Set oWord97 = GetObject(, "Word.Application")
   End Sub 


WORKAROUND

Microsoft provides examples of Visual Basic for Applications procedures 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. The Visual Basic procedures in this article are provided 'as is' and Microsoft does not guarantee that they can be used in all situations. While Microsoft support professionals can help explain the functionality of a particular macro, they will not modify these examples to provide added functionality, nor will they help you construct macros to meet your specific needs. If you have limited programming experience, you may want to consult one of the Microsoft Solution Providers. Solution Providers offer a wide range of fee-based services, including creating custom macros. For more information about Microsoft Solution Providers, call Microsoft Customer Information Service at (800) 426-9400.

To workaround this problem, use one of the following methods.

Method 1: Dimension the Word Object Variable as Object

You can dimension the Word Object variable as an Object type. For example, use the following macro:

   Sub GetWordObject()
      ' Dimension oWord97 as Object.
      Dim oWord97 As Object
      Set oWord97 = GetObject(, "Word.Application")
      Set oWord97 = Nothing
   End Sub 

Method 2: Add the Word 8.0 Object Library Reference

You can add a reference to the Word 8.0 Object Library and use one of the following examples to call the Visual Basic for Applications commands for Word.

Sample Macro That Creates a New Instance of Word:

The following sample macro uses the New keyword to create a new instance of Word, adds a new document, inserts text, saves the document, and then quits the new instance of Word:

   Sub CreateNewWordObject()
      ' Dimension oWord97 as Word.Application object.
      Dim oWord97 As New Word.Application
      ' Add a new document.
      oWord97.Documents.Add
      oWord97.Selection.Text = "This is a test."
      oWord97.ActiveDocument.SaveAs ("ExcelTest")
      ' Quit the Word instance.
      oWord97.Quit
      ' Free the object reference from memory.
      Set oWord97 = Nothing
   End Sub 
NOTE: You must use the Quit method before the macro is finished or the Word instance remains in memory. Setting the object reference equal to Nothing does not remove the instance of Word from memory. However, setting the Object reference equal to Nothing frees the referenced object from memory. Microsoft recommends that you use both statements together to clear memory when the macro is finished.

Sample Macro That Uses the Existing Instance of Word:

If Word is already running and you want to use the existing instance of Word, use the following sample macro:

   Sub GetWordObject()
      ' Dimension oWord97 as a Word.Application object.
      Dim oWord97 As Word.Application
      ' Set the Object reference.
      Set oWord97 = GetObject(, "Word.Application")
      ' Add a new document.
      oWord97.Documents.Add
      oWord97.Selection.Text = "This is a test."
      oWord97.ActiveDocument.SaveAs ("ExcelTest")
      ' Free the object reference from memory.
      Set oWord97 = Nothing
   End Sub 
To add the Word 8.0 Object Library from Microsoft Excel, Microsoft PowerPoint, or Microsoft Access, use the following steps:

  1. On the Tools menu, click References to display the References dialog box.

    The References dialog box displays all object libraries and projects that are registered with the operating system.


  2. Scroll down to see the Word 8.0 Object Library reference.

    References with check boxes that are selected are used by the project; items with check boxes that are cleared are not used. However, you can add the references.


  3. Click the Word 8.0 Object Library reference in the Available References box and click OK.



STATUS

Microsoft has confirmed this to be a problem in the Microsoft products listed at the beginning of this article. We are researching this problem and will post new information here in the Microsoft Knowledge Base as it becomes available.

Additional query words: wordcon word8 word97 xlvbainfo 8.00 8.0 vb vba vbe

Keywords : kbinterop kbprg
Version : WINDOWS:5.0,97
Platform : WINDOWS
Issue type : kbbug


Last Reviewed: November 3, 1999
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.