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

Why is VBScript's bitwise And failing in one case?

I'm using VBScript to parse a return code inside some Windows installer stuff. I wanted to confirm that I was doing the bitwise stuff correctly, so I jotted down some Echo statements, and found that one of them didn't produce the result I…
Dogmatixed
  • 794
  • 1
  • 11
  • 33
3
votes
3 answers

Using VBScript how can I check if the Spooler service is started and if not start it?

I'd like to use VBScript to check if the Spooler service is started and if not start it, the code below checks the service status but I need some help modifying this so I can check if it is started. strComputer = "." Set objWMIService =…
StevenL
  • 147
  • 1
  • 1
  • 8
3
votes
3 answers

How can I convert numbers to letters in VBS?

I want to take a number and convert it into lowercase a-z letters using VBScript. For example: 1 converts to a 2 converts to b 27 converts to aa 28 converts to ab and so on... In particular I am having trouble converting numbers after 26 when…
Xeric
  • 53
  • 1
  • 4
3
votes
2 answers

Open an Excel file saved with password for modify

I would like to open an excel file that is saved with a password for modify with VBScript. My current code VBS code is below, which works, but it keeps popping up with boax asking for a password. How can i open the excel spreadsheet with excel…
Jason Samuels
  • 951
  • 6
  • 22
  • 40
3
votes
1 answer

Check if Excel workbook is open using VBScript

I have a vbs script which writes to an Excel spreadsheet. To be able to save the spreadsheet I need to ensure that it is not already open. Can someone suggest the best way to do this? Some research: I tried to create a Word Tasks objects to show…
DavidA
  • 33
  • 1
  • 4
3
votes
2 answers

Run script using Task Scheduler with user context complete silent with no popup or cmd flash

I have tried to run the script using command cmd.exe /c Start /min powershell.exe -windowstyle hidden -file .ps1 But getting a CMD window for a fraction of a second. I need it to run completely hidden.
v_b
  • 175
  • 1
  • 4
  • 10
3
votes
1 answer

How to create a Windows Service from .vbs file?

I have a .vbs file which I execute using cscript. Now that the script is sable; I want to run in background all the time. Hence, I want this .vbs file to run as a service. How do I create (install) it?
vintrojan
  • 1,277
  • 1
  • 12
  • 22
3
votes
3 answers

vbs taskkill by name

I'am trying to find how to close a process using it's title. I found the command: taskkill /fi "WINDOWTITLE eq the_title_of_the_windows" and it works great. When I try: oShell.Run "taskkill /fi "WINDOWTITLE eq the_title_of_the_windows"", , True I…
Joe Lara
  • 33
  • 1
  • 1
  • 5
3
votes
1 answer

VBScript BrowseForFile() Function - how to specify file types?

I found this function on GitHub and it's working great for what I need, but I can't figure out how to specify file type. It seems to default to "all". Function BrowseForFile() With CreateObject("WScript.Shell") Dim fso : Set fso =…
Drivium
  • 537
  • 6
  • 24
3
votes
2 answers

How to delete (remove) a specific line of a text file based on the line number?

I have a simple script that deletes the first n lines of a text file. Const FOR_READING = 1 Const FOR_WRITING = 2 strFileName = "C:\scripts\test.txt" iNumberOfLinesToDelete = 5 Set objFS = CreateObject("Scripting.FileSystemObject") Set objTS…
Djamille
  • 71
  • 1
  • 1
  • 9
3
votes
3 answers

Running cscript.exe from C# .ashx does not execute code in vbscript file

EDIT I added in some error handling to my .vbs file and it is indeed a permissions issue (I now get a "Permission Denied error"). However, supplying my credentials in the web.config tag does not seem to have any effect. Also when…
Brandon Boone
  • 16,281
  • 4
  • 73
  • 100
3
votes
1 answer

Change default printer based on location

I would like to write a VBScript to change the default printer, based on which printer is connected. I have a laptop that I use at work and at home, and I would like to run this script when starting windows so the default printer is always the…
Tester101
  • 8,042
  • 13
  • 55
  • 78
3
votes
1 answer

How do I consume Classes i have stored in Application?

I have defined a class like this: Class Foo Public SomePublicProperty Public Function init(p_somePublicProperty) set init = Me SomePublicProperty= p_somePublicProperty End Function End Class And then consumed that in…
jaybeeuu
  • 1,013
  • 10
  • 25
3
votes
3 answers

How to save workbook and handle TITUS (or any other document classification add-in) popup?

I'm creating a script in HP UFT 12 which performs grid data validation against a CSV file and saves the results in a Excel file with two worksheets. I'm using Excel for this because it is much more clear for the user, as it allows cell formatting,…
Victor Moraes
  • 964
  • 1
  • 11
  • 28
3
votes
1 answer

How to designate a property as the Default Property for a VbScript class

Given that one can define a class in VB Script, is there any way to specify a default property for that class? e.g. Given Class MyClass Private myName Public Property Get Name() Name = myName End Property end class Is there…
Binary Worrier
  • 50,774
  • 20
  • 136
  • 184