Saturday, December 29, 2007

VBScript Projects in Visual Studio

The debugger in Visual Studio is a very nice tool for debugging VBScripts. Visual Studio will also allow you to have your scripts organized and if you have a version control system like, CVS, Subversion or Source Safe it will allow your to keep track of changes made to your scripts. Visual Studio also provides some Intellisense for COM objects and the basic VBScript functions.

However I have not been able to find a Visual Studio Project Template to create VBScript projects. Some people suggest setting up a custom build action, but as far as I have searched, nobody has actually tryed it. Therefore I have written this blog to share my findings.

As there is no template for VBScript Projects, I have tryed to find the most basic template provided by Visual Studio. This is the Makefile Project. Now choose new Project from the menu, and create a project as shown in figure 1:

Figure 1: Create a Makefile Project.

The wizard will pop up and allow detailed configuration of the project (Figure 2), however we do not need it, so just click Finish.

Figure 2: Wizard for the Makefile Project.

The template has created 3 folders for typic C++ projects and a readme.txt file as shown in figure 3. We don't want any of those so delete them right away.

Figure 3: Default folders and files in the Makefile project to delete.

Now add a new VBScript file to the project as shown in figure 4.

Figure 4: View of solution with the new vbs file added.

Now right click the Project and choose Properties. This will open the Property Pages dialog shown in figure 5. Choose the General Page, and change the "Configuration Type" from "Makefile" to "Utility".

Figure 5: Change the Configuration Type to Utility

Next choose the "Debugging" Page and set the "Command" field to "cscript.exe" and set the "Command Arguments" field to "//X MyScript.vbs". This will fire the command "cscript.exe //X MyScript.vbs" which will execute script in debug-mode.

Figure 6: Set the actual command to run when debugging the script.

Now add the code shown in Figure 7. Note that you have some Intellisense if you press CTRL+Space.

Figure 7: Here you can see that the Intellisense feature works.

Now press Ctrl+F5 for "Start without Debugging" to launch the project. The reason why it is to be started with out the debugger is because Visual Studio will try to attach to the cscript.exe process, however it is the underlyling script that we want to debug.

As we have set the parameter //X the scripting engine will throw a "Script Breakpoint" exception, which the Visual Studio Just-In-Time Debugger will catch and ask you what to do about it, by showing you the dialog in figure 8. Select the current Visual Studio instance and press "Yes".

Figure 8: Visual Studio JIT debugger.

Now Visual Studio will attach to the script and you will be able to debug it as you are used to. On Figure 9 you can see that I have placed a breakpoint which I have hit. You can also see the value of the variable "I".

Figure 9: View of the script being debugged.

This is my first shot a making a VBScript project in Visual Studio. After having done this you can experiment with different setup.
As you will soon discover you will probably want to have more scripts inside the solution. This will make it neccesary to change the project configuration everytime you switch script. I suggest either having more configuration (like debug and release) where you specify the script to run, or to setup a custom build action for each script. Having a custom build action will require you to press CTRL+F7 to compile the active file that you are editing. If you select "Start Without Debugger", the build actions for all files will be activated, and you will be asked to select a debugger for each script.


Jack Gillespie said...

I'm attempting to use your process for debugging vbs scripts and I'm not having much success.

Everything works fine up to the point of invoking the vs2005 jit debugger. I am never presented with the ability to select vs2005 as shown in figure 8 of your blog.

I'm using vs2005 professional. Any suggestions?

armin said...

I have the same issue as Jack. The only options in JIT are:

New instance of Microsoft Script Editor

New instance of Visual Studio 2005

I tried both but didn't get any results. Any help is appreciated.

tripppr said...

I believe you may not be starting the project using the CTRL-F5 sequence mentioned in the post. If you just F5 or "Run" your app, the debugger attaches to the cscript.exe instead as mentioned in the post.

David Willett said...

Thanks for your great article!

I was also wondering how to debug a VBScript that doesn't run standalone, but whose Subs and Functions are called from an external .exe (legacy, written in VB6). Can it work with Attach to Process somehow?

-------------------------- said...

thanks, great article

Rob said...

Can you please explain the advantage here?

Anyone can debug VBA scripts in Excel or Word or VS very easily without all this work.

Simply open your favorite VBA editor (I prefer Excel) and create a new Module. Make all of your library references for the objects you are going to use. Declare your variables as strongly typed. Write your code and make sure it works - debug as you would debug any program.

All the dot/intellisense will work for you.

When you are finished, change your object references to CreateObject(). Delete all the types for your variable declarations. Copy/paste into Notepad and save as .vbs.

It's not rocket science - like this appears to be. You get all the advantage of the IDE, but none of the setup pain you are talking about here.

Adili said...

You need to enable JIT debugging in Visual Studio for scripts.

raj said...

i am unable to add vbscript file... i dont see that kind of file in new items list.. can anyone tell me how to do that

Wanaco said...

I am having the same issue. Cannot add a vbs file to project!

I am using VS2008

Stanley D Lewry said...

surely just make a text file and change the name of it.

am i being dim or does someone just need to make a template for this?

I am using vs 2010

J Bishop said...

This Rocks, thank you and it's perfect for debugging SMTP Sink Scripts on Windows 2008!

Marc said...

Thanks for your article.

I have just a little problem. The intellisense does't works.
How active it ?

PS : Sorry for my english (i'm french !)

Wayne said...

Great details, but now got an issue. Was working ok yesterday, came back today and open project, CTRL+F5 and it just runs all the code and not hit the breakpoints :(

Even recreated new project again from scratch and copy/paste vbs into new file but no debugging. What could have happen?

Pulling my hair out - be great if could shed some light.

Anonymous said...

Thanks a million! Works great!

Dip said...

Thank you so much. This is working for me in visual studio 2008. Before that i was using scrape notepad comes with microsoft windows.

St├ęphane Barizien said...

You saved my day.

Thx a million.

okaypc said...

Thanks. I use this in build MSI-project. vbs as custom actions.

David Guiney said...

I couldn't get the debugger choice to come up when I hit ctrl+F5, so I used wscript.exe instead of cscript.exe (since this is what I would use to debug through the cli) and it worked ok.

Other than that, thanks a million, I hated debugging through the cli, so this is going to make my life much easier.

trailway said...

I am using Visual Studio 2013 and this approach worked for me. Thank you for taking the time to write this post.

Omer Javaid said...


its a great work. Is there any way to use intellisense?

George Archibald said...

Nice work!

It also works in VS2015. What I really want is a way to save it as a template so I could use it for multiple projects. Like the have said, it does only give you the advantage of using VS IDE as a primary editor, but if you are a MS Developer, it is what you are used to and I prefer to have one IDE. It is stupid that Microsoft shouldn't support it properly. Yes, it is not as powerful as Powershell, but Powershell has some really crappy syntax. At least VBScript is readable.

Yuriy Zhubil said...

thnx for sharing this,
just prepared my vbs-project in VS2013.