Make VB apps scriptable

Daily Newsletters

Sign up to ZDNet UK's daily newsletter.

ANALYSIS
Yes, that's right, the technology that was supposed to replace Network Administrator's reliance on BAT files with something a little more modern might have some usefulness for the "real" developer. You could make a case that one reason for the popularity of Microsoft's Office suite is that all the applications featured in it include a simple development environment that allows technical users to automate repetitive tasks. Visual Basic for Applications (VBA) provides this environment in Office, and you can license VBA for use in your applications if you can afford the licensing fees that Microsoft would demand from you. In this article, I'll show you how the WSH and the Windows Script Control can provide a cheap but serviceable alternative to VBA for providing automation scripting in a Visual Basic application. Getting your bearings
The Windows Script Control encapsulates the Active Scripting Engine, which is the heart of WSH, into a UI-less ActiveX control that you can easily use from VB. The control allows you to execute entire scripts or fragments of script written in any language supported by the WSH. You likely already have this control installed on your machine; it's named msscript.ocx and is usually located in your System32 folder. If you don't have it, it's available for download from Microsoft. The control includes some documentation, but in my experience, it's somewhat incomplete and inaccurate. The best way to get your bearings is, unfortunately, to simply play around with it and see what works and what doesn't. Having done a little of that myself, I created a test project that illustrates some of what's possible with the scripting control, which you can download here. I'll be referring you to parts of that project throughout this article. Running script
To run some script using the scripting control, you have several options:
  • The Eval method, which is similar to the VBA Eval function, allows you to evaluate expressions that range from simple arithmetic to complex expressions containing embedded script commands or object method calls, and returns the result to your application.
  • The ExecuteStatement method will execute any complete script statement.
  • You can use a combination of the AddCode and Run methods to load and execute custom script routines. This is the most powerful method, and is the one I'll cover in the remainder of this article.
Introducing ScriptingDemo
The ScriptingDemo app consists of a single form that displays a list of script files that it finds in the application's directory, a textbox for arguments to script procedures, and a single textbox that an executing script can use for output. When the application starts, it instantiates a custom collection object, colCustomers, which loads data from the Customers table of the SQL Server Northwind catalog to use as play data for our sample scripts. Figure A shows the application's main form at runtime.
Figure A
ScriptingDemo main form at runtime
When a script file is selected, the app reads in the code line-by-line and adds it to the scripting control's environment using the AddCode method. Clicking on the Run Script button causes the scripting control to attempt to execute a procedure with the same name as the selected file, sans extension.

Post your comment

In order to post a comment you need to be registered and logged in.

You can also log in with Facebook. Log in or create your ZDNet UK account below

  • Login

Will not be displayed with your comment

By signing up for this service, you indicate that you agree to our Terms and Conditions and have read and understood our Privacy Policy. Questions about membership? Find the answers in the Community FAQ

Get ZDNet UK's daily newsletter

Enter your email address to sign up

ZDNet UK Live

JCB33

How dare film makers, artists or anybody that invests in creativity stop us pirating their works for free. I want to be able to walk into my local...

3 minutes ago by JCB33 on ACTA stumbles in Germany
Moley

@GrueMaster. I prefer horses for courses rather than one size fits all. I, and I suspect most other computer users, do not really wish to have...

2 hours ago by Moley on A tale of two distros: Ubuntu and Linux Mint
greycynic

The product that scares me every time I have to use it is the Office 2007 version of Excel. The first bug that I found was applying the median...

2 hours ago by greycynic on Ten flawed products that derail productivity
GrueMaster

Nice review and very informative. One thing I'd like to add (in reply to whs001's 1st question), the main reason to have the same interface from...

4 hours ago by GrueMaster on A tale of two distros: Ubuntu and Linux Mint
Frederick Wrigley

I'be been using Mint 12 since the RC came out, and I am far more happy with the Cinnamon, the Mate, and, yes (with extensions), theGnome 3...

4 hours ago by Frederick Wrigley via Facebook on A tale of two distros: Ubuntu and Linux Mint
bdantas

Excellent article. One small correction, though--although a fresh installation of Linux Mint 12 will, indeed, provide the user with a version of...

5 hours ago by bdantas on A tale of two distros: Ubuntu and Linux Mint
Alan Ralph

In related news, the ISPs club together to get the members of the Home Affairs Select Committee (ya goofed on that part, ZDNet UK) copies of "The...

6 hours ago by Alan Ralph via Facebook on MPs urge ISPs to take down terrorist material
Alan Ralph

In related news, the ISPs club together to get the members of the Home Affairs Select Committee (ya goofed on that part, ZDNet UK) copies of "The...

6 hours ago by Alan Ralph via Facebook on MPs urge ISPs to take down terrorist material
Moley

For Gnome 2 die-hards, it is possible to add icons to the bottom panel (or top top panel, if you prefer) which provide the exact Gnome 2...

7 hours ago by Moley on A tale of two distros: Ubuntu and Linux Mint
ramwellian

Your comments would seem pretty naive and immature. Your 'solution' appears to be, "gee, let's all just give in to the hackers and give them...

7 hours ago by ramwellian on Cloud computing security: no more oxymoron?
BugStalker

"Interesting thought ... If you installed Win7 as a dual boot on a machine that previously only had Linux, and it wrecked your Linux installation,...

7 hours ago by BugStalker on Windows 7 Declares War on GRUB
whs001

This is an excellent summary of Ubuntu and Mint and the interface differences between them. Most such articles take a very partisan position for...

7 hours ago by whs001 on A tale of two distros: Ubuntu and Linux Mint
Moley

@ewallace. Not so clear. Anyone can obtain the text, for example from here http://www.ustr.gov/webfm_send/2379. I support ACTA so long as it and...

7 hours ago by Moley on ACTA: Facts, misconceptions and questions
45283

I think WinRT is fantastic. I just wish it was an option for people that didn't want to go through Microsoft's App Store with its attendant...

10 hours ago by 45283 on Why Windows 8 needs architectural hygiene for WOA
Burn-IT

Nine people? £30m? Who's back pocket is that lot going in? And IF they say it is for new buildings, what about all the ones the government has...

12 hours ago by Burn-IT on Police set to launch three £30m e-crime hubs
ewallace

Just to be clear, nobody knows what is in the text of ACTA, here is a photograph of the text of ACTA http://twitpic.com/8h9iju as submitted to the...

12 hours ago by ewallace on ACTA: Facts, misconceptions and questions
fgvrg56

Unfortunately main issue is that ASUS is refusing to accept that they make some mistake on this version of asus Transformer prime. 1 - GPS sensor...

13 hours ago by fgvrg56 on Asus Eee Pad Transformer Prime Wi-Fi & GPS problems?
Ben Woods

@Marcus A fair question. Just talked with Archos which said it was working on an announcement for next week....

14 hours ago by Ben Woods on Archos confirms G9 Ice Cream Sandwich update schedule

Any update on this, considering the claimed "first week of February"?

15 hours ago by via Facebook on Archos confirms G9 Ice Cream Sandwich update schedule
apexwm

Bill Goodrich : Just as al_langevin pointed out, with Windows Server 2008 there is no Services for Macintosh anymore. It's gone, not available....

23 hours ago by apexwm on Windows Server 2008 drops the ball for Mac compatibility