PRB: Type Mismatch When Passing Variants to Word Using OLE

Last reviewed: September 29, 1997
Article ID: Q137096
The information in this article applies to:
  • Standard, Professional, and Enterprise Editions of Microsoft Visual Basic, 16-bit and 32-bit, for Windows, version 4.0

SYMPTOMS

When trying to pass a Variant data type to Microsoft Word using OLE automation, a "Type mismatch" error occurs. For example, if objVar is an object variable set to a Word.Basic object, and Text1 is some text box containing a string value, the following line of code generates the error:

   objVar.Insert  Text1

This problem did not exist in Visual Basic version 3.0, so it must be taken into consideration when doing any code porting and/or upgrading.

RESOLUTION

To avoid this problem, the variable passed to Word must be explicitly typed. The previous example can be replaced by either of the following to prevent this error:

   objVar.Insert Text1.Text

   -or-

   objVar.Insert CStr(Text1)

STATUS

This behavior is by design

MORE INFORMATION

Steps to Reproduce and Correct Behavior

  1. Start a new project in Visual Basic 4.0. Form1 is created by default.

  2. Add a command button (Command1) and a text box (Text1) to Form1.

  3. Place the following code in the Command1_Click event procedure:

          Dim objVar As Object
          Set objVar = CreateObject("Word.Basic")
          objVar.FileNew
          objVar.Insert Text1
          Set objVar = Nothing
    

  4. Press F5 to run the program.

  5. Click Command1. Error 13, "Type mismatch" is generated.

  6. Modify the code in the Command1_Click event procedure to this:

          Dim objVar As Object
          Set objVar = CreateObject("Word.Basic")
          objVar.FileNew
          ' Notice that this is the only line changed
          objVar.Insert Text1.Text
          Set objVar = Nothing
    

  7. Repeat step 5. No error is encountered. Check Microsoft Word, and you will find a new document containing the word "Text1" (the value of Text1.Text).
Keywords          : IAPOLE VB4ALL VB4WIN vbwin GnrlVb kbprg
Technology        : kbole kbvba
Version           : WINDOWS:4.0
Platform          : WINDOWS
Issue type        : kbprb


================================================================================


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: September 29, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.