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 paragraph styles

Word • Macros • Editing
Peter Ronhovde
27
min read

We create a macro to toggle the style of the current paragraph between related styles. Such style toggles allow faster access to more commonly used styles.

Thanks for your interest

This content is part of a paid plan.

Toggle paragraph styles

Word includes many standard keyboard shortcuts some of which apply common paragraph styles to the current paragraph. We can further create our own shortcuts for other styles, but this macro takes it up a notch.

Often, we have a set of related styles, and we like them all. We'll set up a macro to toggle between two or three related styles and have access to each of them at the small cost of having to quickly tap the shortcut again.

Huh? Why?

I like to keep my fingers on the keyboard as much as possible, and I particularly dislike hunting through the Quick Styles ribbon panel or worse the Styles Pane. It’s painfully slow even for the "Quick Styles" panel. I eventually discovered the Word shortcuts to apply the Normal paragraph style or heading styles, but those weren't helpful enough since I have a stack of my own custom styles for my novels.

Standard Word paragraph style shortcuts

Word's standard keyboard shortcuts to change the style of selected paragraphs include:

  • Control+Shift+N applies the Normal style
  • Control+Shift+L applies the List Bullet style
  • Control+Alt+1, 2, or 3 apply the respective Heading 1, 2, or 3 styles

Swap out Control for Command and Alt for Option in Word for Mac. These are fine even if limited, but it's cumbersome to create a separate shortcut for every desired custom style.

Some other shortcuts manipulate specific paragraph settings or character formatting, but we're not using those unless necessary. Using styles allows us to quickly and consistently change the formatting across the document.

What is a style toggle?

A style toggle is not a fundamental Word concept, so what is it?

As a specific example, I had several sets of related novel styles like Scene Idea or Scene Tentative which I use both during writing and editing. I did not want different shortcuts, but I eventually had an epiphany. Just connect them to the same shortcut with a macro and let the macro decide which to apply.

The idea is to tap the keyboard shortcut once to change the paragraph to the default style and a second time if I want the second style. It’s very fast since your fingers are already on the shortcut keys.

Example of a paragraph style toggle
Example of a paragraph style toggle (heading text changes for clarity)

Instead of having to remember a separate shortcut for each common paragraph style, we combine similar styles into a single shortcut and let the macro decide which one to apply. For presentation purposes, the above example also changes the paragraph text to match the style name.

How I use style toggles

Let's talk through a concrete but abbreviated example of how I use the style toggles, but I need to briefly explain my writing approach, so it makes more sense.

Brief review of my writing method

While my nature wants to outline down to the paragraph, the practical process of writing just does not allow it for me. As a result, I have adapted my process to work with the words that pass through my fingers.

  • I start with a high-level idea for the novel
  • Work on the characters some
  • Develop an ending
  • Decide on some major plot points
  • Let some fun scene ideas flow sometimes even doing some preliminary writing
  • Make notes in a notes document but also within tentative scenes within the novel

The novel structure develops as I write and edit as opposed to being hammered into the story. I imagine scene ideas and include them in the growing novel along with any running notes. I write some, reorganize as needed, etc. Subscene headings are added as I flesh out the actual text. To my chagrin, the process is cyclical and somewhat random, but I try to keep it fun and exciting for myself as much as possible.

How the styles help me structure my novel

Why does this matter for the current macro?

I create a running but flexible outline as I write using paragraph styles like Act and Chapter but also various versions of scenes and subscenes. The scene and subscene styles help keep my ideas and progress more organized during both the planning and writing phases (they overlap a lot for me). I combine two or three related styles into a single shortcut, so I don't have as many combinations to remember. It may be particular to my own writing process, but I thought others might like the idea.

We'll focus on the scene variations, so a few examples include:

  • Scene Idea – A planned scene likely to be used as I progress in the document. Usually I’ll have scene notes about important elements or snippets for reference when I write the actual text later.
  • Scene Tentative – A tentative scene with some elements I like, but I'm not yet committed to it enough to necessarily be included in the final story.
  • Scene Heading – Scene heading style for scenes where I have completed a rough draft of the scene text.

