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( ...".
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.
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
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.
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
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.
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.
- VBScript, Text to Clipboard to Paste in Any Field, stackoverflow.com
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.
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.
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
- VBScript Version Information, msdn.microsoft.com
Comparison to VBAEdit
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
- Visual Basic for Applications Features Not In VBScript, msdn.microsoft.com
- Does VBScript allow named arguments in function calls?, stackoverflow.com
COM components often used with VBScript in WSH:
- Windows Script Host Object Model, msdn.microsoft.com