Microsoft Word provides a great editing, word processing, and document management environment. However, there is always room for improvement and extension, and Microsoft have provided macro development possibilities through the Visual Basic for Applications environment.
In this article, we address the mechanisms that are available, whilst solving one of the problems that web writers often have with using Word - HTML handling. Web writers often like to edit HTML as plain text in Word, and then cut and paste it into Wordpress, Blogger, or another online editing environment.
In this way, we benefit from the excellent dictionary capabilities and spell check and thesaurus. Since typing tags manually can be tedious and time consuming, and saving the document as HTML in Word causes all kinds of unnecessary tags to be inserted as it converts from its own internal format.
The solution? Use macros to solve the tedium, and save everything as plain old text. Of course, changing the extension for upload can also be an irritation, but we'll solve that elsewhere. For now, let us assume that we just need to be able to cut and past from Word to an online editing environment.
Like Excel, and other Office applications, VBA is edited through the Microsoft Visual Basic Editor. This is accessed from the main menu (Tools->Macro->Visual Basic Editor), or through a simple key press : Alt+F11.
Macros can be attached to the document, global template (called Normal), or a specific template. Bearing in mind that there are many different approaches for organising code in Office applications, we shall assume that we are going to add the code in the Normal template, thus making it globally available to Word.
The easiest way to start a new collection of macros is to place them in a Module. To do this:
The new module will be given a name (such as Module1), so the first thing to do is change it. This is done in the Properties sheet, below the Project tree view. The (Name) should be set to something more meaningful, such as InsertHTML.
We type code into the window on the right hand side of the VBA editing environment. Code is organised into Sub blocks:
These blocks of code can be attached to toolbars, which can be docked or floating, and will be managed entirely by Word. This is a very flexible and powerful feature that we shall come to once we have a macro in hand.
In Word it is easy to insert text using a Macro. The common way to achieve this is to use the Selection object:
If there is nothing selected, then this will simply insert text, but if there is some text hi-lighted, Word will replace it. We can use this feature to conditionally insert text, or modify the text. For example, we could choose to turn a line of text into a heading, by making it bold.
To do this, we need to perform several steps:
To test for an empty selection, we use the following code:
We can temporarily store any text that is selected by using:
To put HTML tags 'around' a piece of text, we need the following code:
Putting this all together is left as an exercise for the reader, but here is the skeleton code:
Inserting the appropriate Selection.methods should render the macro usable.
With our macro in hand, we need some way to access it via Word. The easiest, most user-friendly way to do this is to add a toolbar (otherwise known as a button bar). The options for this are available in the Tools->Customize... menu option.
This will open a small dialog box. To add a new tool bar:
The new tool bar will be empty. To add items, they need to be dragged onto the tool bar from the dialog box. All the available commands are in the Commands tab. Our custom macros should be there too:
For those not following the module and/or sub names used here, the appropriate module and sub names will have to be substituted.
Customising Buttons
To customise the buttons, follow the following steps:
There are many options, from naming through to adding button images.
There has been a lot to absorb here, but by following the instructions, step by step, the reader should become proficient in adapting Word to fit into their working patterns, rather than the other way around.
The basic flow is to create the macro, in a specific Module, and attach it to a button-press. The Word object model is flexible enough to operate on anything from selections to paragraphs, lines, words, and even searching for specific words in the document.
Non-programmers might like to try the following:
Those wanting more information on VBA and MS Office (Word, Excel...) might find the following useful: