VBScript learning notes-objects

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.