Debug VBScript with Visual Studio
For a project that I work on at at my primary employer, we use VBScript inside of DTS packages. Until this project, I’ve not had too much experience with VBScript. What I’ve found most frustrating about the language is the inability to step though the code… until a recent discovery. I’ve found that if I save the target code to my local machine as a VBS file and execute it with WScript.exe, I can attach a debugger to it.
I’ve discovered this by poking around and I don’t know too much about WScript.exe. I’m sure if you Google it, you can find more about it.
For this to work, you’ll need a copy of Visual Studio installed. I am not sure if any of the express editions will work. Drop a line in the comments if you find it will. As a quick walk though:
- Save your code as a VBS file. Don’t forget to call Main (or your main) function as wscript won’t automatically call main.
- Open a command prompt.
- Navigate to the location of the file.
- Type: WScript.exe FileName.vbs //D //X
- You should be prompted to select a debugger, select a new instance of either Visual Studio 2003, 2005 or 2008.
- Visual studio will open up and break at the very first line.
- Step though the code.
For an example, save the following as “Example.vbs” on your desktop.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | MsgBox "Starting Script!" Dim i : i = 0 Const C_Max = 100 Dim sPrimeNumberList : sPrimeNumberList = "Prime Numbers: " & vbCrLf For i = 0 to C_Max If IsNumberPrimeNumber(i) Then sPrimeNumberList = sPrimeNumberList & cStr(i) & ", " End If Next MsgBox sPrimeNumberList MsgBox "Script Completed." Public Function IsNumberPrimeNumber(ByVal iNumber) Dim bIsPrime : bIsPrime = True For j = 2 To iNumber\2 bIsPrime = iNumber Mod j > 0 If Not bIsPrime Then Exit For Next IsNumberPrimeNumber = bIsPrime End Function |
Open a command prompt and navigate to your desktop:
Once there, type in the following:
1 | wscript Example.vbs //D //X |
You should then be presented with a dialog similar to the following:
Once you make a selection, you should get something like the following:
From here, I’ll assume you are familiar enough with Visual Studio to play with the debugger and learn how it works. When debugging VBScript, I’ve found that you don’t have all the options that you might be used to such as Step In To, but for me simply having this much is a life saver.
To get around the step-in-to thing, I simply use Run to Cursor. It works like a charm.
Update: I just found that (at least on this computer) and in Visual Studio 2003, I have all my standard code-stepping buttons available. I’m not sure why on my work computer I only have Step-Over but either way, this is still a life saver.




Hi, nice posts there
thank’s for the interesting information
Thanks for the information
No problem. Glad it was helpful
Unfortunately, it does not work with VisualStudio2005-pro on W2k:
I don´t have any project open, and the VBS code is not included in a project.
After calling the instance of VS2005-pro, just Wscript.exe is available as object in the object browser, but no VBS script code is displayed.
Sincerely
Rolf
Ups I found my fault: Neither the free MS_ScriptDebugger nor VS load a script file for debugging, if there is a fault detecting during “compilation” ( check before execution ). Somehow your file “example.vbs” causes an error, so it does not load. With proper VBS files, VS loads VBS files
.
Hey there Rolf,
The file may have been created or saved in a format not readily supported by the debugger. This is the most popular article on this site, hundreds of people tried it so the file in itself works. Either way, I’m glad you were able to get it to work.
Best wishes,
Frank
This is great info but the one thing I am trying to find out is whether all this will work if you only have VB express installed. (Or one of the other free express languages, etc.). If this gets answered please post it ….. you will be the first place ever to answer that question and thus be first on my search.
Hi,
I tried to debug VBScripts this way and could do it if the VBScript had no errors.
But when the VBScript had even 1 error, Visual Studio opens but the VBScript does not appear and thus can’t be debugged.
Can u help me out on this.
Regards,
Abhishek Jain
I will try to help, certainly. I’d also suggest Stackoverflow.com — someone there might be able to help too.
As for the problem on hand, can you first make sure that the script is in a place such as Documents. I’m concerned that permissions may be interfering. Also, can you post which operating system you are using? This could also make a difference. I had tested this on WinXP and Vista. Finally, which version of Visual Studio do you have? Do you have multiple versions of VS installed, per chance?
Regards,
Frank
Although this approach has worked in the past, I haven’t been able to launch a debugging session supporting a VBScript script on Windows 7 and the Visual Studio 2010 C# Express Edition.
I’m sorry to hear that… Unfortunately, I don’t use Windows 7, so I can’t test this out nor can I find a solution.
Although, if you are using the express editions, you might want to try VbsEdit instead. I found this via: http://www.robvanderwoude.com/vbstech_debugging.php#Editor
You are my hero! I’ve hated using vbs for that very reason.
Hey David — I’m with you but, honestly, even with this I still hate VBScript.
Regards,
Frank