Others not used in the current macro, but I wanted to give a better idea of my process.

  • Scene Edited – A full scene text that has been edited at least once.
  • Scene Fork – A tentative scene that changes the direction of the novel or conflicts with other more established scenes.

A few more styles exist like various subscenes, novel notes, etc., but the above scene styles cover the basic idea in the context of this macro. We'll focus on the top two or three.

Each heading format is distinct, so I can see at a glance what the status of the scene is. The formatting does not show up in the Navigation Pane (the styles are derived from the respective standard Word headings), but I also have another macro to extract all my current outline information, including major notes, into a separate organizational document. I usually place this document on a separate side monitor, so I can glance at it as I write.

Create the empty macro

A previous article covered creating an empty macro using the ribbon and dialogs. When you’re done, you’ll have something like:

Sub ToggleParagraphStyles()
' Toggle the paragraph style between two styles

End Sub

The single quote tells VBA to ignore the rest of the text on that line as a comment meant only for human readers, but our macro steps begin on the empty line.

What do we need for a style toggle?

We need to test a paragraph style, and we need to apply a style to a paragraph.

What is the Selection?

We’ve been using the Selection a lot. For us humans, it is the content spanned in the document usually shown in a different background color (selected using the mouse or keyboard) ready for us to copy, delete, or whatever.

In VBA, the Selection (with a capital S) is a thing called an "object" that obviously includes the current document selection, but it also contains data (properties) and actions (methods) that are used to manipulate it and the corresponding document content. The Selection may also refer to an insertion point (the blinking I-bar waiting for typed text) which contrasts with how we typically use the word as humans, but an insertion point is basically an empty selection, so they are controlled with the same VBA object.

What is a paragraph?

To a human, a paragraph is a sequence of sentences that follow a coherent topic in the document. The basic idea seems to be ancient, stretching back a couple millennia, but it is fundamental element of modern writing just above the sentences that comprise them. Modern Word processors allow us to compose and organize paragraphs in a document and make the whole process easier and faster than using clay tablets or even paper. Move over Gutenburg.

In VBA, a Paragraph (with a capital P) spans some document content, but it also consists of properties and methods that are used to manipulate them and the corresponding document content. For this macro, we are particularly interested in the Style property.

What is a paragraph style?

In human terms, a paragraph style is a particular set of formatting rules applied to a paragraph. A given style necessarily has a (plain text) name to uniquely refer to it.

In VBA, a Style (with a capital S) is an object thingy with various properties used to track all the details related to the formatting applied to the content. As with general Paragraph, a Style also includes various methods used to manipulate the Style within the application or otherwise act on the content.

We need a convenient way to reference or assign a style to a paragraph. After all, users can make their own with essentially any name including alphanumeric characters or underscores (yuck). Most of the time, we can conveniently refer to a style by its plain text name such as "Normal". The name is placed within double quotes, making it a string (of characters) in VBA. Using the respective names means we usually don't need to create Style variables to control them making our lives a little easier.

Get the current paragraph

How do we get the current paragraph or paragraphs?

We want the current paragraph style, so we need to first identify the current paragraph reference. We refer to the Paragraphs collection of the Selection which consists of all paragraphs even partially included in the Selection.

Selection.Paragraphs ' Not done ...

We must eventually correct for a complication that pops up when the initial selection spans multiple paragraphs with different styles, but we'll catch that with a later validation step (see gotchas below). For now, we'll move forward assuming we have a consistent paragraph style.

Get the current paragraph style

We get the paragraphs' style using the Style property.

Selection.Style ' Style of all selected paragraphs

Now that we have the style, what do we do with it?

What is the Selection Style property?

The Selection's Style property technically refers to the style of the entire selection, meaning all paragraphs, so it appears to be a shorthand for the Paragraphs collection style:

Selection.Paragraphs.Style ' Style of all paragraphs in the Selection

We will just use Selection.Style for simplicity and account for the issues with multiple paragraphs and styles later.

Selection.Style ' Shorthand Style reference

