Heading

This is some text inside of a div block.
This is some text inside of a div block.
This is some text inside of a div block.
min read

Toggle basic text formatting

Word • Macros • Editing • Formatting
Peter Ronhovde
10
min read

This macro toggles the formatting of the current word without needing to manually select it. It can be trivially extended to other character formatting attributes or larger blocks of text.

Thanks for your interest

This content is part of a paid plan.

Toggle basic text formatting

Repetitive tasks are annoying which remains true when writing and editing a manuscript. We do some tasks thousands of times without even realizing they can be done more efficiently … if we only do a little up front work.

This macro applies basic formatting to the current word without needing to select the word. It can be trivially extended to other character formatting types or other text blocks such as toggling italics of sentences or paragraphs to indicate internal dialog.

Quick format toggle where no initial selection is necessary
Quick format toggle where no initial selection is necessary

Why bother?

We all get in a rut of just doing things the way we already know. Our fingers already know what to do without thinking about it. It already works, so why change it?

After all, how hard is it to select a word before italicizing it?

Well, yeah, it's not that difficult … but we could also mow a lawn with scissors. That works too. And horses got people where they needed to go for centuries. That sounds good enough!

The trick is to enhance the process while only disrupting our workflow for the better. Sometimes we need to overcome a little muscle memory but only because we want to work faster in the end. Something has to be different, and it would even nicer if we didn’t need to move (macro) mountains to make it happen.

On the other hand, if you want a bit more thorough approach (sign me up!), the member version takes the process up a level.

Create the empty macro

A previous post covers creating an empty macro like the one shown below. The result will be something like:

Sub ToggleWordItalics()
' Italicize the current word

End Sub

The single quote tells VBA the rest of the text on that line is a comment meant for human readers. We start our macro steps on the empty line.

A similar macro ToggleWordBold formatting would be a natural variation, but I find the italics version more useful in practice. I don’t use specific character formatting much beyond italics and bold, so for anything else, I usually use styles.

What are the manual steps?

Assuming no starting selection exists, what steps would we use to manually italicize the current word?

  1. Move to the beginning of the word if not already there (probably with a shortcut Command+ or Control+Left arrow)
  2. Select the current word (Command+ or Control+Shift+Right arrow)
  3. Press Command+I (or Control+I in Windows) to italicize the word

Of course, swap out the Command key for the Control key in Word for Windows. Alternatively, we could double click the word to select it which compresses steps 1 and 2, but using the mouse is often slower than it feels. The above steps aren’t a deal breaker, but we're converting it into a single keystroke.

If you want to do things differently based on whether an initial selection exists, visit the member article version.

Toggle italics for the current word

In this macro, the VBA steps roughly follow the manual steps, but we're not pretending to type on a virtual keyboard. Since the macro is extra short, we'll just use the Selection object (see our brief introduction for more information).

Select the current word

We're assuming we start with just an insertion point (i.e., the blinking I-bar waiting for us to type something). With that, we select the current word at that location using the Expand method.

Selection.Expand ' Default is by a word

We reference actions (called "methods") or data (called "properties") of the Selection (Expand in this example) using a dot as shown.

