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 a descriptive value for WMI Win32_Processor.Family property instead of an index

The below simple VBS example retrieves CPU caption, architecture and family from WMI: s = "" For Each Item In GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\Root\CIMV2").InstancesOf("Win32_Processor") s = s & "Caption = " &…
omegastripes
  • 12,351
  • 4
  • 45
  • 96
3
votes
1 answer

Merge multiple PDF files with VBScript

I'm looking for a way to merge multiple PDF files into one with VBScript, only with Acrobat and Distiller - without using any third-party software. Although I really searched hard for a solution on the web, I couldn't find one. Can someone please…
Sam
  • 565
  • 6
  • 23
3
votes
1 answer

How do I work with user-defined types?

Every time I try to pass a UDT around to other functions I get this compile error: "Only user-defined types defined in public object modules can be coerced to or from a variant or passed to late-bound functions." The only internal function that I've…
Hao Zhang
  • 147
  • 9
3
votes
3 answers

Check a string for non-numeric characters in VBScript

I'm working on a Windows application that's written in VBScript and I need to check a string for any non-numeric characters, specifically anything a-z. I realize I could probably do this using the InStr() function in conjunction with a loop that…
donut
  • 9,427
  • 5
  • 36
  • 53
3
votes
1 answer

Capture HTA window close event using VBScript

I have been playing with using an hta as a means of getting familiar with HTML. I added code to minimize all windows when the hta window opened, but I have not been able to figure out how to restore all windows when the hta is closed. Any…
Adam Turner
  • 55
  • 1
  • 4
3
votes
1 answer

subscript out of range <4,1>

I am implementing post commit hook in svn repo to trigger jenkins build but getting one exception which is I think in the commit.vb file. I know this is so simple question but I haven't work on vb so no idea about. Following this tutorial -…
3
votes
1 answer

Why doesn't WScript.Shell.ExpandEnvironmentStrings work with %CD%?

In the command line you can output the current directory using echo %CD% like this this: The Windows Scripting Host provides the ExpandEnvironmentalStrings method which can be used like this: Dim objWshShell : Set objWshShell =…
rory.ap
  • 34,009
  • 10
  • 83
  • 174
3
votes
2 answers

How to change file permissions with WMI?

I'm want to do the equivalent of what is described here from a script. Basically, I want to take ownership of the file, and set the permissions to OWNER/Full Control. It seems to me that using WMI from a vbs script is the most portable way. That is,…
itsadok
  • 28,822
  • 30
  • 126
  • 171
3
votes
0 answers

Standalone script to execute stored procedure with parameters and iterate through the recordset returned

I am writing a VBScript which executes a stored procedure with parameters which should return a recordset from the SQL Server. I am able to execute simple select statements and using recordset object. The stored procedure returns a single column…
3
votes
2 answers

Why doesn't FileExists support wildcards?

Consider this example VBScript fragment: Dim fs Set fs = CreateObject("Scripting.FileSystemObject") If fs.FileExists("D:\Folder\File*.ext") Then ' Finds nothing! fs.CopyFile "D:\Folder\File*.ext", "D:\OtherFolder\" fs.Deletefile…
Michel de Ruiter
  • 7,131
  • 5
  • 49
  • 74
3
votes
0 answers

WMI Query Output to variable

I'm trying to run some commands based on OS and x86 vs. x64. When running the below code the variable remains blank. I can echo the output and see the query and property seem to be correct. Any idea why that string isn't being assigned tot he…
David
  • 31
  • 1
3
votes
1 answer

how to prevent null byte injection from querystring

The Nessus Vulnerability Scanner was run against a legacy code website. There's a lot of advice about how to prevent null byte injection attacks with PHP but I cannot find anything about fixing this in classic ASP with VBScript. Here's the scanner's…
John Adams
  • 4,773
  • 25
  • 91
  • 131
3
votes
1 answer

JScript: identifying whether double quotes are passed to a WSH script

There are situations when it is important to identify whether double quotes are passed as arguments to a WSH script. For example because they should be passed to another executable to be run. The standard parsing functions/objects: objArgs =…
antonio
  • 10,629
  • 13
  • 68
  • 136
3
votes
1 answer

Read the return value from VBScript into SAS

I have a SAS program which requires many input files whose names vary each month. Rather than update the individual file names in Windows Explorer to hardcoded aliases in SAS or copy and paste the new file names into macro variables, I want SAS to…
Lorem Ipsum
  • 4,020
  • 4
  • 41
  • 67
3
votes
2 answers

Search and Replace a number of characters in Excel using VBscript

I need to search and to replace a specific part of a string in an Excel sheet. Here is my code and I don't know how I can exactly search this part in each Cell.value. my_new_string = "abc" For each objSheet1 in objworkbook2.sheets If…
sourh
  • 131
  • 2
  • 13