This also isn't the same as the first paragraph style unless the initial selection is fully contained within one paragraph. As a result, the Style property is only valid when all paragraphs have the same style, but we handle the invalid case in the second gotcha below.

Conditional statement for style check

In the context of our style toggles, we need test whether a paragraph has a specific style before we apply a different one. We need conditional statements. See our brief introduction for more explanation, but here is a quick review of the essentials.

Rough conditional statement

A rough conditional statement in something like English might read:

If a paragraph has style A Then
Change it to style B
Otherwise
Change it to style A by default
End the paragraph style check

Let's translate this Englishy statement to something more like VBA.

Conditional statement skeleton in VBA

In VBA, an If-Then-Else statement allows us to check a condition and make a decision in our macro about which steps to run, if any.

If SomeCondition Then
' Do these steps if SomeCondition is True
Else
' Do these steps if SomeCondition is False (optional)
End If

SomeCondition is any condition that VBA can determine whether it is True or False (a "Boolean" value). True is essentially a yes to whatever question is asked, and False is a no. The list of possibilities is quite long, so we'll focus on what we need.

If the condition is True, certain steps will be run, but if the condition is False other steps will be run. Generally speaking, the "otherwise" or Else part is optional, but we need it in this macro as a default case.

How do we check a style?

We need a way to check which paragraph style a given paragraph has. We can check directly like so:

' Checks whether the paragraph(s) style is Normal
Selection.Style = "Normal"

This is a condition asking VBA whether the current Selection has the paragraph style "Normal". In the above conditional statement, this is our test condition. Moving forward, we'll switch to a more generic "Style A" to keep it general.

Fortunately, everything works just fine even if the initial selection is empty or does not contain a full paragraph (perhaps just a word or a sentence) since the style property will just refer to the current paragraph.

Checking the current paragraph style

Combine the above ideas, our conditional statement skeleton is:

If Selection.Style = "Style A" Then
' Do these steps if the paragraph has a Style A style
Else
' Do these steps if the paragraph does not have a Style A style
End If

Now, we just need to assign the desired paragraph style.

Assigning a paragraph style (confusion warning)

Fortunately, VBA makes it easy to assign any paragraph style just by assigning its plain text name to the Style property. VBA is actually doing a lot in the background where it references, stores, and applies the relevant formatting information for the style corresponding to the plain text name. To assign a style to every paragraph of the Selection, we just use a literal equals = sign.

' Assigns a paragraph style when used as a separate step
Selection.Style = "Style A"

Wait ... huh?

Overlapping comparison and assignment notation

Isn't that the same thing we used above for the condition?

Uhhh … kind of but not exactly. It's a little confusing because this resembles … okay, looks the same as the way we compare a style property to a name.

I often gloss over such inconsistencies, but in this macro, we can't avoid either usage because we need to both validate a given paragraph's style and then assign a specific style to it based on the result. In either case, we have to use an equals = sign (some languages require a double equals == sign for conditions to be clearer) along with the name of the style in double quotes. VBA will determine the difference based on the context of the command.

We could also assign a style using a properly defined Style variable (perhaps if we were creating or changing a Style within the macro) or a constant from a built-in styles table, but it's usually more convenient to just use the name.

Toggle the paragraph styles

Now we put everything together to toggle the paragraph style between two desired styles. This is just a sketch of one of my own implementations.

If the current paragraph is already a Scene Idea paragraph style, we change it to a Scene Tentative style. Otherwise, we default to setting a Scene Idea paragraph style since that is the more common case (in my own writing when working on a new novel). Our conditional statement skeleton is then:

If Selection.Style = "Scene Idea" Then
' Assign a Scene Tentative paragraph style
Else
' Assign Scene Idea as a default for any other initial paragraph style
End If

Adding the respective style assignments, we finally get the bulk of the macro (but stay tuned for two important validations in the gotchas below).

If Selection.Style = "Scene Idea" Then
' Assign a Scene Tentative paragraph style
Selection.Style = "Scene Tentative"
Else
' Assign Scene Idea as a default for any other paragraph style
Selection.Style = "Scene Idea"
End If

