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

Confusion regarding Dynamic arrays

Ok so I am using the code below to read and display text files into a HTA with VBScript, it loops through all the text files present in a folder (Notes). I have it parsing returns and have removed the file extension from display. What I would like…
Ctrlaltdenied
  • 139
  • 3
  • 11
3
votes
2 answers

How to Move the mouse using VBScript

I am trying to move the mouse using VBScript. I tried to use Sendkeys "{CLICK LEFT , x , y}" and Sendkeys "{MOVETO, 10 , 20}" but it isn't working i also tried to use MouseKeys, so i could move it with the keyboard and therefore use Sendkeys to…
TheGamerLord
  • 79
  • 1
  • 2
  • 9
3
votes
1 answer

Array range error - classic ASP

I am a beginner in Classic ASP. Need to split a string that is formed of many emails delimited by comma and the result to insert (email by email) in a table using additional code I will produce later on. Each record should have a single email…
Gab2021
  • 69
  • 1
  • 11
3
votes
1 answer

Extract time-stamps from log file and save to new Excel file

I want to take time-stamp values from a log file which is in text format in my local machine and save these into an Excel file, using VB Scripting. My log file format is:- 14.000.00.10 - - [07/Mar/2015:16:06:51 -0800] "GET…
Satish Shihani
  • 469
  • 3
  • 17
3
votes
1 answer

Why are null values fetched from ADO Recordset instead of correct ones depending on the order of retrieval? (The DB table has NTEXT values)

I have a DB table with two datetime nullable columns that I need to read from an ASP page using VBScript. This is the code I wrote: Set cmd = Server.CreateObject("ADODB.Command") With cmd .ActiveConnection = conn .CommandType = adCmdText …
Mauro Vanetti
  • 424
  • 1
  • 5
  • 18
3
votes
1 answer

Is there a way using VBscript to check if PS2 keyboard and/or PS2 mouse are attached to a computer running Windows XP or above?

I've a pretty complicated quetions for the community: I have to manage a lab with hundreds of PCs running Win XP Pro SP3. Sometimes students disconnect keyboards and/or mouses from computers so after every class I've to check every single machine…
3
votes
1 answer

VBScript - Capturing output from stdout

I know this has been answered one in another question, but I simply do not understand how it is done. I am trying to get the output of a command line program (Aria2 downloader) into a HTA script so it can be parsed and the download percentage, file…
Ctrlaltdenied
  • 139
  • 3
  • 11
3
votes
1 answer

Sending an email over TLS

I've try send email using TLS and port number is 587 with server name is smtp.gmail.com but always got error "error '8004020e'". I set SSL to false because the port 587 Authentication is TLS. Any wrong in my code? Set objMail =…
Wan Aideed
  • 41
  • 1
  • 6
3
votes
2 answers

TargetPath is blank - on remote drives

I can't seem to get the WShell to return a value for objShortcut.TargetPath, although it passes the full name fine. I've been reading that the WShell can have issues with remote disks, and I had been using an external drive. After testing it on…
994a
  • 31
  • 3
3
votes
0 answers

GNUpg PGP encryption by importing someone else public key using WScript.shell script

When we create our own public key using GNUPG tool, It is working perfectly fine in "WScript.shell" in vb script but when we importing someone else public key using command "gpg --import /public key file path", it is not working from shall…
Jyotish Singh
  • 2,065
  • 3
  • 20
  • 28
3
votes
2 answers

ADODB Excel SQL statement to write to next available row

I am currently able to read and update data in my Excel spreadsheet from using an ADODB connection in my VBScript. That is working smoothly and I understand how to manipulate different columns and different rows with whatever I want. My issue is, I…
mrlemmer11
  • 347
  • 2
  • 15
3
votes
1 answer

Append Procedure to Event upon User Action

Say, I have a procedure which changes the random colour of a cell based on selection change. I know that I can code in a procedure for the worksheet SelectionChange event. I guess Im asking, as in javascript where we can use addEventListeners, can…
Kayote
  • 14,579
  • 25
  • 85
  • 144
3
votes
1 answer

Programmatically create Edge browser instance

Now that Internet Explorer will be discontinued, in Windows 10 Scripting Host (.vbs) how do I create an object to control an Edge instance? Dim oIE Set oIE = CreateObject("InternetExplorer.Application")
Azevedo
  • 2,059
  • 6
  • 34
  • 52
3
votes
1 answer

Command to find Active Drive

I am trying to build a VBScript to automatically run some .exe files. The problem is that the script and the .exe files are on a flashdrive, so it needs to find the current drive letter by itself. I can do it on a batch file using %~d0, but I like…
Wolf
  • 115
  • 3
  • 13
3
votes
1 answer

Is there a way to run VBScript as a command-line argument rather than a file?

Say you have a very simple one-liner VBScript command you need to run, something like getting the day of the month for yesterday: wscript.echo day(date()-1) At the moment, I have to either have a separate .vbs file and then run it with: for /f %%a…
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953