PDA

View Full Version : How do I set up Visual Studio to debug .vbs and/or .wsf scripts?



Sukotto
05-21-2007, 08:52 AM
How do I setup Visual Studio (2005) to give me the same debugging features as the Excel/Word/Outlook VBA editors?

My job for the next few months is to create and maintain .vbs and .wsf scripts that drive MS Office applications. (e.g. a scheduled script that opens a series of Office documents, runs embedded vba functions, and do some other stuff)

When programming in Excel or whatever, I can test functions, set breakpopints, use the "Immediate" window, etc....

I would really like to step through scripts and check variables in these driver scripts but can't figure out how to make it happen. So I have to edit the script, save, then run the script manually via start->run or on the commandline. :banghead:

How do I set up Visual Studio to allow the same debugging features?



I'm runinng VS2005 on Windows 2k, interacting with Office 2k and IE 6.

mvidas
05-21-2007, 09:17 AM
Sukotto,

When I need to debug a .vbs script, I copy the whole thing to a standard module in vba (with the exception of a couple vbscripts that use classes), put all the Dim statements at the top, and usually enclose the main code in a sub called "SubMain" or something similar, and keep the Dim statements out of it (so they'll be public variables). Any separate subs/functions will still be separate, and then run/stepthrough SubMain as needed. I've never
used .wsf scripts so I can't say for sure about those, but for my VBS files it works great. Sometimes I'll write them in vba using the same idea, then just copy/paste into notepad (though usually i just write them there). Using the VBE makes the vbscript nicer looking too, since it capitalizes/formats it.

Matt

mvidas
05-21-2007, 09:22 AM
Just as a quick/simple example of what im referring to, if my vbs file looks like:

dim i, Cntr
cntr=1
for i = 1 to 10
doubleavariable cntr
next
msgbox cntr

function DoubleAVariable(aVar)
avar=2*avar
end functionIn VBA it would look like:Dim i, Cntr
Sub SubMain()
Cntr = 1
For i = 1 To 10
DoubleAVariable Cntr
Next
MsgBox Cntr
End Sub

Function DoubleAVariable(aVar)
aVar = 2 * aVar
End Function

Sukotto
05-21-2007, 01:14 PM
What editor are you talking about here? There does not seem to be a "VBA
" File/Project type in Visual Studio 2005.

mvidas
05-21-2007, 01:23 PM
My apologies, I was actually referring to the Visual Basic for Applications VBE that comes with office (excel VBA, word VBA, etc)

I don't have VS2005 here at work so I can't say for sure, but any VB-based project should work. I havent looked at vs2005 in a while though so I could be thinking of an earlier version (I usually use vb6's visual studio).