XL98: CreateObject and GetObject Work DifferentlyID: Q183539
|
The CreateObject and GetObject methods of Automation work differently in
Microsoft Excel 98 Macintosh Edition than they do in earlier versions of
Microsoft Excel.
This article explains the differences in behavior and offers some
suggestions for making Automation code work with Microsoft Excel 98
Macintosh Edition and earlier versions of Microsoft Excel.
Version Type of object returned
---------------------------------------------
Microsoft Excel 98 Workbook
Microsoft Excel 5.0 Worksheet
Examples:
http://www.microsoft.com/supportnet/refguide/You can demonstrate the change in behavior by running the following Visual Basic for Applications macro in Microsoft Excel:
Sub ShowTypeName()
Dim xlObj As Object
Set xlObj = CreateObject("Excel.Sheet")
MsgBox TypeName(xlObj)
Set xlObj = Nothing
End Sub
In Microsoft Excel 98, when you run the macro, a message box that displays
"Workbook" appears. In earlier versions of Microsoft Excel, the message is
"Worksheet."
Sub DemonstrateProblem()
Dim xlObj As Object
Set xlObj = CreateObject("Excel.Sheet")
MsgBox TypeName(xlObj)
xlObj.Parent.Close False
Set xlObj = Nothing
End Sub
However, this macro fails when you run it in Microsoft Excel 98 Macintosh
Edition, 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:
Run-time error '438':
Object doesn't support this property or method
ExcelVersion = Val(xlObj.Application.Version)
where "xlObj" is the name of the Microsoft Excel object.
Sub FixedProblem()
' Dimension variables.
Dim xlObj As Object, ExcelVersion As Integer
Dim xlTemp As Object
Set xlObj = CreateObject("Excel.Sheet")
' This line guarantees that xlObj will remain viable when we switch
' it to the active sheet.
Set xlTemp = xlObj
ExcelVersion = Val(xlObj.Application.Version)
If ExcelVersion >= 8 Then
Set xlObj = xlObj.ActiveSheet ' Reset the xlObj object to refer
' to the active sheet.
End If
' Code that works with the xlObj object goes here.
Set xlTemp = Nothing ' Both object variables should be set to
Set xlObj = Nothing ' Nothing.
End Sub
This macro works correctly with Microsoft Excel 5.0 and Microsoft Excel 98
Macintosh Edition. The macro also works correctly when you run it from
Microsoft Word 98 Macintosh Edition or any other program (including
Microsoft Excel) that supports Visual Basic for Applications.
Additional query words: XL98 automate ole automation
Keywords : kbprg
Version :
Platform : MACINTOSH
Issue type :
Last Reviewed: July 1, 1999