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
26
votes
6 answers

Can I export excel data with UTF-8 without BOM?

I export Microsoft Excel data by Excel Macro(VBScript). Because file is lua script, I export it as UTF-8. The only way I can make UTF-8 in Excel is using adodb.stream like this set fileLua = CreateObject("adodb.stream") fileLua.Type = 2 fileLua.Mode…
P-P
  • 1,670
  • 5
  • 18
  • 35
26
votes
6 answers

Download a file with VBS

Ive got a VBS Script that,generates an url to download a file from a server on my network. I now need to download the file to "C:\rWallpaper\wallpaper.png", the URL is stored in the variable "url". Id like it to work something like wget on linux,…
Arcath
  • 4,331
  • 9
  • 39
  • 71
26
votes
3 answers

Cannot use parentheses when calling a Sub Error 800A0414 VBS

I am getting the 800A0414 error in lines 7 and 12 of this script: Module Module1 Dim p Sub Main() CreateObject("Wscript.Shell").Run("program.bat", 0, True) p = Process.GetProcessesByName("program") If p.Count > 0…
Strong
  • 263
  • 1
  • 3
  • 6
26
votes
9 answers

Read and write into a file using VBScript

How can we read and write some string into a text file using VBScript? I mean I have a text file which is already present so when I use this code below:- Set fso = CreateObject("Scripting.FileSystemObject" ) Set file =…
Maddy
25
votes
1 answer

How to Verify if file exist with VB script

How do I verify via VBScript if the file conf exist under Program Files (i.e. C:\Program Files\conf)? For example if it exists, then msgBox "File exists" If not, then msgbox "File doesn't exist"
yael
  • 2,765
  • 10
  • 40
  • 48
25
votes
4 answers

ASP/VBScript - Int() vs CInt()

What is the difference in ASP/VBScript between Int() and CInt()?
Seibar
  • 68,705
  • 38
  • 88
  • 99
25
votes
6 answers

List object methods and properties

Is there any way to list available methods for created object in VBS? For example: Set IE = CreateObject("InternetExplorer.Application") I want to list available properties of this object, as: IE.AddressBar IE.Application IE.Busy ... or…
theta
  • 24,593
  • 37
  • 119
  • 159
24
votes
5 answers

Get the current temporary directory path in VBScript?

The VB trick to get the path of the current temporary directory: Private Declare Function GetTempPath Lib "kernel32" Alias "GetTempPathA" (ByVal nBufferLength As Long, ByVal lpBuffer As String) As Long fails in VBScript. So?
Fabien
  • 6,700
  • 7
  • 35
  • 35
24
votes
3 answers

VBScript Invalid Character 800A0408 compilation error

I get a compilation error when I try to run the following vbs code from a command prompt in Windows 7. Option Explicit Dim objNetwork, strRemotePath1, strRemotePath2, strRemotePath3 Dim strDriveLetter1, strDriveLetter2, strDriveLetter3,…
Mike
  • 2,561
  • 7
  • 34
  • 56
24
votes
8 answers

Run a vbscript from another vbscript

How do I get a vbscript to run another vbscript? Id imagine its only a few lines of code but not tried doing this before, nothing is passed between the 2, one just needs to call/run the other. For examples the script being run is called…
markdigi
  • 1,242
  • 6
  • 15
  • 26
23
votes
5 answers

Can I pick up environment variables in vbscript WSH script?

Is is possible to read system environment variables in a Windows Scripting Host (WSH) VBS script? (I am writing a VBScript using Windows Scripting Host for task for a Cruise Control and want to pick up the project build URL.)
mike nelson
  • 21,218
  • 14
  • 66
  • 75
23
votes
2 answers

CInt overflow error when value exceeds 100,000+

I am brand new to programming and I am running into some trouble with CInt overflow error. Whenever the value reaches 100,000+ I get a CInt overflow error. This was a practice exercise in my intro to programming class. As far as I can see I coded it…
Tommy Skaggs
  • 239
  • 1
  • 2
  • 3
23
votes
5 answers

Automatic login script for a website on windows machine?

I saw some guy had a file (I guess a batch file). On clicking of the batch file he was able to log in to multiple sites. (Perhaps it was done using VB.) I looked for such a script on Google but didn't find anything useful. I know a bit of C++ and…
munish
  • 4,505
  • 14
  • 53
  • 83
23
votes
6 answers

Script to make content entry into a text file

How do I include some "text" into a .txt format file without opening the same via a script on Windows?
user358485
  • 243
  • 2
  • 3
  • 10
23
votes
4 answers

Why is modulus different in different programming languages?

Perl print 2 % -18; --> -16 Tcl puts [expr {2 % -18}] --> -16 but VBScript wscript.echo 2 mod -18 --> 2 Why the difference?
bugmagnet
  • 7,631
  • 8
  • 69
  • 131