The Expand method extends the selection both directions in the document until it spans the entirety of the specified document element but no farther except for a possible trailing space (more to it, but we'll leave the details to other articles). Possible expansion units are tabulated in a standard Word Unit constants table which include most of the ones you might expect like word, paragraph, sentence, etc. The default expansion is by a word, so we can omit the Unit option for this macro.

Turn italics on (too direct)

If we just want to set the text in italics, we could use the ItalicRun method, but see below for a better way.

Selection.ItalicRun ' We can do better ...

This italicizes all text in the Selection regardless of whether it is already italicized or not.

Toggling the formatting is better

One of the goals with macros is to make writing an editing easier. If they only do what we can already do, that limits their utility. If we instead toggle the formatting, it makes the macro feel more natural. It's also more useful in practice since our fingers are already positioned on the shortcut keys if we change our mind.

Toward this goal, another approach to assign italic formatting is to use the Italic property of the Selection’s Range (the Selection does not have an Italic property by itself; see our introductory article for more on Ranges).

Selection.Range.Italic = True ' We can do even better ...

True turns italics on for all text in the range and False obviously turns it off, but the details are a little more complicated for real documents.

Some techy details

The ItalicRun method above acted on the selected text. The Italic Range property is a numeric value (technically, a Long numeric data type). A Boolean variable can only store True or False values, but VBA can also interpret a number to represent True or False when it is used where a Boolean value is needed.

This feature is perhaps a little confusing at first, but it is also useful for formatting since real text can contain a mixture of formatting obscuring the notion of a strictly True or False status of italicized text in a range. More specifically, if a range contains a mixture of italicized and not text then the Italic property will have a value of wdUndefined.

Rather than have some complicated logic testing whether the Italic property is True, False, or undefined and take action accordingly, VBA provides an easier way.

Toggle the formatting

Fortunately Word has a special toggle value for a Range’s Italic property. We simply assign the wdToggle constant to the property much like we would if we wanted to set it to True or False.

Selection.Range.Italic = wdToggle

This command doesn’t toggle italics character by character. Rather, it looks at the formatting of the first character in the Range and toggles the italics state of all the spanned text based on it. Most of the time this is what we want.

Gotchas

Could anything go wrong with the above steps?

What if an initial selection exists?

A common special case to consider is whether an initial selection exists when the macro is run. In this macro, the first command expands any selection over the word(s) if they are only partially contained within the selection. This process will only select the entirety of the words in the initial selection, so beginning the macro with a selection already made in the document is safe.

If you want control over what happens when a selection exists, see the member version.

What if no word is nearby?

This is where it gets tricky.

If the insertion point is at the beginning of a paragraph beginning with spaces or similarly at the end of a paragraph around whitespace, the expansion over the nearby word may do strange things (the details that trigger a problem for the macro can be very specific). They are fringe cases for this macro, and the corrections are far more involved than the base macro. At worst, we italicize some spaces, so we will ignore these issues.

Final format toggle macros

The final improved italics macro only contains two steps.

Sub ToggleWordItalics()
' Toggle italics in the current word or selection
Selection.Expand ' Default is by word
Selection.Range.Italic = wdToggle
End Sub

The bold version is almost identical. Just create a new macro and change the line with the Italic property to the Bold property.

Sub ToggleWordBold()
' Toggle bold in the current word or selection
Selection.Expand
Selection.Range.Bold = wdToggle
End Sub

These macros are fully functional, but they're kind of like training-wheels versions of motorcycle macros. For one, they change the Selection automatically. This isn't inherently bad since it can be a visual cue of what the macro did (I sometimes select text on purpose in a macro to draw the writer's eye to the changes), but in this macro, the formatting already changes letting the writer know something happened. These macros can also work a little strangely in a few isolated cases (see gotchas above).

Improvements

What can we do better?

Don't mess with the initial selection

If we're smarter about how we detect and use the initial selection, we can bump up the functionality. Using the base macro above, the member version adds several steps to work more naturally and seamlessly with typical writing or editing. For one, only the document change is visible on screen when the macro runs. This feels better in some cases since we can just keep working uninterrupted. Second, we can take different actions based on whether an initial selection exists or not.

Add some validation checks

Since the macro is working so quickly on our behalf, it's convenient to go ahead and let it check for any issues in the blink of an eye. For example, we could check whether our working range is empty or not before we apply italics.

Unfortunately, the validation steps make the macros look messier and tend to stretch out the steps more than we would like. The checks also take more time to implement and test. Still, I want my macros to work cleanly in most circumstances.

Internal dialog?

We might want to toggle the italics of sentences or paragraphs which is a common formatting technique in a novel to indicate internal dialog.

Selection.Expand Unit:=wdSentence ' Expand by a sentence
Selection.Expand Unit:=wdParagraph ' Expand by a paragraph

We assign wdSentence or wdParagraph unit from a standard Word Unit constants table to the Unit option. Of course, whether we implement it with a sentence, paragraph, or both (probably as different shortcuts) is a matter of preference and might even change by novel.

These commands have a hidden beneficial side effect where we can make a "sloppy" selection of sentences or paragraphs, and the Expand method extends the selection both ways to include them in their entirety.

Affiliate Links

If you're interested in using Word or another tool related to the article, check out these affiliate links. I may make a small commission if you purchase when using them, but there is no increase in cost for you, and it helps to support this site and associated content.

I've been using Microsoft for Business for commercial use (that's us writers) on one of the lower pricing tiers for years. I get to use my macros, have online storage, and don't have to worry about software updates.