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

How to list all WMI classes having methods using VBScript?

Using VBScript, how can I list all WMI classes that have methods?
Salvador
  • 16,132
  • 33
  • 143
  • 245
3
votes
2 answers

Search folder for files that contain a server name and extension, then select the last modified file, and grab date in the filename

Trying to use VBScript to do this. I have a folder that has multiple files that include the server name with an extention of .vib. All the files show up as servername.vm-T
3
votes
1 answer

Digitally Signing scripts in [VSCODE]

In our environment the two main scripting languages that we use are VBScript/WScript and PowerShell. Our PowerShell is set up so that all scripts have to be digitally signed. Inside the PowerShell ISE we added a menu item that saves the current…
CFo
  • 109
  • 2
  • 13
3
votes
2 answers

How to delete XML comments

I wrote a VBScript to remove all the comments in a XML file. But it didn't work correctly. The script reads from a .xml file, generates a XML file which removes comments and another temp XML file. Set argus = WScript.Arguments If argus.Count = 0…
Rui Lu
  • 33
  • 4
3
votes
1 answer

Open Excel and Run Macro using VBScript

I'm trying to use this VBScript to open a file and run a macro. Ideally I'd open it from my personal workbook, but happy to run from anywhere as a compromise. Option Explicit On Error Resume Next RunExcelMacro Sub RunExcelMacro() Dim xlApp Dim…
REdim.Learning
  • 655
  • 2
  • 14
  • 32
3
votes
2 answers

Windows Product Key - Different answers from different techniques

Following on from this question (which asks how to retrieve a Windows Product Key), there seems to be conflicting information obtained from the various methods. This question asks why is there a difference when they should all return the same…
3
votes
0 answers

A trappable error (C0000005) occurred in an external object

I am calling a stored procedure named INSERT_BYTE with a string more than 4000 bytes . The code is a s follows : Dim Conn Set Conn = Server.Createobject("ADODB.Connection") Conn.Open "Driver={Oracle in XE};DBQ=xe;UID=biotpl;PWD=biotpl;" …
Christopher Marlowe
  • 2,098
  • 6
  • 38
  • 68
3
votes
1 answer

Synchronous MSXML2.XMLHTTP producing "The data necessary to complete this operation is not yet available."

In a VBScript, having instantiated: On Error Resume Next Set xmlhttp = CreateObject("MSXML2.ServerXMLHTTP") I am doing a sequence of synchronous HTTP PUTs like this: xmlhttp.open "PUT", TheURL, False xmlhttp.setRequestHeader…
feetwet
  • 3,248
  • 7
  • 46
  • 84
3
votes
1 answer

How to Implement An Event in Microsoft Script Control?

All docs i've read on MSScriptControl say that it can respond to events of objects added to it. the script control allows you to write script that will automatically fire when an event on an object occurs. …
johny why
  • 2,047
  • 7
  • 27
  • 52
3
votes
1 answer

call a vbs file in a removable drive to run

I need a little explanation on specifying path to mounted removable disk. My batch code... wmic LOGICALDISK where driveType=2 get deviceID > "C:\Windows\Temp\wmic.txt" cls for /f "skip=1" %%b IN ('type C:\Windows\Temp\wmic.txt') DO (xcopy…
jkdesigns
  • 41
  • 7
3
votes
1 answer

unexplained differences in wuapi between VBScript and C++

When trying to use wuapi to query Windows 7 Enterprise SP1 for updates, the attached VBScript sample returns different results than the attached C++ sample. The VBScript includes updates for Microsoft Office, which is what I want. During my…
3
votes
3 answers

How to write a script for organizing my desktop windows? (WinXP)

I would like to write a script that moves the currently open Windows to a certain location. Ideally, my program would look like this: var window = FindWindow("Winamp"); window.setPos(1140, 900, 160, 124); // set x, y, width and height window =…
StackedCrooked
  • 34,653
  • 44
  • 154
  • 278
3
votes
1 answer

Vbscript Trim function

I have a script that reads in a comma delimited text file, however whenever I use Trim(str) on one of the values I have extracted in the file, it won't work... My Text File: some string, anotherstring, onelaststring some string, anotherstring,…
tarki
  • 67
  • 1
  • 7
3
votes
2 answers

Declaring Variables Memory Leaks

I am wondering what would be the most correct way to deal with memory when using VBScript. Should declare all variables right before I use them? The beginning of the program? I understand global vs local, however in my script all variables are…
tarki
  • 67
  • 1
  • 7
3
votes
1 answer

Issue with System out of Memory in vbscript while reading a big file

I am trying to do some text replacement in vbscript. The problem is that the file is 150MB big. Below is the function which just deletes the header rows and gets rid of blank lines. As I had suspected that it is giving me an error "out of memory" at…
imba22
  • 651
  • 1
  • 13
  • 25