1. Create an object
VBScript syntax for creating an object instance:
set variablename = CreateObject("Objectname")
Variablename is the variable used to save the object reference, and objectname is the type of the object to be created. Set tells VBScript to save an object reference instead of a common value.
Ii. Reference of nested objects
In scripts or WORD macros, we often see structures similar to the following.
ActiveDocument.PageSetup.Orientation = wdOrientLandscapeActiveDocument.PageSetup.TopMargin = InchesToPoints(0.5)ActiveDocument.PageSetup.BottomMargin = InchesToPoints(0.5)ActiveDocument.PageSetup.PageWidth = InchesToPoints(11)
In this example, the activedocument object returns a pagesetup object.
By saving a reference to the pagesetup object, you can reduce a lot of input work when creating this script, as shown below:
set ps = ActiveDocument.PageSetupps.Orientation = wdOrientLandscapeps.TopMargin = InchesToPoints(0.5)ps.BottomMargin = InchesToPoints(0.5)ps.PageWidth = InchesToPoints(11)
VBScript has a special program structure called the with statement. The preceding example is rewritten using the with statement:
with ActiveDocument.PageSetup .Orientation = wdOrientLandscape .TopMargin = InchesToPoints(0.5) .BottomMargin = InchesToPoints(0.5) .PageWidth = InchesToPoints(11)end with
3. Release objects
You can explicitly release an object by setting the variable value of the saved object to nothing.
VBScript learning notes-objects