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

Error running Excel Macro VBScript

Running a job from AutoSys and I am getting an error. VBS runs an excel macro. VBS code : Option Explicit Dim xlApp, xlBook Set xlApp = CreateObject("Excel.Application") On Error Resume Next set xlBook = xlApp.Workbooks.Open("Z:\Confidential…
Eoin2211
  • 911
  • 2
  • 19
  • 39
3
votes
3 answers

How can I call java from asp?

I'm not familiar with ASP, but am helping someone out with their website as a side project. I am trying to call Apache FOP (a java application) from ASP using VB. I have seen simple examples using the GetObject('java:...') constuct, but I don't…
molson
  • 81
  • 1
  • 6
3
votes
2 answers

How to encode? response.cookie with asp classic - vbscript?

Im trying to set a cookie with the value /sv/en and the / ends up like %2F in the cookie, so I wonder how to encode or whatever you have to do to write a / instead. Im using this to set the cookie now. Response.Cookies("googtrans")="/sv/en" …
Claes Gustavsson
  • 5,509
  • 11
  • 50
  • 86
3
votes
2 answers

Convert multiple .xls files to .xlsx in ssis

I have a folder that receives multiple excel files in .xls format. I need to change the format type to .xlsx in order to load the excel data into SQLvia SSIS. I know how to rename the file using "File System Task" but that works for a specific file.…
Syed Jafri
  • 51
  • 1
  • 4
3
votes
1 answer

Response.Write does not show & encode html tags correctly

I faced a confusing issue with my new installation of Windows 10 and IIS that comes with it. The issue with Classic ASP is that Response.Write and also <%=varname%> outputs html tags so that all  < signs and >signs in the tags are replaced by the of…
M.Y.
  • 549
  • 1
  • 3
  • 23
3
votes
3 answers

Output ADODB.RecordSet as JSON

I'm trying to change my application so that it outputs JSON instead of HTML when it makes an AJAX request for some data. I have an ADODB RecordSet. I need to loop through it row-by-row and add/change/remove different values. Then I need to take all…
tylerl
  • 1,160
  • 1
  • 19
  • 41
3
votes
1 answer

How to run VBS function with parameters from Java and assign result to variable

I have this excel macro: Function Calculate_Something(StartDate As Date, EndDate As Date) As Double //some math is here, not important Calculate_Something = Result End Function And I want to pass my dates to this macro, execute it in my Java…
westman379
  • 493
  • 1
  • 8
  • 25
3
votes
1 answer

UFT Image used as button

I am trying to run the below code, but it throws and error when executing the last line of code. Browser("name:=401K Calculator").Page("title:=401K Calculator").WebEdit("name:=age").Set 26 Browser("name:=401K Calculator").Page("title:=401K…
3
votes
1 answer

How to run a Microsoft SQL Server Agent Job from VBScript

I have an ERP system that allows me to attach VBScript to a button on a customized window. I would like to be able to run this script so that it fires off a SQL Server Agent Job on the server (SQL Server 2008). I've been looking in to this for a…
Jon Rose
  • 129
  • 2
  • 8
3
votes
1 answer

How to write `select ... FOR XML` queries that generates an xml and an xsd so, that they are kitchen ready for an SQLXMLBulkLoad?

I'm looking for the most easy way to export a table (or a part of it) to an xml file and then import this xml file into a corresponding table in some other database. The principle I found is very simple: Export: on the source database I generate…
mma
  • 381
  • 2
  • 15
3
votes
1 answer

VBScript works great when I run it from command prompt but not from within Inno Setup

I have a VBScript that I wrote that works great! It does everything that I need it to perfectly when I run it from a command line in Windows 10. cscript.exe "C:\SetupTempPath\MyScript.vbs" "First Param" 1234 "Third Param" For some reason when I…
Arvo Bowen
  • 4,524
  • 6
  • 51
  • 109
3
votes
4 answers

current working directory in a vbscript invoked by a drag & drop operation

When I was trying to get elevated rights for my batch script, when I found two related SO questions How to request Administrator access inside a batch file How can I auto-elevate my batch file, so that it requests from UAC administrator rights if…
Wolf
  • 9,679
  • 7
  • 62
  • 108
3
votes
2 answers

Looping VBScript

I'm trying to run a VBScript checking the time until it reaches 22:00 (10PM), and then runs shutdown.bat. I always get errors such as "'loop' without 'do'". Can anyone look at my code and see if there's a way to fix it? Do If Hour(Time()) =>…
H3153N
  • 31
  • 2
  • 2
3
votes
1 answer

Read POST data in AJAX call

I have some Session values that I am constantly changing via Ajax calls. I can't seem to get a handle on the POST data to process it and set the values. What I am passing to it here is an array of strings like is shown in my code below. Here is…
user1601513
  • 153
  • 2
  • 10
3
votes
1 answer

Continuous ping with updates to Excel from list of hostnames via VBScript

The following VBScript will display the status of a single ping command from a list of hostnames to an Excel spreadsheet: Set objExcel = CreateObject("Excel.Application") objExcel.Visible = True objExcel.Workbooks.Add intRow = 2 '# Define…
Vandal
  • 903
  • 1
  • 14
  • 27