Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

XL2000: Cannot Use xlConstant Names in Variables in Visual Basic Code


View products that this article applies to.

This article was previously published under Q213809

↑ Back to the top


Symptoms

When you attempt to use Microsoft Excel 2000 constant names to set properties in Visual Basic code, you may receive the following error message:
Type mismatch
For example, if you want to show the Open dialog box, and you use the following sample code (where the Excel constant is represented by a variable)
Sub Open_Box()
   Dim var As String
   var = "xlDialogOpen"
   Application.Dialogs(var).show
End Sub
				
you receive the "type mismatch" error message.

↑ Back to the top


Cause

This behavior occurs because Excel constant names are not actually handled as strings, but rather as the values that they represent.

↑ Back to the top


Workaround

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 a Microsoft Certified Partner or the Microsoft fee-based consulting line at (800) 936-5200. For more information about Microsoft Certified Partners, please visit the following Microsoft Web site: For more information about the support options that are available and about how to contact Microsoft, visit the following Microsoft Web site: To set the Excel constant values to variables in Visual Basic, an integer variable must be set to the value that the constant name represents, as in the following sample code:
Sub Open_Box()
   Dim var As Integer
   var = xlDialogOpen ' Note the absence of quotation marks.
   Application.Dialogs(var).Show
End Sub
				
When this type of code is run, the Open dialog box appears as expected.

↑ Back to the top


Keywords: KB213809, kbprogramming, kbprb, kberrmsg, kbdtacode

↑ Back to the top

Article Info
Article ID : 213809
Revision : 7
Created on : 11/23/2006
Published on : 11/23/2006
Exists online : False
Views : 263