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
2 answers

Security implications of Request.ServerVariables("REMOTE_ADDR") vs Request.ServerVariables("HTTP_X_FORWARDED_FOR")

Let's say we're tracking the end-user IP for a web service: ip = Request.ServerVariables("HTTP_X_FORWARDED_FOR") If ip = "" Then ip = Request.ServerVariables("REMOTE_ADDR") End If I've read that this is the best method of retrieving end-user IP…
user19471
  • 381
  • 5
  • 16
3
votes
1 answer

"Invalid procedure call" error when passing a variable as argument vs. passing the return value of another function

I get an error: invalid procedure call or argument: 'AddRange' when passing a variable to ArrayList.AddRange(), but the code works fine when I pass the return value of a function instead, i.e. My list: Dim Foo Set Foo =…
Micha Wiedenmann
  • 19,979
  • 21
  • 92
  • 137
3
votes
2 answers

activate task view programmatically windows 10

Is there a way to activate task view programmatically in windows 10? There was a way to activate Flip3D programmatically in win7 using the following copied from VBScript SendKeys CTRL+LWIN+TAB? CreateObject("WScript.Shell").Run "rundll32 DwmApi…
blakedesign
  • 31
  • 1
  • 2
3
votes
4 answers

Sending email from webpage using Outlook

I have a webpage that has a button that sends a letter on the page to an email recipent. Currently we are use Lotus Notes and with VB script, we are able to create an object of Lotus Notes and one of the properties for this object is PutInFolder. …
Michael
  • 152
  • 2
  • 13
3
votes
1 answer

Replace with regex keeping part of the pattern

In the example below, how to preserve part of a pattern? The pattern search must include closing tag as spans elsewhere must not be targeted. Example: remove only from some text : regEx.Pattern = "[^>]*" hr2 =…
Paul
  • 117
  • 2
  • 9
3
votes
1 answer

Remove span using VBScript regex

How do I remove a span tag using a VBScript regex? For example, the following HTML should be reduced to just the h3 opening tag:

It must be by regex, as it is part of a regex…

Paul
  • 117
  • 2
  • 9
3
votes
0 answers

ASP classic recordset error '8007000e' Out of memory

I have a script which exports a recordset to excel. My recordset has 139 columns. I can see in fiddler that table header of the excel is created but when it comes to write the values I face this error: Microsoft Cursor Engine error '8007000e' Out of…
Leila
  • 232
  • 6
  • 15
3
votes
0 answers

validating a digitally signed vbscript in C#

Need example code for validating a signed vbscript in C# using WinVerifyTrust. Per MS - you can sign a vbscript - https: //technet.microsoft.com /en-us/library/ee176795.aspx I grabbed an example from msdn written in C and successfully…
kpirkl
  • 31
  • 1
3
votes
1 answer

Can on marshal a string[] to a safearray variant?

When I marshal my function returning a string[] as UnmanagedType.Struct with SafeArraySubType = VarEnum.VT_ARRAY as in namespace StackOverflow { [ComVisible(true)] [Guid("4BDC43D4-8FD7-4F58-BEE5-E57C3C144C1B")] public class Array { …
Micha Wiedenmann
  • 19,979
  • 21
  • 92
  • 137
3
votes
1 answer

Running a .vbs script through R, scheduled in Task Scheduler

I have a VBS script that takes an Excel file and saves it as a PDF. I call this vbs as within an RScript that is automated through Task Scheduler. The RScript runs just fine when I run it in R manually. However, when I have it scheduled in Task…
GreenboyCJ
  • 33
  • 4
3
votes
2 answers

How to initialize multiple variables together?

I need to initialize several variables with the same value in VBScript. The only way I can find is for example x = 5 : y = 5 : z = 5. Is there a way similar to x = y = z = 5?
AhmedWas
  • 1,205
  • 3
  • 23
  • 38
3
votes
1 answer

VBScript in Classic ASP

I'm working with a very weird version of VB...it doesn't want me telling it what is what, it wants to figure that out on its own. In C# I can easily hard code an array...not so much in this VB. I would like to create a hard coded array while calling…
GaidenFocus
  • 353
  • 4
  • 12
3
votes
2 answers

HTA create vbscripts on the fly

Is there a way in HTA to read a file containing vbscript and dynamically add it to a subroutine ? so than when i click on a button that subroutine gets called having the vbscript which is read from the file having it ? I have tried reading the file…
Mani kv
  • 153
  • 2
  • 19
3
votes
4 answers

VBS unzipping - object required: 'objshell.NameSpace(...)'

I know very little about bash or vbs. I am trying to make a script that will automatically unzip a zip called 'dungeon.zip', which contains a little game I programmed. I want to unzip it to a folder called dungeon in the same directory that the zip…
Bretsky
  • 423
  • 8
  • 23
3
votes
1 answer

A class contains an array of objects. Do I need to set each of them to nothing on terminating the containing class?

I am working with code something like the below. An array of BasketItemViewModel is created in the scope of the Basket class by looping DB results and creating a view model from each row. When I ditch the Basket class, and Set it to nothing, am I…
Martin Hansen Lennox
  • 2,837
  • 2
  • 23
  • 64