This documentation is archived and is not being maintained.

Using Named Constants in VBScript

Office 2003

You cannot use named constants in VBScript code. The following example works in Visual Basic but does not work in VBScript.

Set valueAxis = ChartSpace1.Charts(0).Axes(chAxisPositionLeft)

VBScript regards the named constant chAxisPositionLeft as just another uninitialized variable, so its value is 0 (zero). Because the actual value of chAxisPositionLeft is –3, this code does not work as expected in VBScript.

The Constants property returns an object that allows VBScript programmers to use named constants. This property applies to each of the top-level container objects (ChartSpace, DataSourceControl, PivotTable, and Spreadsheet). It returns an object that contains all of the named constants available in the Microsoft Office Web Components type library (no matter which object the Constants property is applied to, it always returns the complete set of named constants).

To use named constants in VBScript, you can set an object variable to the object returned by the Constants property and then use that object to qualify the named constants in your code, as shown in the following example.

Set chConstants = ChartSpace1.Constants
Set valueAxis = ChartSpace1.Charts(0).Axes(chConstants.chAxisPositionLeft)


You can also use the Constants property directly in an expression, as shown in the following example.

Spreadsheet1.ActiveSheet.Export "sstest.xls", Spreadsheet1.Constants.ssExportActionNone


Note  You can use the Constants property in Visual Basic, but it is neither required nor recommended. Using the Constants property in containers where it is not required will cause your code to run significantly slower.