Questions tagged [vbscript]

VBScript (Visual Basic Scripting Edition) is an interpreted scripting language developed by Microsoft that is modeled on Visual Basic. VBScript is not the same thing as VBA or VB.NET. They are three different things, so use the correct tags.

VBScript (Visual Basic Scripting Edition) is an interpreted scripting language developed by Microsoft that is modeled on Visual Basic.

VBScript is commonly used for automating administrative and other tasks in the Windows operating systems (via Windows Script Host --) and for server-side scripting in web applications. It can also used for client-side scripting in (not other browsers), but is generally used for this purpose only in intranet web applications, where the browser can be limited to IE. VBScript is also the language used to create custom Outlook forms ().

Although VBScript has much common syntax with VBA, do not tag questions as unless you are specifically asking about both. It is also completely different than VB.NET.

Some differences between VBScript and VBA:

  • Does not support enumerated constants; replace with the numeric value:

    'VBScript
    Outlook.CreateItem(0)
    
    'VBA allows this:
    Outlook.CreateItem(olMailItem)
    
  • All variables are of type Variant, and are declared without a type specification:

    'VBScript
    Dim olApp  
    Dim msg
    
    'VBA allows this:
    Dim olApp As Outlook.Application  
    Dim msg As Outlook.MailItem
    
  • Method calls don't support named arguments.

    'VBScript
    wb.SaveAs "output.csv", 6, , , , False
    
    'VBA allows this:
    wb.SaveAs FileName:="output.csv", FileFormat:=xlCSV, CreateBackup:=False
    

Executing VBScripts with WSH -- WScript/CScript

VBScript can be executed locally either in GUI mode, in which output is displayed as a window:

wscript.exe C:\Script\Location\script.vbs

or in console mode, in which input is read from and written to a console window.

cscript.exe C:\Script\Location\script.vbs

Running wscript.exe or cscript.exe without specifying the path will run the script in the machine's architecture -- 32-bit on 32-bit machines, and 64-bit on 64-bit machines. On 64-bit machines, it is possible to have the script run in the 32-bit emulation layer:

C:\windows\SysWOW64\wscript.exe  C:\Script\Location\script.vbs
C:\windows\SysWOW64\cscript.exe  C:\Script\Location\script.vbs

Note: Scheduled VBScript tasks must be run with cscript.exe because computer/domain policies limit activation of GUI applications when no user is logged on.

Debugging scripts

Visual Studio (Community Edition, or the Integrated Shell of pre-2013 versions) can be used to step through scripts and locate errors.

  • //X will trigger the debugger if there is a runtime error, or a code breakpoint
  • //D will trigger the debugger at the beginning of the script

No variable expansion
Like other languages in the Visual Basic family, VBScript does not expand variables inside string literals, so in code like

var1 = "fox"
var2 = "The quick brown var1 jumps over the lazy dog."

the value of var2 will remain The quick brown var1 jumps over the lazy dog. instead of becoming The quick brown fox jumps over the lazy dog. To get the value of a variable in a string, the variable must be concatenated with the rest of the string:

var1 = "fox"
var2 = "The quick brown " & var1 & " jumps over the lazy dog."

Frequently Asked Questions

Resources

Related Tags

18718 questions
58
votes
12 answers

How to set delay in vbscript

How to set delay in vbscript? WScript.Sleep(100) does not work on Windows XP, Vista.
Mark
  • 589
  • 1
  • 4
  • 3
56
votes
9 answers

Getting current directory in VBScript

I'm trying to get the current directory and use it to run an application no matter where the file is put and no matter how the path is changed Dim fso: set fso = CreateObject("Scripting.FileSystemObject") Dim CurrentDirectory CurrentDirectory =…
CodeKeyer
  • 601
  • 1
  • 6
  • 7
54
votes
3 answers

Creating and writing lines to a file

Is it possible to create a file and write lines to it in vbscript? Something similar to echo in bat file (echo something something >>sometextfile.txt). On execution of the vbscript depending on the path of the script would create an autorun.inf file…
Dario Dias
  • 817
  • 6
  • 19
  • 32
53
votes
2 answers

Does VBScript have a substring() function?

Is there a substring() function in VBScript similar to Java's string.substring()?
Carlos Blanco
  • 8,592
  • 17
  • 71
  • 101
52
votes
7 answers

Add item to array in VBScript

How do you add an item to an existing array in VBScript? Is there a VBScript equivalent to the push function in Javascript? i.e. myArray has three items, "Apples", "Oranges", and "Bananas" and I want to add "Watermelons" to the end of the array.
Choy
  • 2,087
  • 10
  • 37
  • 48
51
votes
9 answers

"Continue" (to next iteration) on VBScript

A colleague and I were trying to figure out a way of doing the equivalent of a "continue" statement within a VBScript "For/Next" loop. Everywhere we looked we found people had no way to do this in VBScript without having nasty nestings, which is not…
EKI
  • 838
  • 1
  • 8
  • 15
51
votes
2 answers

Regular Expression Rules in Outlook 2007?

Is it possible to create rules in Outlook 2007 based on a regex string? I'm trying to add a filter for messages containing a string such as: 4000-10, a four digit number followed by a dash and then a two digit number, which can be anything from…
Kevin Coppock
  • 133,643
  • 45
  • 263
  • 274
49
votes
8 answers

How do I include a common file in VBScript (similar to C #include)?

VBScript doesn't appear to have a way to include a common file of functions. Is there a way to achieve this?
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
49
votes
6 answers

Is there "\n" equivalent in VBscript?

I want to use the Replace function in VBScript to replace all line breaks in a string for "\n". I come from Java, so using \n inside a string means a line break. Is there an equivalent in VBScript?
Carlos Blanco
  • 8,592
  • 17
  • 71
  • 101
48
votes
9 answers

Getting the Username from the HKEY_USERS values

Is there a way to connect between the values under HKEY_USERS to the actual username? I saw some similar questions, but most (if not all) talks about C# code, and my need is in VBScript.
modz0r
  • 1,022
  • 1
  • 10
  • 17
47
votes
5 answers

Is it possible to embed and execute VBScript within a batch file without using a temporary file?

People have been embedding and executing VBScript within batch files for a long time. But all the published solutions that I have seen (at the time this question was originally posed) involve writing a temporary VBS file. For example: Embed VBScript…
dbenham
  • 127,446
  • 28
  • 251
  • 390
46
votes
2 answers

How do I return an exit code from a VBScript console application

I Have an old vbs script file being kicked off by an AutoSys job. Can I, and how do I, return an int return value to indicate success or failure?
Philip.ie
  • 1,506
  • 1
  • 17
  • 19
44
votes
5 answers

How do I debug a stand-alone VBScript script?

I have a VBScript script that takes 2 command-line arguments and does some validation. I need to debug this to see how the program is getting executed. I was trying to paste this into Excel (using VBA). However there are some constructs like Const,…
Dheer
  • 3,926
  • 6
  • 34
  • 45
43
votes
4 answers

Try-Catch-End Try in VBScript doesn't seem to work

I'm trying the following code: Try ' DOESN'T WORK Throw 2 ' How do I throw an exception? Catch ex 'What do I do here? End Try but I'm getting the error Statement expected in the catch clause. Does anyone know how I can catch/throw…
user541686
  • 205,094
  • 128
  • 528
  • 886
42
votes
6 answers

What is the best way to iterate through an array in Classic Asp VBScript?

In the code below For i = LBound(arr) To UBound(arr) What is the point in asking using LBound? Surely that is always 0.
Ronnie
  • 8,053
  • 6
  • 34
  • 34