Shortcut routine procedures by simple codes
Beyond signing the cheques for the software, few solicitors pay much attention to the word processing systems which we use daily. In this article, I’d like to lift the hood on the sheet of paper which you see on screen when you look at a program such as Word, and look instead at the underlying nuts and bolts of a thing called Visual Basic for Applications (VBA), which lets you write customised programs within Word.
Recording Macros
Most secretaries will be familiar with the programming device called a macro. A macro is a way of recording a series of actions so that the same series can be repeated quicker next time. As macros are an important part of VBA, here’s a short example. You’ll recall when Legal Post was introduced, most firms advertised the fact by sending their letters out with a footer along the lines of, “Please note that we are no longer members of DX. Our new LP number is ED 1234.” Short and sweet, but it was still a pain for secretaries to type that in at the end of every letter, so the smart ones will have set up a macro to do it for them, and they will have done it like this:
In Word, click Tools | Macro | Record New Macro. A dialog box pops up, called “Record Macro”. You can then give the macro a name, say, ‘writeLPdetails’, and specify that it should be available for use in all documents by selecting ‘All documents (Normal.dot)’ in the ‘Store Macro in’ select menu. Normal.dot is the template for the sheet of paper you see when you open a new blank document in Word.
Set a way of running the macro later on by clicking the ‘Assign macro to keyboard’ button, which pops up another dialog box called ‘Customize Keyboard’. In the text-box called ‘Press new shortcut key’, press an appropriate combination of keys, such as Alt + L at the same time. Later on, once the macro has been written, pressing Alt + L will run it. Still in the ‘Customize Keyboard’ box, click ‘Assign’ to confirm the key combination, then click ‘Close’.
A small dialog box will then pop up with two buttons on it, one to stop recording and the other to pause recording. From that point on, until you press one of those two buttons, each time you press a key or use the mouse to select a menu option, the action will be recorded in the macro. So, at this point you first of all press Ctrl + End to put the cursor at the very end of the document, then press Enter a couple of times to take a couple of new paragaphs to make sure the text will be on a separate line away from the rest of the letter, then you type in your new LP details. Then you click the Stop Recording button. The macro has now been recorded and you can test it by clicking Alt + L, whereupon the macro runs, and puts the cursor at the end of the page, takes a couple of new paragraphs, then types your text, then stops.
Remembering key combinations for several macros becomes awkward. It is possible to run a macro by other means, such as clicking Tools | Macro | Macros, to see a list of available macros. You can also click View | Toolbars | Customize, and, after reading the associated help-files, set up customised drop-down menus like in the tools menu at the top of Word, with each macro accessed by pressing a button in a menu.
The Visual Basic editor
Although the ‘writeLPdetails’ macro is a relatively trivial example, macros of this type which simply involve recording keystrokes and menu selections are useful in situations involving lengthy and repetitive typing of boilerplate text or formatting of documents. Any Word command contained in the File, Edit, View, etc options at the top of the screen, and any available set of keystrokes, can be recorded in a macro.
For the purpose of this article, however, the foregoing macro provides some raw material for showing what VBA is, and for that, you need to look at the Visual Basic editor. There are several ways of opening the VBA editor, but seeing as we want to look at the connection between the above macro and VBA, the easiest way is to click Tools | Macro | Macros. You’ll get a dialog box called ‘Macros’, with a list of all available macros. Click the macro called ‘writeLPdetails’ to highlight it. Then click the button marked ‘edit’ in the ‘Macros’ dialog box.
At this stage a new program called the Visual Basic Editor should open, and figure 1 is a screen shot of part of it:
At this point, you are looking at what lies behind a sheet of paper on screen in Word. On the left hand side of the screen is a window called ‘Project Explorer’. If this window is not visible, it can be made visible by clicking ‘View | Project Explorer’. Project Explorer sets out in a hierarchical tree structure the component parts of the sheet of paper which you see on screen. Each open document is referred to as a Project, and in this particular screen shot, you’ll see that I had two projects open, Normal.dot and the sheet upon which I was writing this article. Within the project Normal there are references to ‘This document’, which is the particular instance of Normal.dot which is open at that particular time, and there is a reference to five forms which I have made available to Normal.dot on my computer, and, finally, there is a reference to ‘New Macros’, which is where the macros I’ve written are stored.
Looking at the right hand side of the screen, there is a block of text, as follows:
Sub writeLPdetails()
Selection.EndKey Unit:=wdStory
Selection.TypeParagraph
Selection.TypeParagraph
Selection.TypeText Text:= _
“Please note that we are no longer members of DX.Our new LP number is ED 1234. “
End Sub
This block of text is the macro which you recorded earlier. Each line is a line of Visual Basic code which corresponds to the key presses you made when recording the macro. When you told the computer that you were going to record a macro called writeLPdetails, the Visual Basic editor started behind the scenes, and first of all, within the New Macros section of the blank document template Normal.dot, it set up a sub-procedure by writing the words ‘Sub writeLPdetails()’ and ‘End Sub’ to denote the start and end points of the macro and also to assign a name to that sub-procedure so that it can be called by name later on. Then, within the start and end points, it translates your key presses into Visual Basic code to represent the various key presses.Visual Basic has a number of phrases and words which it uses to pass instructions to the computer in a way which the computer can ‘understand’ and act on. In this particular example ‘Selection.EndKey Unit:=wdStory’ is Visual Basic code which is understood by the computer to mean put the cursor at the very end of the current document, and as this is such a simple example, you can figure out what the rest of the code thereafter means.
While this is a trivial example, it is important to understand that if a repetitive process can be reduced to a pre-defined set of keystrokes, the whole thing can be stored in a single sub-procedure which can then be run by referring to its name. The macro here simply moves the cursor around a bit and types a short text string to the page, but it should be apparent that you can record macros which automate more extended processes such as writing entire pro-forma documents to the page, and indeed, in legal situations involving more than one document, several documents can be produced from a single macro by recording a page-break into the macro to separate each document. You should also be aware that if you are recording a new macro, you can, during that process, call and run existing macros, so that, for example, if you write two macros to draw up two separate documents, you can then draw up a third macro which calls the other two macros and runs them both together.
Visual Basic has keywords to represent any command which can be keyed manually into the computer, and, thus, practically any routine procedure can be represented in code in the Visual Basic editor. The full set of keywords can be found by clicking View | Object Browser, which is a library of the whole Word object model. In programming terms, everything on screen from the complete Word program through it’s various menu options down to a full stop is an ‘object’ which has various properties and methods, each of which can be manipulated by using Visual Basic code. This means that, past this trivial example, Visual Basic can be used to program in a highly customised way everything from writing some text onto the page to opening entirely new documents or document templates, writing text to them from customised dialog boxes and printing a specified number of copies from specified printer-trays
In the second part of this article, Scott Cownie explains how to manually edit VBA code
In this issue
- Firms lack capital ambition
- Rural law firms facing issues of succession
- Acquiring masters degree can be rewarding business
- Laying firm foundations for future growth
- Registering a trademark makes patently good sense
- What makes a good partner?
- Claims information before merger options
- Shortcut routine procedures by simple codes
- Jamieson arrives with reforming agenda
- Refining details of new civil legal aid scheme
- Round the houses
- Take care with the crave
- Essentials of the anonymous Budget
- Changing duty on commercial leases
- Scottish Solicitors’ Discipline Tribunal
- Planning for the future – simplicity itself?
- Website reviews
- Book reviews
- Commercial property transactions common standard