The line, Selection.Style = “Scene Idea”, sets the Style property of the Selection paragraph(s) to the desired style.

Any gotchas?

Much of the time, we breeze over gotchas, but we have two significant issues to worry about for this macro. Since these are both moderately serious, we need to include the corrections in the final macro.

Is the style name valid?

The more frustrating gotcha occurs when the paragraph style does not exist in the current document or the current template (usually Normal.dot). Trying to refer to an invalid style is an error. To my knowledge, attempting an invalid style assignment can’t corrupt the document, which is somewhat comforting to a writer, but it is annoying when your macro crashes, and you get a popup window complaining at you.

You might think it wouldn’t happen. (I convinced myself so for a while until reality kept whacking my knuckles; owww, oooh, ummph; okay, I'll fix it.) What if you create a new document, but the style isn’t there, and it isn’t part of your Normal.dot template? Or you change your style names sometime later, and your old documents don't have the revised style.

Of course, we can take steps to ensure our styles are consistent across our works in progress, but it’s going to happen. Sometime.

The real problem with missing styles

VBA should at least allow us a clean way to catch the error and end the macro nicely, but it doesn’t, so we have to work around it. A separate function covers how to check whether a style is valid before you try to assign it to a paragraph. It’s simple in principle but a little complicated to explain, so it is outside the scope of this article. Please include that function along with either or both of the macros below to have a safer set of final macros.

Do all the styles exist?

I want to skip this error check to make things simpler, but crashes are unacceptable, and missing styles are a common place to experience errors. If we're going to do this right, we need to add a few steps to validate all the styles used in the macro.

Style validation function

One validation function mentioned above is:

IsStyleValid("Style Name") ' Not done ...

It returns a True or False (Boolean) value answering whether or not the style is valid in the current document. As such, we can directly use it in a conditional statement to validate the styles. We need to provide the name of the style in double quotes like "Style A" or in a properly defined plain text string variable (not shown).

Since we're usually more worried about an invalid style, it's more convenient to just use that version of the function.

IsStyleInvalid("Style Name") ' Not done ...

Of course, this version just returns True if the style is invalid and False if the style exists. It sounds a little confusing at first, but it simplifies constructing the conditional statement below since we only want to exit the macro if either style is invalid.

Exiting the macro

The command to exit the macro is simply:

Exit Sub

Some care should be used, in general, since it quits the macro immediately. No other steps are run. Most of the time this is fine, but if (in another macro) we're doing something more complicated, we might need to run other steps to quit cleanly. In this macro, stopping immediately is acceptable.

Style validation conditional statement

A rough conditional statement would be:

If Style A is not valid or Style B is not valid Then
' Exit the macro immediately
End the style validations

If either style is invalid, then we should exit the function. This compound condition corresponds to an Or (Boolean) operator. An "operator" is a symbol or word that acts on something else (a condition here) and gives us a (usually different) result. In this case, Condition A Or Condition B is True if either or both conditions are True, but it is False only if both conditions are False.

Working all this into a VBA conditional statement, we get:

If IsStyleInvalid("Style A") Or IsStyleInvalid("Style B") Then
Exit Sub ' Exit the macro immediately
End If
Is validating the style inefficient?

Is it inefficient to validate both styles every time the macro is run?

Yes, but the priority is avoiding a macro crashing, and using styles is a topic where errors happen more frequently.

I avoided this for a long time because I convinced myself I would not make the mistake, but it happened. And again. Then … yeah, you get the point. I tired of the error messages interrupting my workflow, so I added the validation steps. Unless you're having computer problems, almost every editing macro we run is so fast that you will never notice the difference.

Does an initial selection exist?

If the user runs the macro with an initial selection in the document and that selection spans more than one paragraph with different styles, the Selection.Style property will not be defined, and the Style property will not return a specific style name. Despite the documentation, it will literally have a value of Nothing. This is not "Nothing" as a text style name that does not exist in the document (or the current global template), but Nothing as in not referring to anything valid in the document or otherwise.

Trying to carry out an action using a variable set to Nothing results in a VBA error. As such, it will usually "crash" the macro, so it stops running immediately and pops up an annoying notice to that effect. We want to avoid any such problems.

Easy solution (not used)

The easiest solution in this case is to sidestep the problem. Just pretend it doesn't exist. How? Before we do anything else, just collapse the Selection with the Collapse method.

Selection.Collapse ' We can do better (not used)

With this statement, the Selection cannot span more than one paragraph but doing so without any other consideration does not allow us to apply the macro to multiple paragraphs with different styles. I use this quick and dirty solution in some macros, but multiple paragraphs with different styles is not an unusual use case, so let's work around it.

Allow an initial selection

Taking an extra step for maximum usability, it makes sense to allow an initial selection when the macro is run. Toward that goal, we need to account for the fact that the Selection.Style property may be Nothing if the selection spans more than one paragraph with different styles. In such a case, the documentation states it will return the style of the first paragraph, but in testing, when the selection spans more than one paragraph with different paragraph styles, the property will literally refer to Nothing. This is a value VBA gives to objects that exist as a variable or property but are not defined yet (usually based on some document element).

To test whether the Selection.Style is Nothing, we use that phrasing (VBA tries to be easy to read). We literally set up the condition using the wording "Is Nothing".

Selection.Style Is Nothing ' Validating the style property

This condition tests whether the object thingy stored in the Style property of the Selection is assigned a value of Nothing, meaning it is not valid to use in the document.

If we have an invalid Style property, the easiest action to take is to just collapse the Selection. If you prefer that approach, include it here, but we can do better. We know the default style in this macro is Scene Idea, so why not just go ahead and assign it to all of the paragraphs? Putting this into a conditional statement, we have:

If Selection.Style Is Nothing Then
' Assign default style to all selection paragraphs
Selection.Style = "Scene Idea"
Exit Sub
End If

If the Style property is invalid, we make the default assignment to all paragraphs (even partially) contained within the Selection and just exit the macro like we did earlier. We exit immediately because we're done, and the following steps would change our default assignment. Of course, this assignment needs to be done after the Scene Idea style has been validated for the document.

Must be valid before any other use

This Style property validation must be performed before any other Selection.Style references. We need to know the property is valid before we even attempt a specific test, or we'll get an error. While it's a tad awkward, the workaround accomplishes most of what we desire from a fully functional style toggle macro.

Final toggle paragraph styles macro

An example macro implementation to toggle between two paragraph styles is:

Sub ToggleParagraphStyles()
' Toggle the current paragraph style between two different styles

' Validate the two styles used in the macro
If IsStyleInvalid("Scene Idea") Or IsStyleInvalid("Scene Tentative") Then
Exit Sub ' Exit the macro immediately
End If

' Avoid any issues with multiple paragraphs in an initial selection,
' so just assign the default if the Style property is not valid
If Selection.Style Is Nothing Then
' Assign default style to all selected paragraphs
Selection.Style = "Scene Idea"
Exit Sub
End If

' Assign a paragraph style based on the current style
If Selection.Style = "Scene Idea" Then
' Assign a Scene Tentative paragraph style
Selection.Style = "Scene Tentative"
Else
' Assign Scene Idea as a default for any other paragraph style
Selection.Style = "Scene Idea"
End If
End Sub

Of course, change the style names to yours and create your own variations for any other style groups you like. I have several combinations. I assigned this macro to Command+Control+H in Word for Mac and Control+H in Windows. I regularly override default keyboard shortcuts that Word provides since I’d rather have my favorite commands at my fingertips than a few barely used ones.

The style validations at the top are basically a necessity (unless you like your macro crashing if you switch documents and neglect to keep the styles consistent), but we cover that function in another article.

The Selection.Style references are shorthand for the Paragraphs collection style. In my testing, all paragraphs must have the same style to return a valid result (despite the documentation asserting it will return the style of the first paragraph when multiple paragraphs are selected), which is the reason for the validation in the second set of steps.

Why was the Selection used?

I've previously advocated using range variables over the Selection since the changes are visible immediately on the screen, but this macro uses the Selection anyhow. Why? This macro actually only makes a single change to the document.

A little extra complicated

I wanted to keep this macro simple by excluding the style and property validations at the top of the macro, but the possible crashes resulting from either situation were not worth the risk (see both gotchas above). Checking for a invalid Style property (is it Nothing?) also allowed the macro to apply a style to multiple paragraphs in the selection, which is a nice bonus.

Cycle between three or more styles

Occasionally, we might have three related styles we would like to tie to a given shortcut.

How do we cycle the paragraph styles?

What is the correct order of style checks and assignments to get the desired behavior? After checking the current paragraph's style, a new style is assigned to the paragraph with this basic flow:

  • Style A → Style B
  • Style B → Style C
  • Otherwise assign Style A

The cycle would be: Style A converts to Style B, Style B to Style C, and then back to Style A. Style A is the default which is usually the one we want to apply most often. How does this cycle work in a conditional statement?

Chained conditions

As mentioned previously, an If-Then-Else conditional statement extends the basic use of an If-Then statement. We can check a condition and make a decision about whether to run certain steps if the condition is True or others if it is False. If we have a more complicated set of conditions, we can add an extra ElseIf (jam Else and If together without a space) Then condition check in the middle. An optional Else part can still be included, if needed. The extended conditional statement looks something like:

If FirstCondition Then
' Do these steps if FirstCondition is True
ElseIf SecondCondition Then
' Do these steps if SecondCondition is True but only
' if the FirstCondition is False (optional)
Else
' Do these steps if both conditions above are False (optional)
End If

As with a regular If-Then-Else, the last Else part is optional, and it is the default if no other conditions are met. If the Else is omitted, and none of the above conditions are met, no steps will be run. If any condition is matched, only those steps are run, and the conditional statement ends. See our introductory article for more explanation.

We can chain more ElseIf-Then links together in the middle, but it gets messy after a few cases. Another related command, Select-Case, is clearer when many cases exist. While testing variations of the current macro, four styles have felt like too much, so I've settled into a maximum of three paragraph styles as a good, practical limit for cycling styles.

Final cycle paragraph styles macro

Our final macro to cycle between three paragraph styles is:

Sub CycleParagraphStyles()
' Cycle the current paragraph style between three different styles

' Validate the three styles used in the macro
If IsStyleInvalid("Scene Idea") Or IsStyleInvalid("Scene Tentative") Or _
IsStyleInvalid("Scene Heading") Then
Exit Sub ' Exit the macro immediately
End If

' Avoid any issues with multiple paragraphs in an initial selection,
' so just assign the default if the Style property is not valid
If Selection.Style Is Nothing Then
' Assign default style to all selection paragraphs
Selection.Style = "Scene Idea"
Exit Sub
End If

' Assign a paragraph style based on the current style
If Selection.Style = "Scene Idea" Then
' Assign a Scene Tentative paragraph style
Selection.Style = "Scene Tentative"
ElseIf Selection.Style = "Scene Tentative" Then
' Assign a Scene Heading paragraph style
Selection.Style = "Scene Heading"
Else
' Assign Scene Idea as a default for any other paragraph style
Selection.Style = "Scene Idea"
End If
End Sub

We added an extra style validation check for Scene Heading at the top, but we included an underscore _ to split the longer compound condition. An underscore tells VBA to continue with the next line in the editor as if it were the same line. It just helps structure the command, so humans can read it easily.

See the other comments above about the overall macro elements and reasoning.

Possible improvements?

I've tried as many as four styles in a cycle, but in my experience, that’s too many in a practical sense.

Another variation adds a few extra steps to automatically create the new style in the document if it isn’t found, but that is a topic for another day if enough interest exists. I only added it for my most used styles.

I included several versions for various novel style groups, so I eventually created several functions to generalize the toggle or cycle steps. The revised macros iterate through the paragraphs and apply the styles independently to each one. Such functions might appear in future member content if enough interest exists.

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.