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

18334 questions
174
votes
6 answers

vbscript output to console

What is the command or the quickest way to output results to console using vbscript?
Regmi
  • 2,368
  • 4
  • 22
  • 32
124
votes
4 answers

Pass Nothing from Javascript to VBScript in IE9

I have a framework written in VBScript. Inside some function in this framework parameter of the function is checked for Nothing in If statement and then some actions executed. Code that uses framework written in Javascript. So I need to pass Nothing…
mixel
  • 22,724
  • 10
  • 111
  • 154
87
votes
4 answers

VBScript -- Using error handling

I want to use VBScript to catch errors and log them (ie on error "log something") then resume the next line of the script. For example, On Error Resume Next 'Do Step 1 'Do Step 2 'Do Step 3 When an error occurs on step 1, I want it to log that…
apandit
  • 3,214
  • 1
  • 24
  • 32
85
votes
5 answers

Can I pass an argument to a VBScript (vbs file launched with cscript)?

I have this script saved in "test.vbs": Set FSO = CreateObject("Scripting.FileSystemObject") Set File = FSO.OpenTextFile(workFolder &"\test.txt", 2, True) File.Write "testing" File.Close Set File = Nothing Set FSO = Nothing Set workFolder =…
Peter
  • 1,443
  • 3
  • 12
  • 9
78
votes
2 answers

Using command line arguments in VBscript

How can I pass and access command line arguments in VBscript?
Sunil
  • 1,781
  • 3
  • 15
  • 24
77
votes
8 answers

Any good libraries for parsing JSON in Classic ASP?

I've been able to find a zillion libraries for generating JSON in Classic ASP (VBScript) but I haven't been to find ANY for parsing. I want something that I can pass a JSON string and get back a VBScript object of some sort (Array,…
Mark Biek
  • 135,050
  • 52
  • 150
  • 195
71
votes
8 answers

return only Digits 0-9 from a String

I need a regular expression that I can use in VBScript and .NET that will return only the numbers that are found in a string. For Example any of the following "strings" should return only 1231231234 123 123 1234 (123)…
Brian Boatright
  • 33,940
  • 33
  • 76
  • 102
71
votes
7 answers

Is there a need to set Objects to Nothing

I always read that it is recommended to set objects to nothing, once I am done with them. But I normally use them only in functions inside forms. Isn't the reference lost and memory released when the function scope is left, regardless of setting…
Ramon
  • 931
  • 1
  • 6
  • 10
68
votes
18 answers

What's the environment variable for the path to the desktop?

I'm writing a Windows batch file and want to copy something to the desktop. I think I can use this: %UserProfile%\Desktop\ However, I'm thinking, that's probably only going to work on an English OS. Is there a way I can do this in a batch file…
Scott Langham
  • 53,246
  • 34
  • 122
  • 193
64
votes
7 answers

What does the "On Error Resume Next" statement do?

I came to some VBScript examples, and I saw the statement On Error Resume Next basically at the beginning of the script. What does it do?
Carlos Blanco
  • 8,092
  • 15
  • 63
  • 97
62
votes
4 answers

How to open Explorer with a specific file selected?

I would like to code a function to which you can pass a file path, for example: C:\FOLDER\SUBFOLDER\FILE.TXT and it would open Windows Explorer with the folder containing the file and then select this file inside the folder. (Similar to the "Show…
Jester
  • 2,682
  • 5
  • 27
  • 40
60
votes
6 answers

Can Windows' built-in ZIP compression be scripted?

Is the ZIP compression that is built into Windows XP/Vista/2003/2008 able to be scripted at all? What executable would I have to call from a BAT/CMD file? or is it possible to do it with VBScript? I realize that this is possible using WinZip, 7-Zip…
travis
  • 33,392
  • 20
  • 68
  • 93
58
votes
8 answers

Adding quotes to a string in VBScript

I have this code: a = "xyz" g = "abcd " & a After running it, the value of g is abcd xyz. However, I want quotes around the value of a in g. After running the code, g should be abcd "xyz" instead. How can I accomplish this?
sushant
  • 895
  • 5
  • 17
  • 32
55
votes
11 answers

How to set delay in vbscript

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

Get the type of a variable in VBScript

How do I get the type of a variable using VBScript?
Ash Burlaczenko
  • 21,581
  • 14
  • 63
  • 95
1
2 3
99 100