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

Goto styled paragraph

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

We create a VBA macro to jump to the nearest paragraph with a given style including an example use case of quickly navigating to inline novel notes.

Thanks for your interest

This content is part of a paid plan.

Goto Styled Paragraph

I want to be able to jump to paragraphs with specific styles.

Why?

This is one of those features that arise as you work. I'd really like to be able to ... do this or that. Or maybe whoa, that was really aggravating ... there should be a better way.

I don’t like Word comments

I don’t like the Word comments feature when writing and editing a novel. They’re fine for office interactions on smaller documents, but I find them cumbersome to use in most aspects for writing especially when a novel may have a few thousand notes at varying levels of detail by the time a rough draft is finished.

Novel notes

As a result, I prefer to include my novel notes inline in distinctly styled paragraphs. These paragraphs can easily be marked as ignored or deleted (via other keyboard shortcuts) when they are completed.

Quick overview of my notes

Just so we have a reference for the intended application below, I use several novel note styles for different purposes: Scene Note, Scene Comment, and Scene Summary.

  • Scene Notes are more like notes for topics to research or for something to validate such as a setting-specific detail that I need to nail down eventually.
  • A Scene Summary line describes something to include or that should otherwise occur in a scene.
  • Scene Comments are more general comments about things to remember for a scene or perhaps a reminder about the background, internal character thoughts, an intended mood, or other details I need to remember when I edit the scene.

When editing, it is efficient to jump to the next or previous similar note without having to scroll, scroll … make changes, and scroll more.

Ughhh.

For example, I may want to iterate through my “Scene Notes” to make sure I’ve accounted for all setting-specific or research-related elements in each scene or subscene before I begin my first read-through. Of course, this is just part of my editing process that has grown over several novels, so your approach will be different.

Inline text notes extension?

The idea could easily be extended to inline text within paragraphs if that’s more to your liking, but I haven’t worked like that much. For example, a VBA Find operation could also utilize unique fonts and/or other formatting to quickly identify and jump between the notes.

Editing more efficiently

I think many writers misjudge the effectiveness of using (or making) better tools to improve their process. Even when just navigating a document, it doesn’t seem slow to grab the mouse and use a scrollbar or to even tap Page Down key multiple times before fishing for some relevant text. Our fingers are busy the whole time, so it seems productive, but we can often dramatically improve the efficiency of specific or repetitive editing tasks.

We previously covered a function that gets the range of the nearest paragraph with a certain style. Now we’ll leverage that function to create two short macros to quickly jump to novel note paragraphs.

We’re thinking about two specific macros today, but the applications are more general. Upcoming articles will also illustrate how to toggle scene heading styles without moving to the heading paragraph. If you’re interested in more navigation improvements, check out the navigating to empty paragraphs and the new paragraph with sentence macros which are two of my favorites. Both are simple yet useful.

You’ll probably have your own personal techniques where editing efficiency could be improved. Let me know any interesting ideas you have or have used.

Macro skeletons

We’ll focus on “Scene Comments” notes mentioned above to be specific, the following macros trivially apply to other paragraph styles. Our macro skeletons are:

Sub GotoNextSceneComment()
' Jump to next Scene Comment paragraph in document

End Sub
Sub GotoPreviousSceneComment()
' Jump to next Scene Comment paragraph in document

End Sub

Word macros and keyboard shortcuts

These macros are “subroutines” with no parameters. If we intend to assign keyboard shortcuts or custom buttons to a macro in Word, it cannot have any parameters or be a function. On the other hand, voice command scripts that interact with Microsoft Word from within Dragon Professional do not have this same limitation (see separate article to get started if this sounds interesting), but it is a separate paid application and a topic for another day.

Set a working range

A common initial step requires setting up a range variable to work within the document. A range variable is something like a hidden Selection or insertion point with which we can manipulate the document. They share many features but are not quite the same. We’ll call our variable name r just to keep the name short.

Dim r As Range

Get paragraph range

The previous function we’ll utilize today looks like:

FindParagraphStyleRange(SomeStyle, Direction)

SomeStyle is our intended paragraph style which can be either plain text in double quotes like “Scene Comment” or a style name stored in a String variable. We’re using the former in these macros.

Direction is a True or False value where True searches forward in the document, and False searches backward. The second argument may be omitted in which case the search defaults to forward.

Set paragraph range

If our search function finds a match, it returns a range of the nearest paragraph in the specified direction with the intended paragraph style.

Set r = FindParagraphStyleRange("Scene Comment")

This particular function “call” will search for the next paragraph with a style of “Scene Comment”. The search is forward in the document because the second argument is missing and True is the default value.

We assign the function result to the working range r we defined earlier. Set is required for range assignments (and other objects) because they contain more information than a number or plain text.

Was a paragraph found?

When running the macro in a general novel document, a significant chance exists that no matching paragraph style was found. If so, the function literally returns a result of Nothing. Since a range is an object, we can check whether it is equal to Nothing using the “Is” operator.

r Is Nothing

Technically, Is compares whether two objects are the same thing, but Nothing is the value objects have when not assigned to anything in the document yet, so we also use Is to compare an object variable to Nothing.

We only want to jump to the paragraph if it is not Nothing, so the condition becomes.

Not r Is Nothing

Remember a Not switches True and False values, so this overall statement is True if a matching paragraph style was found.

Conditional statement

Since this is a Boolean value, we can use it directly in a conditional statement:

' Check whether a matching paragraph was found
If Not r Is Nothing Then
' Jump to identified paragraph ...
End If

If a paragraph is not found, then we skip the inside steps, and the macro ends, so nothing else happens.

Collapse the range

This is more of a preference but given the name of the macros begin with “Goto”, we do not expect either macro to select the intended paragraph. We should probably collapse the range.

r.Collapse

The range collapses toward the Start position by default, meaning no text is spanned by the range (it is empty in the intuitive sense), but it is now positioned at the beginning of the matched paragraph.

Select the paragraph location (range)

We’ve been working with a range variable r which is essentially invisible to the user (unless we make a change in the document), so the original Selection is currently unchanged. We want to jump the Selection to the beginning of the matched paragraph. The easiest way is to Select the now empty range.

r.Select

Final Macros

These short macros allow me to jump between notes while writing, but a more practical application is to quickly iterate through a specific category of notes (see top) while editing to ensure each note has been handled (or purposefully rejected).

Sub GotoNextSceneComment()
' Jump to next Scene Comment paragraph in document

' Declare working range and find styled paragraph
Dim r As Range
Set r = FindParagraphStyleRange("Scene Comment")

' Move to styled paragraph if one was found
If Not r Is Nothing Then
r.Collapse
r.Select ' Move to identified paragraph
End If
End Sub

The macro the jump to the previous Scene Comment paragraph is almost the same. We just add a second argument of False to the FindParagraphStyleRange function to indicate a backward search.

Sub GotoPreviousSceneComment()
' Jump to previous Scene Comment paragraph in document

' Declare working range and find styled paragraph
Dim r As Range
Set r = FindParagraphStyleRange("Scene Comment", False)

' Move to styled paragraph if one was found
If Not r Is Nothing Then
r.Collapse
r.Select ' Move to identified paragraph
End If
End Sub

Of course, you could add different versions of these macros for different types of novel notes, scene headings, or whatever.

Notice how using the previous function makes these macros almost trivial. All the details of finding the intended paragraph are nicely tucked away inside the function. If we happen to improve or even extend that function, these macros inherit those changes (usually) automatically.

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.