PRB: Type Mismatch When Passing Variants to Word Using OLE
ID: Q137096
|
The information in this article applies to:
-
Microsoft Visual Basic Standard, Professional, and Enterprise Editions, 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
- 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
- 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).
Additional query words:
Keywords : kbprg kbVBp400 IAPOLE VB4WIN vbwin
Version : WINDOWS:4.0
Platform : WINDOWS
Issue type : kbprb