2

According to the Microsoft documentation, one can create instances of the COM objects using both the ActiveXObject() and the WScript.CreateObject() functions. It seems like the lines

var objXL = new ActiveXObject("Excel.Application");

and

var objXL = WScript.CreateObject("Excel.Application");

are identical. Is this a true assumption? and if not what is the difference? Examples to show the difference would be highly appreciated.

P.S. The post this has been flagged as a duplicate to is about the difference between VBScript's CreateObject() method and JScript's WScript.CreateObject(). It answers mention the JScript's ActiveXObject() constructor with no further elaborations.

Foad S. Farimani
  • 12,396
  • 15
  • 78
  • 193
  • 3
    Here's the [same question](https://stackoverflow.com/q/7932771/692942) from 9 years ago. – user692942 Oct 31 '20 at 22:08
  • @Lankymart . thanks. hadn't seen that. but yet not answered! – Foad S. Farimani Oct 31 '20 at 22:10
  • 3
    Here's [an answer](https://stackoverflow.com/a/787394/692942). – user692942 Oct 31 '20 at 22:10
  • @Lankymart the question is not correct. JScript doesn't have a `CreateObject()` function like VBScript. And the answer says `I would guess there is no difference between the two functions` with no further explanations. – Foad S. Farimani Oct 31 '20 at 22:12
  • 2
    The answer is, there is no difference. What do you expect when you ask a question that is going to attract a lot of opinion based answers? – user692942 Oct 31 '20 at 22:14
  • @Lankymart for me it is a learning opportunity to know what those two functions do. I expect to see opinions and examples and learn. – Foad S. Farimani Oct 31 '20 at 22:16
  • It's a Microsoft extension of the ECMAScript to support COM objects. See now obsolete [`ActiveXObject`](https://developer.mozilla.org/en-US/docs/Archive/Web/JavaScript/Microsoft_Extensions/ActiveXObject). – user692942 Oct 31 '20 at 22:18
  • In which case, do it somewhere else because [so] is not designed for opinion based questions. – user692942 Oct 31 '20 at 22:19
  • 1
    @Lankymart I think the page you linked is not for classic JScript. I don't think `let` is a valid JScript command. – Foad S. Farimani Oct 31 '20 at 22:22
  • 1
    You're right, but ECMAScript has moved on where as JScript (ECMAScript3) didn't. In terms of a reference that is the closest you're going to get, because Microsoft in their infinite wisdom decided to wipe any reference to official documentation from search engines. – user692942 Oct 31 '20 at 22:26
  • @Lankymart but JScript, VBScript, WSH, and HTA are still shipped in all Windows machines out there. I honestly wonder why they never took off as JavaScript, NodJS and Electron did. – Foad S. Farimani Oct 31 '20 at 22:28
  • 2
    Appears as though you can still find the [official documentation](https://learn.microsoft.com/en-us/previous-versions/windows/internet-explorer/ie-developer/scripting-articles/7sw4ddf8(v=vs.84)) but it wasn't easy. – user692942 Oct 31 '20 at 22:35
  • @TylerH it is a relevant discussion but the question is completely different. At least I now know that the two are not identical, considering that the `WScript.CreateObject()` method has a second parameter. I still need to figure out what it exactly does. – Foad S. Farimani Nov 02 '20 at 18:11
  • 2
    ActiveXObject dates back to JScript in IE, back when Microsoft still tried to actively promote ActiveX as a way to do scripting in web browsers. CreateObject goes back to earlier times and has a Basic lisp. ActiveX is more capable, also supported a visible representation of an object. But that didn't get used much and as a programmatic widget (as used in the question) the distinction disappears. – Hans Passant Nov 02 '20 at 22:45
  • @HansPassant thanks for the comment, sir. Would you be kind to elaborate on the more capable part? – Foad S. Farimani Nov 02 '20 at 22:47
  • 3
    Having a visible representation is a big deal. Most programmers are familiar with the concept of a "control", a space carved out in the host view that has its own behavior, written by programmers that don't have to talk to the host programmers every day. But that world is not ideal, they live on an island and the boat does not arrive as often as it should. Which is what killed it. – Hans Passant Nov 02 '20 at 23:14

1 Answers1

6

Are they the same?

The short the answer is Yes they are the same (in the sense they perform the same job of instantiating an automation object).

Basically unlike VBScript which has the global function CreateObject() there is no such equivalent in JScript which was based on ECMAScript 3rd Edition. So, Microsoft added its own extension ActiveXObject which does the same job as CreateObject.

Both languages can be hosted in the Windows Scripting Host which gives them access to WScript.CreateObject() which is another method that does exactly the same function but only in the context of the WScript object that is only available through the Windows Scripting Host.

Following up

There has been some debate about whether they are the same, I still stand by my original answer they are the same. However, I will concede that I was comparing VBScript CreateObject() and JScript new ActiveXObject() not Wscript.CreateObject() (which is slightly different).

Let's be clear though, all these functions and objects serve the same purpose which is to instantiate an automation object (COM). To back this up here is the official description of each;

WScript - CreateObject() Method

Creates a COM object

JScript - ActiveXObject Method

Enables and returns a reference to an Automation object

VBScript - CreateObject() Function

Creates and returns a reference to an Automation object

If they were completely the same what would the point of them be? We already have language-specific automation instantiation methods, so what would the point of Wscript.CreateObject() be?

The difference is when called with a second parameter it allows you to specify a prefix that will use to distinguish event handlers for that COM object.

Here is an example taken from this answer that shows how the second argument is used to set a prefix of objIE_ that will then be used to prefix any event handlers associated with that COM object, in this case, the InternetExplorer.Application object.

// JScript
var objIE = WScript.CreateObject("InternetExplorer.Application","objIE_")
objIE.Visible = true

while (objIE.Visible){
    WScript.Sleep(500);
}

function objIE_NavigateComplete2(pDisp, URL){
    WScript.Echo("You just navigated to", URL)
} 

function objIE_OnQuit(){
    boolBrowserRunning = false ;
}

It allows an Internet Explorer instance to be opened and the URL navigated to captured through the bound event, once the Internet Explorer Window is closed the script will end.

So while not identical they do perform the same function of instantiating an Automation (COM) object.


Useful Links

user692942
  • 16,398
  • 7
  • 76
  • 175
  • So `WScript.CreateObject()` is not available in the HTA files? – Foad S. Farimani Nov 01 '20 at 00:27
  • 2
    No, because `mshta.exe` is it’s own host. Both `cscript.exe` and `wscript.exe` allow the use of the `WScript` object. But other WScript objects like `WScript.Shell` can be because they are instantiated through `CreateObject()` and `new ActiveXObject()`. – user692942 Nov 01 '20 at 00:29
  • Is it correct to call `ActiveXObject` an "object generator"? it is a function, not a property per se. – Foad S. Farimani Nov 01 '20 at 00:31
  • 2
    It’s actually more an object instance than a function as it uses the `new‘ keyword. Notice how the [official documentation](https://learn.microsoft.com/en-us/previous-versions/windows/internet-explorer/ie-developer/scripting-articles/7sw4ddf8(v=vs.84)) calls it an "Object" not a "Function". – user692942 Nov 01 '20 at 00:32
  • so it is a class? can you be kind to provide an example using the `new` keyword in another context? for example to create a String object? – Foad S. Farimani Nov 01 '20 at 00:34
  • 2
    There’s [an example here](https://stackoverflow.com/a/6399300/692942) if it helps. – user692942 Nov 01 '20 at 00:35
  • Yup. Had seen those. I am no expert here but I think we may call `ActiveXObject()` some sort of class generator. My knowledge of OOP is very primitive, but object to me is an instance of a class. – Foad S. Farimani Nov 01 '20 at 00:39
  • 2
    The use of `new` means it is an instance of a class, so object seems right imo. Doesn’t matter either way, think the questions been answered. – user692942 Nov 01 '20 at 00:47
  • BTW the `var testString = new Number("123.22")` works. One can create `String()`, `Number()`, and `Boolean()` objects with the `new` keyword. I guess we should be able to create `function` objects as well. – Foad S. Farimani Nov 01 '20 at 00:48
  • Think is important to note that I originally only answered this question in an attempt to explain the differences between `CreateObject()`, `WScript.CreateObject()` and `new ActiveXObject()`. – user692942 Nov 16 '20 at 23:59
  • Is there something wrong with this answer? – user692942 Dec 10 '20 at 11:51
  • sadly this is not a correct answer. `new ActiveXObject()` and `WScript.CreateObject()` are not identical. for example, the latter is not available in the context of HTAs and it also has a second parameter related to events, that I haven't comprehended yet. – Foad S. Farimani Dec 10 '20 at 11:55
  • 1
    @Foad I'm not sure how not being available in HTAs is relevant? The `WScript` object is only available through the Windows Scripting Host, my point is they **all** perform the same function, I've tried to edit my answer to clear that up. – user692942 Dec 10 '20 at 12:19
  • Thanks for editing the answer it is indeed now more complete. I would appreciate it if you could elaborate on the second parameter of `Wscript.CreateObject()` as also discussed [here](https://www.reddit.com/r/learnjavascript/comments/jlr5k7/what_is_the_difference_between_new_activexobject/garv2mz?utm_source=share&utm_medium=web2x&context=3). It would be very kind of you to provide an example as well. – Foad S. Farimani Dec 10 '20 at 12:46