Microsoft KB Archive/137096

= PRB: Type Mismatch When Passing Variants to Word Using OLE =

Article ID: 137096

Article Last Modified on 12/9/2003

-

APPLIES TO


 * Microsoft Visual Basic 4.0 Standard Edition
 * Microsoft Visual Basic 4.0 Professional Edition
 * Microsoft Visual Basic 4.0 Professional Edition
 * Microsoft Visual Basic 4.0 16-bit Enterprise Edition
 * Microsoft Visual Basic 4.0 32-Bit Enterprise Edition

-



This article was previously published under Q137096



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



Steps to Reproduce and Correct Behavior
 Start a new project in Visual Basic 4.0. Form1 is created by default. Add a command button (Command1) and a text box (Text1) to Form1.  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  Press F5 to run the program. Click Command1. Error 13, "Type mismatch" is generated.  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 </li> 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).</li></ol>

Keywords: kbprogramming kbprb KB137096

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.