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 Microsoft Advisory Services. For more information, visit these Microsoft Web sites:
Microsoft Certified Partners -
https://partner.microsoft.com/global/30000104Microsoft Advisory Services -
http://support.microsoft.com/gp/advisoryserviceFor more information about the support options that are available and about how to contact Microsoft, visit the following Microsoft Web site:
http://support.microsoft.com/default.aspx?scid=fh;EN-US;CNTACTMS
You can demonstrate the change in behavior by running the following Visual
Basic for Applications macro in Microsoft Excel:
Sub test()
Set w = Application.ActiveWindow
If w.NewWindow Then
MsgBox "Yes"
End If
End Sub
In Microsoft Excel 2000, this macro runs without error. In later versions of Microsoft Excel, this macro causes a run-time error 438.
This following macro works correctly with versions of Microsoft
Excel after 2000, because the
NewWindow method returns an object:
Sub test2()
Set w = Application.ActiveWindow
Set NewWin = Nothing
Set NewWin = w.NewWindow
If Not (IsEmpty(NewWin)) Then
MsgBox ("yes")
End If
End Sub
However, this macro fails when you run it in Microsoft Excel 2000 or earlier, because the
NewWindow method returns a Boolean value. When you run the macro, you receive the following error message:
Run-time error '13':
Type mismatch
Making your Code Work in All Versions of Microsoft Excel
If 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:
ExcelVersion = Val(Application.Version)
The value of "ExcelVersion" is either 5, 7, 8, 9, or 10 for Microsoft Excel 5.0, 7.0, 97, 2000, or 2002 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:
Sub test3()
Set w = Application.ActiveWindow
ExcelVersion = Val(Application.Version)
If ExcelVersion < 10 Then
MyBool = w.NewWindow
Else
Set NewWin = Nothing
Set NewWin = w.NewWindow
MyBool = Not (IsEmpty(NewWin))
End If
If MyBool Then
MsgBox ("yes")
End If
End Sub
This macro works correctly with Microsoft Excel 2000 or 2002.