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 more

Word • Macros • Functions • Editing
Peter Ronhovde
19
min read

Macros to toggle paragraph styles allow us to keep our fingers on the keyboard more. It’s much faster than the mouse for many tasks (but definitely not all). These functions enable several style toggle macros.

Thanks for your interest

This content is part of a paid plan.

Toggle Paragraph Styles Function

When editing, it’s often convenient to quickly change paragraph styles. For example, a standard Microsoft Word keyboard shortcut Command+Shift+N on Mac (or Control+Shift+N on Windows) applies the Normal style to the current paragraph. Taking this a step further, grouping two or three related styles together allows us to use keyboard shortcuts to access more styles faster saving us time while editing.

This is more of a workhorse function. No one is clamoring for a VBA function to toggle paragraph styles, but several related editing macros will benefit from it.

Function skeleton

We’ll create two versions, one for toggling between two different paragraph styles and another for cycling between three. The function skeletons are:

Function ToggleParagraphStyle(wParagraph As Paragraph, StyleA As String, _
StyleDefault As String) As String
' Toggle between two different paragraph styles

End Function
Function CycleParagraphStyle(wParagraph As Paragraph, StyleA As String, _
StyleB As String, StyleDefault As String) As String
' Cycle between three different paragraph styles

End Function

An underscore _ character tells the VBA editor that you are just continuing the same step on the line below usually to make it easier for humans to read. VBA does not care as far as interpreting the steps.

What parameters?

We accept a Paragraph variable for which we’ll toggle the paragraph style. We’ll call the parameter wParagraph which is short for “which paragraph.”

We then need two (or three) styles for the paragraph style toggle. One style will be applied to the paragraph if it initially has any style other than StyleA. We’ll call the default StyleDefault. If these are confusing, just use StyleA, StyleB, and perhaps StyleC for the second function.

Cycling between more than three styles becomes annoying, but the pattern is easy if you wish to implement more.

Return values

I don’t think we would see riots in the streets if we made this a subroutine instead of a function (only functions return values). This function doesn’t really need to return anything important to be useful, but we’re changing the given paragraph’s style, so it might be helpful to let whoever know which style the function picked. They aren’t required to use the information anywhere.

As such, we’ll return the name of the chosen style based on the toggle. A style name is a plain text String variable. We could be fancy and return the corresponding Style object instead, but I think that is overdoing it. Just returning the plain text name is simple and sufficient.

Validations (no fun!)

Yeah, I know they aren’t fun, but enduring a little aggravation and extra work now will very likely save much more frustration later, so put on your big-boy (or girl) boots and tighten your belt.

What do we need to validate?

Is the given paragraph valid?

A Paragraph is an object in Word VBA which might not be assigned to anything in the document yet. If we tried to use it as such, the function would crash and pop up a mean message.

Fortunately, we’ve dealt with this situation several times before when working with ranges. Essentially, we check whether the wParagraph variable is Nothing (meaning it has yet to be assigned to a meaningful paragraph in the document). We use the “Is” operator (like a plus + sign for numbers but for VBA objects) to compare whether two objects are the same or whether one object Is currently Nothing.

wParagraph Is Nothing

This results in a Boolean value (True or False), so we can put it in a conditional statement. We can’t do anything if the paragraph variable isn’t assigned to a document paragraph, so we just exit the function.

If wParagraph Is Nothing Then
Exit Function
End If

Since the command is so simple, I like the more compact notation on one line.

If wParagraph Is Nothing Then Exit Function

What about the styles?

Styles are an awkward subject since VBA doesn’t give us tools to validate whether a style exists in the current document (well, not without being cumbersome and painfully slow). To rectify this, we can create our own. A previous article covered a function IsStyle to do just this.

IsStyle(SomeStyle)

We “pass” the function a plain text style name or one stored in a String variable as shown here. The function returns a True value if the style exists in the document or the current template (usually Normat.dot) or False if not.

Style validations with IsStyle

We only want to exit the function if the style does not exist, so we include a Not “operator” with each style check. Remember a Not switches True and False values. Our two given styles from the function parameters are StyleA and StyleDefault.

Not IsStyle(StyleA)

Our second style check is:

Not IsStyle(StyleDefault)

If either style doesn’t exist, we need to exit the subroutine, or the style assignment later would crash the function along with whatever macro was calling it. The Or operator gives us a True result if either condition is True, so we combine the two conditions.

Not IsStyle(StyleA) Or Not IsStyle(StyleDefault)

On a personal note, I avoided style validations for a long time, but I my style macros often crashed when I switched between documents. Now, they run more smoothly.

Combined style validation

We exit the function if this compound condition is True. The concise version is:

If Not IsStyle(StyleA) Or Not IsStyle(StyleDefault) Then Exit Function

We could combine both validations (the paragraph and styles) into one conditional statement, but it looks a little messy, so I’m leaving them separated.

Changing a paragraph style

