The information in this article applies to:
SYMPTOMS
The CreateObject and GetObject methods of Automation work differently in Microsoft Excel 97 and later than they do with versions earlier than Microsoft Excel 97.
MORE INFORMATIONBehavior in Different Versions of Microsoft ExcelWhen you use CreateObject or GetObject in a macro to work with a Microsoft Excel sheet object, such as "Excel.Sheet" or "Excel.Sheet.8," the type of object the macro returns is different for different versions of Microsoft Excel.
ExamplesMicrosoft 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 Solution Provider or the Microsoft fee-based consulting line at (800) 936-5200. For more information about Microsoft Certified Solution Providers, please see the following page on the World Wide Web: http://www.microsoft.com/mcsp/For more information about the support options available from Microsoft, please see the following page on the World Wide Web: http://www.microsoft.com/support/supportnet/overview/overview.asp You can demonstrate the change in behavior by running the following Visual Basic for Applications macro in Microsoft Excel or Microsoft Visual Basic, version 4.0:
In Microsoft Excel, when you run the macro, a message box that displays "Workbook" appears. In versions of Microsoft Excel earlier than Excel 97, the message is "Worksheet". This change in behavior may cause a problem if your code uses properties and methods that are specific to the type of object to which the macro references. This following macro works correctly with earlier versions of Microsoft Excel, because the Parent property of xlObj (a Worksheet object) is a Workbook object; and the Close method applies to workbooks:
However, this macro fails when you run it in Microsoft Excel 97 or later, because the Parent property of xlObj (a Workbook object) is an Application object, and the Close method does not apply to the Application. When you run the macro, you receive the following error message:
Making your Code Work in All Versions of Microsoft ExcelIf you want to use Automation with Microsoft Excel, but you do not know which version of Microsoft Excel is running, you can modify your code to work correctly with any version of Microsoft Excel.One way to do this is to check the version of Microsoft Excel from the macro, and then store the version number in a variable. To do this, use the following line of code:
where "xlObj" is the name of the Microsoft Excel object. The value of "ExcelVersion" is either 5, 7, 8, or 9 for Microsoft Excel 5.0, 7.0, 97, or 2000 respectively. After you determine the version of Microsoft Excel you are using, modify the macro to work correctly with that version of Microsoft Excel. For example, you can make the macro in this article work correctly by adding a few lines of code. The following example illustrates how to change the macro:
This macro works correctly with Microsoft Excel 5.0, 7.0, 97, or Microsoft Excel 2000. The macro also works correctly when you run it from Microsoft Visual Basic, Microsoft Word, or any other program (including Microsoft Excel) that supports Visual Basic or Visual Basic for Applications. Additional query words: XL2000 automate ole automation
Keywords : kbprg kbdta kbdtacode KbVBA |
Last Reviewed: July 6, 1999 © 2000 Microsoft Corporation. All rights reserved. Terms of Use. |