Home > Random, Tips > Debug VBScript with Visual Studio

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:

  1. Save your code as a VBS file. Don’t forget to call Main (or your main) function as wscript won’t automatically call main.
  2. Open a command prompt.
  3. Navigate to the location of the file.
  4. Type: WScript.exe FileName.vbs //D //X
  5. You should be prompted to select a debugger, select a new instance of either Visual Studio 2003, 2005 or 2008.
  6. Visual studio will open up and break at the very first line.
  7. 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:

Command Prompt for Debugging VBScript from Visual Studio

Command Prompt for Debugging VBScript from Visual Studio

Once there, type in the following:

1
 wscript Example.vbs //D //X

You should then be presented with a dialog similar to the following:

Visual Studio Debugging VBScript

Visual Studio Debugging VBScript

Once you make a selection, you should get something like the following:

Debugging VBScript from Visual Studio

Debugging VBScript from Visual Studio

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.

Frank Random, Tips , , , ,

  1. May 23rd, 2009 at 14:11 | #1

    Hi, nice posts there :-) thank’s for the interesting information

  2. Rushabh
    July 16th, 2009 at 10:46 | #2

    Thanks for the information

  3. November 7th, 2009 at 01:42 | #4

    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

  4. November 7th, 2009 at 04:27 | #5

    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 :-) .

  5. November 8th, 2009 at 18:48 | #6

    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

  6. Bill S
    December 22nd, 2009 at 13:10 | #7

    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.

  7. Abhishek
    January 21st, 2010 at 00:41 | #8

    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

  8. January 21st, 2010 at 13:06 | #9

    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

  9. R. N. Chanon
    January 25th, 2010 at 10:18 | #10

    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.

  10. January 25th, 2010 at 14:25 | #11

    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

  11. David
    February 10th, 2010 at 18:12 | #12

    You are my hero! I’ve hated using vbs for that very reason.

  12. February 13th, 2010 at 09:08 | #13

    Hey David — I’m with you but, honestly, even with this I still hate VBScript. :-)

    Regards,
    Frank

  1. No trackbacks yet.