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
3
votes
1 answer

Retrieving array from dictionary appears to result in a deep copy

The following code adds an array to a dictionary. The array's first element is given a default value. Then, the array is looked up in the dictionary and a new value is assigned to the first element. Yet, upon looking up the array again in the…
Ian Warburton
  • 15,170
  • 23
  • 107
  • 189
3
votes
4 answers

VBscript to monitor system performance leaks memory

I have a simple script that monitors processes' different performance statistics in Windows XP in a loop until it is terminated. Despite my efforts, the script's memory footprint increases in size over time. Any advice is greatly appreciated. …
Ben
  • 31
  • 1
  • 2
3
votes
1 answer

Connecting to SQL Server 2000 with an ASP page using VB?

I am in need of a tutorial or guide that demonstrates how to connect to SQL Server 2000 with ASP pages using Visual Basic.
sujatha
  • 49
  • 5
3
votes
2 answers

How to change font in MsgBox

How do you change font in a MsgBox? X = MsgBox("I want this to be bold times new roman.")
Mario Mario
  • 51
  • 1
  • 1
  • 2
3
votes
3 answers

Can I use FileSystemObject get a single file from a folder using its index?

If there is only one file in a folder, can I pick it up without knowing its name or iterating through the files in the folder? (The code is VBS, but it could be anything, the FSO is the interesting part here.) This didn't work for me: dim fso set…
vacip
  • 5,246
  • 2
  • 26
  • 54
3
votes
2 answers

Dynamic arrays in VBScript with Split(). Is there a better way?

A lot of the scripts I write at my job depend on the creation of dynamically-sizable arrays. Arrays in VBScript make this a pretty arduous task, as one has to Redim arrays every time one wants to resize them. To work around this, I've started making…
Carlos Nunez
  • 2,047
  • 1
  • 18
  • 20
3
votes
2 answers

"Open with" Options through vbscript

Manually we right click on a file and select the "open with" option to open in other format. Now i need to do this through vbscript
Ramesh
  • 31
  • 1
  • 2
  • 6
3
votes
5 answers

Slashes in windows registry key

I'm trying to set full app path of test.exe in registry as name. But it gives me wrong result. Expected Output : Output : This is the code I'm using Dim WshShell, bKey Set WshShell = WScript.CreateObject("WScript.Shell") WshShell.RegWrite…
5x7Code
  • 676
  • 2
  • 9
  • 25
3
votes
4 answers

How do i prevent a VBScript from running standalone?

I'm doing a mash between VbScript and CMD, i can call the VBScript easily with cscript.exe //NoLogo "%~dp0TASK.vbs" >>"%~dp0output.txt" But I need to disable the feature of users clicking on the VBScript and calling all sorts of errors, rather than…
Bloodied
  • 1,004
  • 7
  • 20
3
votes
2 answers

Creating shortcut links (.lnk) from Java

I am writing an installer (launcher) in Java and require the ability to be able to create a shortcut on the users desktop during the process. I am interested in any ideas as the best way to do this. My one option I have considered is using a VB…
cweston
  • 11,297
  • 19
  • 82
  • 107
3
votes
2 answers

I can't close the Excel application

I have this code I wrote in VBScript for wincc, and after running it the Excel application is still running, and the project is not working properly after this script. What can I do to close the Excel app? Here is the script: Dim fso Dim…
3
votes
4 answers

Is Powershell a mature enough tech for enterprise to be using?

Is Powershell a mature enough tech for enterprise to be using? Are its many benefits worth the time and effort to convert existing VBS scripts, or would you only use it for new scripting projects? We are currently using a mixture of vbs and batch…
Anonymous Type
  • 3,051
  • 2
  • 27
  • 45
3
votes
1 answer

WebService returns 409 it seems to be dropping the encoding=utf-8 how could this happen

This VBScript posts an XML document to a webService: Dim xmlhttp, oXML, sourceURL, WSURL, WSUserName, WSPassword, XMLResponse sourceURL = "C:\temp\myFileName.xml" WSURL = "https://mywebServiceURL" WSUserName = "myUserName" …
Mat41
  • 1,287
  • 4
  • 15
  • 29
3
votes
1 answer

trying to connect to remote mysql from .asp page

I am trying to connect to a remote MySQL database from Classic ASP page, when I write VBScript code in it the website is giving HTTP 500 Internal Server Error. I checked with the host they are saying its not an error from their end. Please help…
3
votes
1 answer

vbscript that creates a shortcut to desktop and when user clicks on it prompts them for shut down

I am currently working on a script that will create a shortcut to the desktop and when clicked on, will prompt a user if they want to shut down. When I test my script, it just decides to run automatically. Is there a way I can maybe seperate the two…
Chris G
  • 31
  • 2