Installing and Registering for COM

Using from WSH

Though there are some less known techniques that you may need to use, which are described in following sections.

Accessing Enumeration Values

As JScript and VBScript access COM classes via IDispatch, they do not make use of type library, hence they do not have direct access to constants defined there, like Protocol.Sftp for instance. To use these, you have to instruct WSH to import the type library into the script namespace.

You can use Windows Script File (WSF) and its <reference> tag for that. It makes WSH import all enums from the assembly type library into constants in script namespace with name like <type>_<member>, e.g. Protocol.Sftp becomes Protocol_Sftp.

See how <reference object="WinSCP.Session"/> allows use of Protocol_Sftp in below JScript script embedded in WSF:

Error Handling in VBScript

VBScript does not support catching exceptions, what is a common way of handling errors in examples for most other languages.

In case you need to use custom error handling, instead of aborting the script on error (the default for WSH), use On Error statement.

Use On Error Resume Next to disable default error handling. Then you need to query Err.Number after every statement to test for errors. You can revert to default error handling (aborting the script) using On Error GoTo 0.

If you do not want to test for errors after every statement, you need to group the staments you want to guard into a subprocedure and enable custom error handling before calling/entering the subprocedure.

This approach is also recommended to ensure that Session.Dispose is called even in case of error.