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

Batch file conversion to vbscript

I need to convert a batch file to vbscript but am unfamiliar with both. If I can understand what is going on in the batch file I can work out the vbscript easy enough. Problem is the batch file runs a few cscript commands which is supposed to have…
user256260
3
votes
2 answers

How to check the given path is a directory or file in vbscript?

I want to check the path given by user is a directory or a file in vbscript. Is there any regular expression or the other ways I can do this ? Any help would be great.
Anh Nguyen
  • 299
  • 4
  • 10
  • 21
3
votes
1 answer

How to get date and time in one place given the date and time in another place

Is there an object that's part of standard Windows 7 that allows us to, within VBScript, get the local time in a given city/time zone? One approach would be to use something like SWbemDateTime to convert a given time zone to UTC then re-convert back…
Franzl
  • 687
  • 1
  • 6
  • 13
3
votes
3 answers

VBScript to move file with wildcard, if it exists

I am attempting to create a script that checks for the existence of archived eventlog files and, if any files exist, moves them to another folder. Running this script does nothing and gives no errors. I believe the wildcard in the If statement is…
user3157550
  • 31
  • 1
  • 1
  • 2
3
votes
2 answers

Running code from VBA to VBScript back to VBA

I'm trying to figure out a way to call a VBScript function using vba in Excel, and then pass a value back to excel-vba. See below VBA within Excel Sub RunTest() Dim objString as String 'Begin Pseudocode objString = Call VBScript…
Tronald
  • 1,524
  • 1
  • 13
  • 28
3
votes
2 answers

How to Create Folders and Subfolders in %AppData%

First of all thank you very much for all the help I’ve found over 3 Vbscript that has save my life during this last six months. I’m new to Vbscripting and I’m currently working on getting a Vbscript that create folders and copy a file at the same…
Leonavas
  • 35
  • 1
  • 1
  • 5
3
votes
3 answers

How to return exit status from an HTA (vbscript) to the calling Batch file

How to return exit status from an HTA (vbscript) to the calling Batch file ? My batch file code @echo OFF echo Configuring Test... call AppConfigurationEditor.hta call TestConfigurationEditor.hta 1.If user click on CANCEL button on the first HTA I…
Hanumanth
  • 1,097
  • 1
  • 9
  • 14
3
votes
2 answers

unknown Method in xPath run by VBScript

the error message is msxml3.dll: Unknown method. /Record/CelloXml/Integration/Case/ServiceEvent[-->last()<--]/Service/Comment My code looks like this and the error is on the case NEW Case OLD works fine with [0] in there 'On Error Resume…
Malachi
  • 3,182
  • 4
  • 27
  • 45
3
votes
1 answer

vbScript System.Collections.ArrayList Object does not support... list.Add

I am having a problem resolving the oList object with my CreateObject("System.Collections.ArrayList") The error I get in vbscript "Microsoft VBScript runtime error: Object doesn't support this property or method: 'list.Add'" Based on this tutorial…
user1741874
3
votes
1 answer

How to detect Firefox user agent?

I am working on an application where I am required to make legacy code, which has been designed primarily for Internet Explorer, work with Firefox. The problem I have hit is iframes nested within a table structure do not expand to the full height…
Jonathan
  • 1,715
  • 11
  • 14
3
votes
1 answer

How to save SAPI text to speech to an audio file in VBScript?

I have the following VBScript code for text to speech conversion: Set objVoice = CreateObject("SAPI.SpVoice") objVoice.Speak Inputbox("Enter Text") I want to save the speech to an audio file. How can I do this?
user3065043
  • 47
  • 2
  • 8
3
votes
1 answer

vbscript error path not found while using movefolder method

I am fairly new to vbscript, and attempting to write a script that will pick up month and year stamped folders (2012_04) and move them to a year stamped folder (2012). I am getting a Path not found error though when I attempt to move the folder, and…
3
votes
2 answers

Hex to binary using VBScript

Is it possible to convert a Hexdump to binary using VBS? I made a program that reads any file as Byte array, then it converts it to Hex. In output Hexdump looks like this 00 00 00 0A 4D til the end. Now i want to know if i can convert it back to…
Haytham Mtair
  • 107
  • 2
  • 4
  • 13
3
votes
2 answers

IsEmpty or IsNull or IsNothing?

Can someone help me determine which I should be using? Here is the situation - I am pulling a value from a column in the Data Table. If there is anything in that column, I set the data to a variable and take an action. If the column is blank, I…
James Craig
  • 448
  • 3
  • 9
  • 20
3
votes
1 answer

Excel: How to call functions in Sheet modules from VbScript

Using VBScript, I'm trying to run a subroutine that resides in one of the code modules. Now this particular sub calls several other subs/functions, including few that reside on the sheet modules. And on calling these subs residing in Sheet Modules,…
user2696565
  • 536
  • 7
  • 16
1 2 3
99
100