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
31
votes
7 answers

VBScript How can I Format Date?

I want the date to look like MM-DD-YYYY instead of MM/DD/YYYY.
Cocoa Dev
  • 9,361
  • 31
  • 109
  • 177
31
votes
3 answers

Error checking for NULL in VBScript

I have the following VBScript in a Classic ASP page: function getMagicLink(fromWhere, provider) dim url url = "magic.asp?fromwhere=" & fromWhere If Not provider is Nothing Then ' Error occurs here url = url & "&provider=" &…
Vivian River
  • 31,198
  • 62
  • 198
  • 313
31
votes
4 answers

MsgBox "" vs MsgBox() in VBScript

I'm trying to write a VBScript and I'm using functions such as Randomize, and MsgBox. I'm curious as to what is the difference of using () and not using them. For example: Randomize - This line works. Randomize() - This line works also. MsgBox…
user1862387
  • 311
  • 1
  • 3
  • 3
30
votes
2 answers

How does garbage collection work in JavaScript?

How does garbage collection work in JavaScript? Is it similar to .NET garbage collection? And is it because the implementation of garbage collection in VBScript is bad that people avoided it and established a preference for JavaScript as their…
Ali Tarhini
  • 5,278
  • 6
  • 41
  • 66
30
votes
1 answer

Cannot use CreateObject from VB scripts on Windows 7 x64

When I try to create a COM object in VB script on a Windows 7 (64 bit) machine, I always get the following error: "ActiveX component can't create object 'xxx.xxx'. Code: 800A01AD I have applications that use the COM object without any problem. I…
Dave Lee
  • 363
  • 1
  • 4
  • 8
30
votes
5 answers

Is there ways to create optional arguments to functions in vb script?

Is there ways to create optional arguments to functions in vb script allowing you to write functions something like... myFunc("happy") myFunc("happy", 1, 2, 3) myFunc("happy", 1) etc.
Rob Segal
  • 7,515
  • 12
  • 43
  • 72
30
votes
7 answers

Launch programs whose path contains spaces

I need to launch programs in my local system using VBScript. But I am having trouble with the syntax. This is what I am using right now - Dim objShell Set objShell = WScript.CreateObject( "WScript.Shell" ) objShell.Run("iexplore") Set objShell =…
anonymous
29
votes
3 answers

Detect paste in input box

I have a input box, and I would like to use vbscript or javascript (no jquery) to capture the paste event.
Itay.B
  • 3,991
  • 14
  • 61
  • 96
29
votes
17 answers

How do you keep debug code out of production?

It happens to the best of us. Particularly when dealing with languages without built in debugging capabilities such as breakpoints and watched variables, these bugs bite developers. Debugging code, alerts and Response.Writes, show up in production…
Thomas Langston
  • 3,743
  • 1
  • 25
  • 41
29
votes
4 answers

VBScript, purpose of colon?

What is the purpose of the colon? e. g.: Dim objConn : Set objConn = OpenConnection()` Is the colon used to combine the two statements into one line? I just want to be sure.
burnt1ce
  • 14,387
  • 33
  • 102
  • 162
28
votes
6 answers

How to run vbs as administrator from vbs?

Can anyone help me with running vbs from itself but with administrator rights? I need rename computer with Windows 8 via VBScript, but it's possible only if I run my script through administrator command line (CMD → Run as Administrator →…
Daphnis
  • 437
  • 1
  • 5
  • 14
28
votes
15 answers

Response.Write vs <%= %>

Bearing in mind this is for classic asp Which is better, all HTML contained within Response.Write Statements or inserting variables into HTML via <%= %>. Eg Response.Write "" & vbCrlf Response.Write "" &vbCrLf Response.Write "
Jon P
  • 19,442
  • 8
  • 49
  • 72
27
votes
6 answers

Check if an Object exists in VBScript

I'm maintaining a Classic ASP app written in VB Script by an outside company long, long ago. I have an array of imagefile paths, like so: dim banners, arrKeys, i set banners=CreateObject("Scripting.Dictionary") banners.Add "banner1.jpg",…
Armstrongest
  • 15,181
  • 13
  • 67
  • 106
27
votes
4 answers

Breaking a String Across Multiple Lines

I wrote a script which is connecting to an Oracle database to select multiple entries in a specific table. The statement looks like this: rs.open "SELECT PATH301 FROM NC301B WHERE EDIPROC like 'P30_' AND (LF301M > 0) AND (PATH301 NOT LIKE…
rel0aded0ne
  • 451
  • 2
  • 5
  • 17
27
votes
2 answers

Run Command Line & Command From VBS

I need to run a command to copy a file from one location to another through Command Prompt using a vbs file. this is what I have however it keeps throwing an error at me. 'Dim oShell Set oShell = WScript.CreateObject ("WScript.Shell") oShell.run…
user1590368
  • 379
  • 1
  • 4
  • 4