ZDNet UK


Skip to Main Content

ZDNet.co.uk - Winner of Best Business Website 2007
  1. Home
  2. News
  3. Blogs
  4. Reviews
  5. Prices
  6. Resources
  7. Community
  8. My ZDNet

 

ZDNet UK RSS Feeds


IT Jobs

Office applications Toolkit

Make VB apps scriptable

Lamont Adams Builder.com

Published: 26 May 2002 22:32 BST

  • Email
  • Trackback
  • Clip Link
  • Print friendly
  • Post Comment

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.

Next

Previous

1 2


  • Email
  • Trackback
  • Clip Link
  • Print friendly Print with Dell

Did you find this article useful?
63 out of 155 people found this useful


Full Talkback thread

0 comments


Company/Topic Alerts

Create a new alert from the list below:













Related Jobs

QA & Support Analyst - Leading Trading House - London

It is essential that candidates have knowledge / exposure of trading systems, strong integration testing skills, advanced scripting e.g.perl, good ...

Test Analyst - Consultancy - Central London - Contract

You will be responsible for analysing and understanding new and existing software components and requirements, gathering test requirements and ...

Endur Analyst (Endur, Openlink, AVS, Trading,ISEB)

Analyse and prioritise business requirements and problems, and seek effective solutions through improvements in processes and possible enhancements ...

Featured Talkback

Why do so many (virtually all) software packages think that they are so important that they have to be started automatically every time the computer boots? What is the largest number of "speed access", "update check", "camera download" and whatever other background programs you have ever seen running? Of those, how many did you really need?

By: J.A. Watson

Read full story:
Annoying software: a rogues' gallery

Discussions

319762 319762

Eve of Distraction

Saturday 26 July 2008, 4:37 AM

1 comment

Vista Upgrade Blog

Microsoft's pre-modern message puts a...

Over at ZDNet.com, Ed Bott reports a first sighting of Microsoft's eagerly awaited $300 million ad campaign. Already the cause of much speculation, the consensus is that this will be... More

8 comments

A $40 CONSUMER-class router has create...

Believe it or not I don't work in IT, haven't for 7 years. Yes I work with Microsoft's Windows XP Embedded and as a result I have to know a lot about the OS, the kernal, Win API calls... More

Post a comment

Sick Puppy Redo

I generally follow a dispassionate investigative process when trying to discern what happened when a project goes bad. Although its a low priority item, it gets done simply because... More

Post a comment