Create macros for Office applications

Fed up with wasting time in Word and the other Microsoft Office applications? Then make your mark with macros

Written by Paul Wardley

Macros are stored lists of instructions that can be executed by clicking a button or using a shortcut key, and they’re available in every version of Microsoft Office.

In their simplest form, macros can be created by recording keystrokes and mouse clicks while performing a set of actions, but with the help of Visual Basic for Applications (VBA) ­ a programming language built into recent versions of Office ­ there is no limit to what macros can be told to do.

This Masterclass demonstrates how to use both recorded macros and VBA to automate everyday Office tasks. Although we’ve used Office 2003 to demonstrate how macros can be created, edited and implemented, the same techniques can be applied to earlier versions of Office and to Office 2007 ­ see ‘Macros in Office 2007’ at the foot this page.

All the programs in Microsoft’s Office 2003 family contain a simple macro recorder for keystrokes and mouse clicks, and this works fine if all you want to do is automate a repetitive process to save time, but the weakness of macros recorded in this way is that they can only do things that are possible using the standard commands and menus built into the program that created them. Macros created using Visual Basic for Applications don’t suffer from this constraint, so they can be used to create new commands and features that Microsoft itself didn’t include.

Macros created by recording actions are automatically saved as VBA programs, so they can be adapted and improved using the same Visual Basic Editor that is used to create VBA programs from scratch. This editor works identically in every Office application, so by learning to use it in one of them you’ll be able to use it in all the others.

Recording a macro
Using Word, here’s how to record a macro that sorts any document that’s in the form of a list. Start by typing a short list of items into a new document, then save it for later use. As always when recording macros, it’s a good idea to first work out the required sequence of actions and write them down. It’s also better to use keyboard shortcuts whenever possible, saving mouse clicks and movements only for those actions that absolutely require them. This limits the chances of mouse actions being misinterpreted when replayed.

The keystrokes for sorting a list and placing the cursor at the beginning of the document are: Ctrl and Home, Shift and Ctrl and End, Alt and A, S, Enter, Ctrl and Home. Try these out before recording the macro to see what each action does. To create the macro, open the Tools menu and select Macro, Record New Macro.

Give the macro a name with no spaces in it and optionally assign it to either a toolbar or a keyboard shortcut. Word macros can either be stored for use only within the current document or placed in the global template (Normal.dot). In this case, use Normal.dot to make the macro universally available. Click OK, then perform the list of commands. In the tiny toolbar that is displayed while this is going on, click the Stop Recording icon (it’s the one on the left) when finished.

The list has now been sorted and the macro has been created and saved. Close the document without saving the changes to the list, then reopen it. The list can now be re-sorted by invoking the macro you just created from its toolbar or by using its keyboard shortcut. If neither of these was assigned, press the shortcut key combination of Alt and F8 to display the Macros dialogue box, then highlight the new macro and click Run.

Macros in Office 2007
The sample macros used in this Masterclass work with Office 2007 as well as earlier versions of Office. Although Office 2007 has no Tools menu, the Macros dialogue box can be summoned from the Developer ribbon. If this is not available, click the Office button and select Word Options (or the options for whatever program you’re using), and in the Options dialogue box tick the Show Developer tab in the ribbon. Click OK. To save Office 2007 documents complete with macros for use with earlier versions of Office, use the 97-2003 format instead of the default.

reader comments

related articles

 

Microsoft updates Excel security patch

Last week's fix caused performance problems 20 Mar 2008

related whitepapers

today's top stories

Coding moves with the times

We examine how software development has evolved to better serve the changing needs of business, and speaks to IT leaders who are delivering significant benefits to their organisations by using the latest programming methods 15 Oct 2008

Agile framework simplifies offshore development

Case study: Getronics business application services 15 Oct 2008

Computing launches all-new IT jobs site

Updated Computingcareers.co.uk provides enhanced feature for jobseekers 14 Oct 2008

Q&A: BT Business head of SaaS, Chris Lindsay

BT's head of software-as-a-service explains the benefits of the on-demand delivery model and how the current economic downturn could force firms to re-evaluate how they buy software 14 Oct 2008

WiMax: Threat or opportunity?

We examine the merits of WiMax and its benefits relative to other wireless technologies in our latest video 13 Oct 2008

Advertisement

Newsletter signup

Sign up for our range of FREE newsletters:

Existing User

Newsletter user login:

Jobs

Related jobs

Job of the week

Job alerts

Sign up here

Find your next job

IT Salary Checker

Check salary here

Advertisement

White papers

Search white papers

Top categories

VPN, Extranet and Intranet Solutions

WAN/ LAN Solutions

Network Security

Interoperability-Connectivity

Grid/ Utility Computing

Latest poll

Are you worried about your job prospects in IT over the next 12 months?

Are you worried about your job prospects in IT over the next 12 months?

Will the economic crisis affect your job prospects?

Previous poll results

Latest audio and video articles

Remote workerVideo

WiMax: Threat or opportunity?

We examine the merits of WiMax and its benefits relative to other wireless technologies in our latest video 13 Oct 2008

programming codeVideo

The definitive guide to software development

Five key trends and five best practice tips to help you improve your programming capabilities 09 Oct 2008

Latest in-depth articles

Features

Enter the dragons' den

Getting an innovative IT product off the ground takes cash, commitment and a lot of patience 15 Oct 2008

TimepieceFeatures

Coding moves with the times

We examine how software development has evolved to better serve the changing needs of business, and speaks to IT leaders who are delivering significant benefits to their organisations by using the latest programming methods 15 Oct 2008

Advertisement

Primary Navigation