Jumping between document headings is another way to navigate documents faster. With that in mind, we create two sets of such macros. The latter set also works with derived heading styles such as chapters and scenes.
Thanks for your interest
This content is part of a paid plan.
Goto a heading
Tools for manipulating headings in Word could use a boost. Practical shortcuts and commands are limited, but one fundamental aspect we can improve immediately is implementing faster, better navigation between headings. Faster navigation leads to faster editing.
It's always easy to grab the mouse and click, but it's slower than it seems, so finding ways to work faster using the keyboard will speed up the process. It's why keyboard shortcuts were invented. Jumping to headings is a special case, but it's handy when needed, and the effort required to build the macros is small.
I add chapter and scene headings as I work through my novel, so I probably use these more than the average writer, but they're also useful in other documents.
Create the empty macro
We're starting from scratch, so see a previous post on creating an empty macro. We’ll also need a similar one for jumping to the previous heading since you’ll probably want both. The result is something like:
The single quote on the respective second lines tells VBA to ignore the rest of the text on that line, and our VBA commands start on the empty lines.
Goto next heading standard VBA method
After perusing the movement methods available using the Selection object in VBA, one of the obvious choices is the GoToNext method:
This command works with a list of document content types, so we need to tell Word What type of document content to jump to it in the document.
The constants wdGoToHeading is a number, so we just assign it to the What option using a colon equals := symbol. Some other options exist to control the jump, but we’ll focus on what we need here for brevity.
Goto previous heading
If we want the previous heading instead, we use the complimentary GoToPrevious method:
That's the only change but see the important caveat below.
Final go to standard headings macros
The final macro to jump to the next standard heading is:
For the previous heading, the macro is slightly different:
I assign these to Command+Option+Up and Command+Option+Down, respectively, in Word for Mac and Control+Alt+Up and Control+Alt+Down in Word for Windows.
I’m not advocating abandoning the mouse, but in some cases, jumping between headings using the keyboard is more direct and thus faster.
Limitations of the GoTo commands for headings
A quick caveat is the Goto command when used for headings restricts us to jumping only to standard Word heading styles Heading 1 – 9. The extended content below is one attempt at creating versions that also apply to derived heading styles.
Go to a heading for derived styles
The Selection's GoTo method along with its sibling methods give detailed control over the content type and destination for our navigation, but when applied to headings, they only work for the standard Word heading styles Heading 1 through 9.
I don't like that.
I use the standard heading styles or work and novel notes documents, but I also use derived heading styles in my novels like Act, Chapter, Scene, and Subscene. A better macro should ideally apply to standard or derived heading styles.
Fortunately, we have another tool at our disposal in the form of last week’s Select Headings article. Bookmarks.
Bookmarks … what?
The previous article addresses using a Bookmark for manipulating headings, so we’ll present an abbreviated version here.
Brief explanations of a Bookmark and a Range in Word
In VBA, a Word Bookmark (capital B) contains a Range (capital R) of spanned content. Both are "objects" in VBA meaning they are their own data types with associated stored information (properties) and actions (methods) that allow us to manipulate them and the related document content.
In Word, a Range is a span of document content essentially defined by Start and End document position properties. A Bookmark is a slight generalization, but unlike the physical analogy, a Word Bookmark corresponds to a range of document content rather than a specific location in a document. Of course, the range could be empty thus referring to a specific position, but it can span part of a document, in general.
Word predefined bookmarks
Word has some predefined bookmarks which is uses to internally track document content. Although they are hidden from the Bookmarks dialog, we can access them using VBA. Specifically, we want to access the \HeadingLevel bookmark which tracks the current heading level at the insertion point in real time.
Select the current heading range
To set up the heading selection, we need to access the Bookmarks collection of the Selection.
In this macro, we need to tell the collection we want to refer to the \HeadingLevel bookmark. We put this name, including the backslash \, in double quotes and use it as an argument for the collection.
The parentheses are required because we will refer to a method of the resulting Bookmark. We now want to select the range spanned by the Bookmark. Fortunately, the Bookmark has a convenient Select method.
If we were to stop here, the entire heading range would be selected in the document. We instead want to move the insertion point to the respective neighboring heading paragraph.
Collapsing the selection
After selecting the bookmark range, this macro is supposed to jump to the next heading. To finish our task, we can Collapse the selection toward its end.
Collapsing the selection is like tapping either the Left or Right arrow key when there is a selection on screen. The selection goes away, and the insertion point (the blinking I-bar) is positioned at the beginning or ending of the selection, respectively.
The beginning is the default which is not what we need when moving to the next heading.
Collapsing to the end of the selection
The "next" heading corresponds to the end of the current heading content, so we instead collapse toward the end of the selection using the Direction option.
This wdCollapseEnd constant is from a short table, and we just assign the constant value to the Direction option. Only two directions exist for the collapse, so we don’t have to worry about any other options.
Isn’t this a hack solution?
It may be a little confusing to first select something only to then collapse the selection, but as far as I know, this is the only way to jump to derived heading styles. However, even if we used (without explanation) the more intuitive GoTo method using the hidden \HeadingLevel bookmark like:
Using the GoTo method still selects the whole bookmark heading range on the jump because the bookmark spans a range of content. We would still need to collapse the selection anyhow. Just selecting the Range at the start of the macro is a little more intuitive and gives the same result.
I do not like making a potentially large selection in the document only to immediately collapse it, but we gain the additional functionality of using the macro for any type of heading style, derived or standard. Later member articles may cover how to avoid potential screen flicker issues.
Go to previous heading
The macro to jump to the previous heading is almost the same, but we must deal with a gotcha.
Collapsing the selection
After selecting the bookmark range, this macro is supposed to jump to the previous heading. To finish our task, we can Collapse the selection toward its start.
Collapsing toward the beginning is the default, so we are done with the collapse step.
Goto previous heading gets stuck
The jump to the previous heading will get “stuck” at the current heading because every time it is run, it will select the same heading content. It immediately "finds" the "previous" heading and stops. Then when you collapse the selection, you’re still at the same location.
A simple solution consists of moving one character to the left using the MoveLeft method before running the other macro steps.
The steps one character to the left (the default) which places the insertion point just prior to the last paragraph marker in the previous heading content. It ensures that we will always move past the current heading if we are at its exact beginning, and it never hurts other cases if our insertion point is anywhere else inside the heading content.
This is a bit of a hack fix in the spirit of duct tape handymen around the world (well, those that do VBA programming in Word), but as far as I am aware, it works for all cases.
Final go to heading macros
The final macro to jump to the next heading with standard or derived heading styles:
The macro to jump to the previous heading is slightly different.
The insertion point (or initial selection) can start at any location within the heading level, and it still jumps to the start or end of the indicated heading.
I assign these macros to Command+Option+Up and Command+Option+Down, respectively, in Word for Mac and Control+Alt+Up and Control+Alt+Down in Word for Windows. I use these more when editing article content but also in novels and notes documents. I don’t need them all the time, but they’re handy when I do.
Caveats on how they work
These macros behave a little differently compared to the originals above.
Navigation difference
Selecting the heading using the \HeadingLevel bookmark selects all subordinate content, even subheadings and their content under your current heading level. The practical effect in the revised macro is it jumps to the next heading with the same level. That is, it will skip any headings and content with a lower outline level.
This is different from the original GotoNextHeading macro up top using the Selection's GoToNext command. That version jumps to the next heading of any level.
This feature is, of course, a matter of preference.
Asymmetry in which heading level
An asymmetry exists in how these new next and previous versions of the macros act. The new GotoNextHeading will jump to the next heading of the same level, but the previous version jumps to a heading of any level.
I do not like this asymmetry, but given the VBA tools we can access, the solution is more involved than it might seem on the surface and is outside the scope of this article. Upcoming member articles may cover extensions which handle all the special cases consistently provided enough community interest exists.
Improvements
What could we do to make the macros better?
Use a range variable
In this macro, if we select the entire heading and content and then collapse the selection, we may see some screen flicker with the large initial selection. Testing seems to show little to no flicker, but using a range variable (see our brief introduction) would allow us to select only the final result and not risk seeing any intermediate macro steps.
Make the heading level navigation consistent
Intuitively, we want the heading navigation to either jump to the next or previous heading of the same level or any level. The former at the top work for any level, but they are restricted to standard heading styles. The latter above work slightly differently, but the solution requires multiple non-trivial steps. If enough interest exists, it may appear as member content.