JavaScript Instance tutorial OLE Automation (3)

Source: Internet
Author: User
Tags object constructor contains ole version variable
javascript| Tutorial

Using OLE Automation in JavaScript

3. How to create a new instance of a Automation object

Jscript (Microsoft version of JavaScript) is a relaxed type of language. In other words, the variable is not clearly defined as the specified data type. You cannot declare a variable to be of the specified object type, so early binding cannot be in JScript. The ActiveXObject () constructor for JScript is typically used to create a late-bound interface for an object:

var wdapp; A normal variable

Wdapp = new ActiveXObject ("Word.Application");

JScript does not require obvious variable declarations, so we can use the following code:

var wdapp = new ActiveXObject ("Word.Application");

The general syntax for the ActiveXObject () function is as follows:

var objvar = new ActiveXObject (class[, servername));

OBJVAR specifies a variable that holds a reference to the instantiated object. class uses the Library.object syntax, which refers to the name of the application (such as Word or Excel, and so on) or the library that contains the object, and the object is the type of object class to create. ServerName (this is an optional parameter) specifies the name of the server that contains the object.

This ActiveXObject () constructor creates an instance of an OLE Automation (ActiveX) object. Once the object is created, you can use the defined object variable to refer to it. If an instance of an ActiveX object is already running, ActiveXObject () may start a new instance when it creates an object of the required type. The following code snippet allows users to open a Word document directly:

var pause = 0;

var wddialogfileopen = 80;

var wdapp = new ActiveXObject ("Word.Application");

var dialog = Wdapp.dialogs (wdDialogFileOpen);

var button = dialog. Show (pause);

Using OLE Automation in JavaScript

3. How to create a new instance of a Automation object

The preceding code snippet refers to the Word.Application object, and its method is described later in the tutorial. Notice the hierarchy, where a method or attribute is an object, and it has its own methods and properties.

Like visual Basic 6,jscript you can add a ActiveXObject function in one step by adding a new parameter (ServerName). You can specify the name of the server on the registered Actinvex object. Creating an object on a remote server can be done when the Internet Security check program shuts down. You can create an object on a remote network computer by passing the name of the computer to the servername parameter of the ActiveXObject () constructor. For example, if a network share is named "\\MYSERVER\D\Program Files," then the servername value is ' MYSERVER '. Alternatively, you can use the DNS format or an IP address to specify the location. The following function returns the version number of the instance running on the remote network computer (named "MYSERVER") Word:

function GetVersion () {

var wdapp = new ActiveXObject ("Word.Application", "MYSERVER");

return wdapp.version;

}

If the specified remote server does not exist or is not found, an error occurs. When we test this function, we can see that JScript ignores the servername parameters. So we put these functions together in VBScript and we can see that it doesn't work:

Function getversion

Dim Wdapp

Set Wdapp = CreateObject ("Word.Application", "MYSERVER")

GetVersion = Wdapp.version

End Function



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.