Benefits of Using VBA and VBS with MS Outlook
Ever thought of tailoring some extra functionality into your Microsoft Outlook? Even if you program in other languages, such as VB.NET or C#, $, you might want to consider VBA or VBScript. In this article we will take a look at both, and see how well they mesh with Outlook.
With little fanfare, Visual Basic celebrated its 20th birthday this year. In these two decades, the straightforward language and its user-friendly integrated development environment (IDE) have spawned a number of derivatives, all reliant on Microsoft’s Component Object Model (COM) (for instance, the File System Object (FSO) is used to create, read, update and delete files). Amongst them, VBScript and VBA are available for Outlook automation coding.
VBS:
VBS is about as pared-down as a programming language can get. While the Active Scripting language is very limited in functionality, VBS is a lot easier to learn than its competitors. The lightweight approach, coupled with a fast interpreter, makes it a versatile Swiss knife in Microsoft environments.Web pages and standalone scripts occasionally make use of it, but for our purposes, we will look at Outlook forms.
Outlook displays data from individual Outlook items through forms, which are essentially templates for code and UI. Remember all those newsletters with embedded feedback radio buttons, or questionnaires within the client? Emails use a message form; contacts are entered via a Contact form, and so on for different tasks.
The best thing with this route is that you can customize forms by redesigning the standard ones. Of course not all of the existing components are editable; you can code only bits where you see fit instead of starting from scratch.
A form designer is available in all versions of Outlook: for instance, in 2007, if you turn on developer commands, you can see a Design This Form button in the Developer tab. The most basic one is the post form, but if you’ll only experiment, anything from data validation, database synchronization and Outlook item creation is possible.
If you’re running Exchange Server, forms are stored in the Organization Forms library for universal access. If it’s for personal use, simply publish it to a folder or your Personal Forms library and run it from there.
Unfortunately, VBS doesn’t support Outlook’s built-in constants as specified in MSOutl.olb, so you have to make a constant declaration; there are also a number of VBA features absent.
VBA:
Visual Basic for Applications, or VBA, is essentially an implementation and IDE of VB6, embedded into Microsoft Office applications. Aside from building functions and automating processes, it also allows access to low-level functions through DLLs. Transgressing the boundaries of previous application-specific macro languages, its turf penetrates further into the host application, such as the UI, custom forms and dialog boxes. It also has the ability to create import/export filters for miscellaneous file formats. As with VBA, it requires a host but by using OLE automation, it can control other applications in turn.
In Outlook, VBA provides a few useful procedures. There’s the macro, naturally, which runs code on demand; rule procedures handling incoming messages and meeting requests; and event handlers that respond to Reminder Fire events as well as user interactions. Automation-wise, you can use VBA to add appointments, tasks and reminder. Perhaps find a user’s upcoming free time? You can also manipulate text files, for instance deleting multiple junk messages.
Like any programming language snippets, VBA macros are a potential security threat. Outlook now denies unsigned macros from executing at all, which means you should use Self – Cert to generate a security certificate for signing. There’s always the dirty shortcut, namely, lowering the security level from Outlook, but then you run the risk at your discretion.
Distributing VBA:
Curiously, for a language of such prowess, VBA remains firmly anchored in personal use, and distribution within large-scale organizers remains a hassle. While Word and Excel, both document-centric applications, have officially supported methods, Outlook paradoxically doesn’t have one apart from building add-ins. Outlook 2000, that translates to COM, and later versions have implemented .NET framework.
Of course, you could always resort to a no-frills strategy, but there are caveats. Improperly handled, VBA macros are known to simply evaporate into thin air without cause, so remember to back up diligently.
A simple file export is one option, so you can transfer these files to others and ask them to import them correspondingly. But while the code is preserved in the process, it gets tedious if you have many different modules. The Code might not work as expected – for example, the procedures might have to be manually copied into a new ThisOutlookSession if you export from one; procedure name overlaps have to be resolved as well.
Alternatively, you can copy all your Outlook macros simply by duplicating your VbaProject.otm in your hidden system folder. However, beware that the users have to forego their own VBA codes, and in certain versions the user will have to manually enable application-level events. If you are performing a new installation, there’s a similar method. Office Profile Wizard saves all the settings of your machine’s Outlook into an .ops, which can be imported into the Custom Installation Wizard for deployment.
Debugging VBScript
Microsoft provides a very minimal debugger for all Windows Script Host languages, which includes VBScript as well as JScript. A more sophisticated version called the Script Editor is now available in Microsoft 2000 upwards. You can invoke it by pulling a Stop statement in the code, or by setting breakpoints manually. Make sure Visual Studio is installed if you can’t find the latter option in newer versions.
Conclusion
Shortly put, VBScript and VBA denote the ends of two spectra. In VBScript, we have essentially an interpreted language for web and Windows Shell: the fact that it works in Outlook is primarily due to the latter’s implementation of forms. VBA comes packed with a lot more features, and has a large library of functions.