VBScript Programming

From Wikibooks, open books for an open world
Jump to navigation Jump to search

VBScript, short for Visual Basic Scripting Edition, is a Microsoft-supplied scripting language similar to Visual Basic and Visual Basic for Applications. It can be used via Windows Script Host (WSH) as a Windows scripting language. Furthermore, it can be used as a client-side scripting language in web pages instead of JavaScript, but such pages will only work with Internet Explorer. Moreover, it can be used for server-side web scripting as part of Active Server Pages (ASP), and in other environments. This book focuses on the language use via WSH.

Scripts to be used with WSH are usually plain text files with .vbs extension. By default, Windows associates the extension with an interpreter of the language, and therefore, entering the script name on the Windows batch command line leads to script execution, albeit without waiting for script completion. Furthermore, scripts can be run from the desktop or the Windows file explorer, usually by double clicking.

VBScript can access objects available via Microsoft COM interface, including those of Microsoft Office. Thus, Microsoft Office can be scripted not only from Visual Basic for Applications (VBA) embedded in office files such as spreadsheets but also from VBScript scripts, in a syntanctically similar yet somewhat different manner.

Two commands are available to let WSH execute a script: wscript and cscript. wscript is for GUI whereas cscript is for command line interaction. By default, .vbs extension is associated with wscript.

VBScript is mostly case-insensitive as for keywords and identifiers. Thus, "End If" is equivalent to "end if", and "FSO.CreateTextFile( ..." is equivalent to "fso.createtextfile( ...".

Data types[edit]

Unlike in VBA, variables cannot be dimensioned to be restricted to values of a particular data type, such as String. Rather, they are all of data type Variant.

However, there is still an underlying data type of each variable, depending on what you assign to the variable. You can find out about the current underlying data type of a variable content using TypeName function.

Links:

File operations[edit]

Unlike in VBA, file operations are not part of the language built-in core but rather are accessible from FileSystemObject.

Writing to a text file:

FileName = "MyFile.txt"
Set FSO = CreateObject("Scripting.FileSystemObject")
Set File = FSO.CreateTextFile(FileName, True)
File.Write "Hello, there." & vbCrLf
File.Close

MsgBox "File " & FileName & " written."

Getting current directory:

Set FSO = CreateObject("Scripting.FileSystemObject")
CurrentDirectory = FSO.GetAbsolutePathName(".") 
MsgBox CurrentDirectory

Links:

Excel[edit]

Excel can be scripted using VBScript by accessing Excel.Application object. Unlike VBA, VBScript does not support named argument passing to methods, only positional argument passing. Furthermore, functions that appear global from VBA in Excel need to be called as methods of the Excel object in VBScript.

An example:

Set FSO = CreateObject("Scripting.FileSystemObject")
CurrentDirectory = FSO.GetAbsolutePathName(".") 

Set Excel = CreateObject("Excel.Application")
Set Workbook = Excel.Workbooks.Open(CurrentDirectory & "\" & "MyFile.xlsx")
Workbook.Sheets(1).Cells(1, 1).Value = "Hey"
Workbook.Save
Workbook.Close

Constants[edit]

There are multiple built-in constants, starting with vb. For instance, vbOKCancel is used in conjunction with MsgBox.

Applications-specific constants such as Excel's xlAnd are not available.

Links:

Clipboard[edit]

VBScript does not support VBA's MSForms.DataObject to access clipboard.

There are workarounds:

  • For writing to the clipboard, you can run clip.exe available in Windows 7.
  • For reading from the clipboard, you can access Internet Explorer via COM and let it read from the clipboard.

Links:

External processes[edit]

You can run external processes using Run method of WScript.Shell:

Set Shell = WScript.CreateObject ("WScript.Shell")
Shell.Run "tasklist /v", 0, True

You can also do so using Exec method of WScript.Shell:

Set MyShell = CreateObject("WScript.Shell")
Set ExecObject = MyShell.Exec("tasklist /v")
' AllText = ExecObject.StdOut.ReadAll
Do While Not ExecObject.StdOut.AtEndOfStream
  Line = ExecObject.StdOut.ReadLine()
  If InStr(Line, "AcroRd32.exe") > 0 Then
    'Do something
  End If
Loop

Keywords: external commands, running programs.

In another Wikibook: Excel VBA#Command Output.

Links:

Regular expressions[edit]

You can use regular expressions using RexExp object:

Set RegExp = New RegExp
RegExp.Pattern = "[0-9][0-9]*"
If RegExp.Test("354647") Then
  MsgBox "Test passed."
End If

Alternatively, you could create the regex object via Set RegExp = CreateObject("VBScript.RegExp"), but, in VBScript, that is unnecessary.

Versions[edit]

VBScript versions include 5.1 (Win 2000), 5.6 (XP), 5.7 (Vista) and 5.8.

To find out about the version:

VBScriptVersion = ScriptEngineMajorVersion & "." & ScriptEngineMinorVersion

Links:

Comparison to VBA[edit]

Features missing from VBScript while present in VBA:

  • Named argument passing to functions and methods
  • Application-specific named constants such as Excel's xlAnd are not available; you have to set them on your own or pass numbers instead
  • Built-in file I/O; VBScript can use FileSystemObject
  • Creating custom collections via new Collection
  • Dimensioning variables with particular data types
  • Etc.

Links:

COM components[edit]

COM components often used with VBScript in WSH:

  • WScript.Shell
  • WScript.Network
  • Scripting.FileSystemObject
  • Scripting.Dictionary
  • Shell.Application
  • Excel.Application
  • Word.Application
  • Outlook.Application
  • InternetExplorer.Application

Links:

Related Wikibooks[edit]

External links[edit]