A Paragraph in VBA is an object with its own properties (data) and methods (actions). The style of the given paragraph wParagraph is easily accessed by referencing its Style property:

wParagraph.Style

This technically refers to a Style Variant type, meaning in this case that it can refer to a Style object, a Style identifier, or a plain text name. The latter allows us to assign the style name directly to it.

wParagraph.Style = StyleA

StyleA is a plain text String variable, but VBA handles the details of actually applying the paragraph style in the background.

Style toggle

The toggle logic is straight forward, but you might squint a little bit as you read the description of it.

If the current paragraph style is already StyleDefault, then change it to StyleA. Otherwise change any other style to StyleDefault. A hybrid VBA-English conditional statement for this decision looks like:

' Change paragraph style
If current paragraph style is the default style Then
' Change paragraph to style A ...
Else
' Otherwise, change paragraph to default style ...
End If

Getting more VBA-ish, we check the paragraph style against StyleDefault. The condition is:

wParagraph.Style = StyleDefault

Technically, the left-hand side could refer to a Style object, a numeric identifier, or a plain text variable. VBA will decide which one by context based on the type of StyleDefault. Since StyleDefault is a String, it compares them as plain text. Does the plain text name of wParagraph.Style exactly match the String variable name?

The conditional statement is:

' Change paragraph style
If wParagraph.Style = StyleDefault Then
wParagraph.Style = StyleA
Else
wParagraph.Style = StyleDefault
End If

See this previous article if you would like a brief introduction the conditional statements in VBA.

Cycle variation

With the cycle between three styles, we essentially add a similar ElseIf condition in the middle to switch from StyleA to StyleB. The conditional statement is something like:

' Change paragraph style
If current paragraph style is the default style Then
' Change paragraph to style A ...
ElseIf current paragraph style is style A Then
' Change paragraph to style B ...
Else
' Otherwise, change paragraph to default style ...
End If

Converting this conceptual conditional statement to a VBA version for this function is:

' Change paragraph style
If wParagraph.Style = StyleDefault Then
wParagraph.Style = StyleA
ElseIf wParagraph.Style = StyleA Then
wParagraph.Style = StyleB
Else
wParagraph.Style = StyleDefault
End If

Assign return value

We need to tell the user what style we changed the paragraph to be, so we set the function name to wParagraph’s Style property:

ToggleParagraphStyle = wParagraph.Style

VBA will automatically interpret the Style property to be a plain text name because the function returns a String value.

Final Functions

Putting the steps together, our macro the toggle between two paragraph styles is:

Function ToggleParagraphStyle(wParagraph As Paragraph, StyleA As String, _
StyleDefault As String) As String
' Toggle between two different paragraph styles

' Verify paragraph is valid
If wParagraph Is Nothing Then Exit Function
' Verify both styles are valid
If Not IsStyle(StyleA) Or Not IsStyle(StyleDefault) Then Exit Function

' Change paragraph style
If wParagraph.Style = StyleDefault Then
wParagraph.Style = StyleA
Else
wParagraph.Style = StyleDefault
End If

ToggleParagraphStyle = wParagraph.Style
End Sub

Extending this macro to cycling between three styles, we have:

Function CycleParagraphStyle(wParagraph As Paragraph, StyleA As String, _
StyleB As String, StyleDefault As String) As String
' Cycle between three different paragraph styles

' Verify paragraph is valid
If wParagraph Is Nothing Then Exit Function
' Verify all styles are valid
If Not IsStyle(StyleA) Or Not IsStyle(StyleB) Or _
Not IsStyle(StyleDefault) Then Exit Function

' Change paragraph style
If wParagraph.Style = StyleDefault Then
wParagraph.Style = StyleA
ElseIf wParagraph.Style = StyleA Then
wParagraph.Style = StyleB
Else
wParagraph.Style = StyleDefault
End If

CycleParagraphStyle = wParagraph.Style
End Sub

I find that trying to cycle between more than three styles is annoying, meaning a better solution probably exists at that point.

With statement?

The above functions are a tiny, itsy-bit less efficient with the repeated references to wParagraph.Style. You’d never notice a practical time difference in an editing macro, but all the wParagraph.Style references hanging around is also unsightly.

We could simplify the repeated references using an extra With statement and perhaps improve the presentation. Extracting the If statement and return value at the bottom of the second function, we would have:

' ... rest of function above
With wParagraph
' Change paragraph style
If .Style = StyleDefault Then
.Style = StyleA
ElseIf .Style = StyleA Then
.Style = StyleB
Else
.Style = StyleDefault
End If

CycleParagraphStyle = .Style
End With

Hmmm.

I’m not impressed. Are you?

The above functions aren’t overly complicated as is, and this doesn’t seem much clearer than the original (maybe just an itsy-bit better). This version is ever so slightly more efficient, but I’m still inclined to leave the functions as originally presented. It was, however, instructive to see an alternative presentation option along with another example using a With statement.

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.