Introduction to VBA for MS Word

How to extend Microsoft Word using VBA and macros.

© Guy Lecky-Thompson

Help!, SXC.HU

This article explains how VBA and macros can be used in Microsoft Word to add, modify, and format text.

Introduction

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.

Editing VBA Macros

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:

 Sub myMacro
  ' Code block
 End Sub

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.

Conditionally Inserting Text

In Word it is easy to insert text using a Macro. The common way to achieve this is to use the Selection object:

  Selection.TypeText ("Text to insert")

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:

   If Selection.Text <> "" Then
     ' Insert code here
   End If

We can temporarily store any text that is selected by using:

   szTempText = Selection.Text

To put HTML tags 'around' a piece of text, we need the following code:

  Selection.TypeText ("<B>")
  Selection.TypeText (szTempText)
  Selection.TypeText ("</B>")

Putting this all together is left as an exercise for the reader, but here is the skeleton code:

Sub MakeHTMLBold()
 If Selection.Text <> "" Then
  ' Save selected text
  ' Code to insert start tag
  ' Insert saved text
  ' Code to insert end tag
 Else
  ' Code to insert start + end tag only
 End If
End Sub

Inserting the appropriate Selection.methods should render the macro usable.

Top VBA Tip!

Creating a Button Bar

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.

Summary

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.

Further Reading

Non-programmers might like to try the following:

Those wanting more information on VBA and MS Office (Word, Excel...) might find the following useful:


The copyright of the article Introduction to VBA for MS Word in Computer Programming is owned by Guy Lecky-Thompson. Permission to republish Introduction to VBA for MS Word must be granted by the author in writing.




Post this Article to facebook Add this Article to del.icio.us! Digg this Article furl this Article Add this Article to Reddit Add this Article to Technorati Add this Article to Newsvine Add this Article to Windows Live Add this Article to Yahoo Add this Article to StumbleUpon Add this Article to BlinkLists Add this Article to Spurl Add this Article to Google Add this Article to Ask Add this Article to Squidoo