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
42
votes
4 answers

HTTP GET in VBS

Is there a way to perform an HTTP GET request within a Visual Basic script? I need to get the contents of the response from a particular URL for processing.
Justin Bennett
  • 8,906
  • 2
  • 27
  • 30
41
votes
6 answers

What is the difference between VB and VBScript

What is the difference between VB and VBScript?
DNR
  • 3,706
  • 14
  • 56
  • 91
40
votes
7 answers

Running command line silently with VbScript and getting output?

I want to be able to run a program through command line and I want to start it with VbScript. I also want to get the output of the command line and assign it to a variable and I want all this to be done silently without cmd windows popping up. I…
Onat
  • 771
  • 1
  • 11
  • 17
40
votes
5 answers

How to do a single line If statement in VBScript for Classic-ASP?

The "single line if statement" exists in C# and VB.NET as in many other programming and script languages in the following format lunchLocation = (dayOfTheWeek == "Tuesday") ? "Fuddruckers" : "Food Court"; does anyone know if there is even in…
Max
  • 4,965
  • 17
  • 49
  • 64
40
votes
8 answers

Permission denied on CopyFile in VBS

I'm trying to automate pushing a file into my users' home directories, but am stuck on a "Permission Denied" error — is thrown on line 6 here, with the CopyFile call. There are other parts of the script (not shown) that create and copy folder…
Triz
  • 757
  • 2
  • 10
  • 19
40
votes
8 answers

Run Excel Macro from Outside Excel Using VBScript From Command Line

I'm trying to run an Excel macro from outside of the Excel file. I'm currently using a ".vbs" file run from the command line, but it keeps telling me the macro can't be found. Here is the script I'm trying to use Set objExcel =…
muttley91
  • 12,278
  • 33
  • 106
  • 160
39
votes
5 answers

How to call a VBScript file in a C# application?

I need to call a VBScript file (.vbs file extension) in my C# Windows application. How can I do this? There is an add-in to access a VBScript file in Visual Studio. But I need to access the script in code behind. How to do this?
balaweblog
  • 14,982
  • 28
  • 73
  • 95
38
votes
4 answers

Hex-Value in Visual Basic

Can someone just help me refresh my mind? How do you specify hex values in a Visual Basic 6 / VBScript Source? It's not 0xABCD as it is in C++, that's what I can remember... It was something similar... But what?
BlaM
  • 28,465
  • 32
  • 91
  • 105
38
votes
6 answers

VBScript support in Internet Explorer 11

I tried the following HTML page with two scripts: … On Windows…
Ensayo
  • 389
  • 1
  • 3
  • 3
37
votes
3 answers

How to do multiple conditions for single If statement

I'm trying to do two conditions on a single If statement in vbscript. Should be really simple, but it's not working. Something like: If Not (fileName = testFileName) & (fileName <> "") Then Else .... I'm making it two if statements to get it…
James Drinkard
  • 15,342
  • 16
  • 114
  • 137
37
votes
5 answers

Can you debug VBScript in Visual Studio?

Can Visual Studio run and debug VBScript files?
New Bee
  • 991
  • 1
  • 13
  • 24
37
votes
2 answers

Return value from a VBScript function

I have two functions, and I am trying to use the result of one function in the second one. It's going to the else part, but it's not printing anything for "cus_number". How do I get the "cus_number" printed? Function getNumber number = "423" End…
Jill448
  • 1,745
  • 10
  • 37
  • 62
36
votes
1 answer

Does VBScript have Increment Operators

Like Javascript has ++ and += for a increments?
Control Freak
  • 12,965
  • 30
  • 94
  • 145
36
votes
3 answers

how can I check if a file exists?

I want to check to see if a file exists and if it does, I want to open it and read the 1st line, If the file doesn't exist or if the file has no contents then I want to fail silently without letting anyone know that an error occurred.
Cocoa Dev
  • 9,361
  • 31
  • 109
  • 177
36
votes
12 answers

ActiveX component can't create object

I have just installed a third party app on my Windows Server 2008 server and I get the ActiveX Component can't create object message when I try to access using a CreateObject in VBScript. It is definitely installed and exists under "Programs and…
GordyII
  • 7,067
  • 16
  • 51
  • 69