Headings provide document content organization, but few native Word commands exist to manipulate them. Toward that goal, we create a Word macro to select a heading and its content. Even better, the technique works for either standard or derived heading styles.
Thanks for your interest
This content is part of a paid plan.
Select heading content
Headings organize our documents at a level somewhere between paragraphs and sections, but few Word commands exist to manipulate them. If we want to work efficiently with headings, we need to create our own tools.
One common task is to select things to enable us to take other actions. We can select words, lines, or paragraphs using keyboard shortcuts; but there is no stock way to select a heading or its content other than doing so manually. Do we always have to grab the mouse or tap furiously on the keyboard to select everything? Of course not. That’s why you’re here, and this is a nice one-liner today.
The most obvious method to accomplish this task is probably using the Selection's GoTo commands along with some range variables, but such an approach is unnecessarily complicated. A better one-line command exists if we instead take advantage of a hidden bookmark Word defines for every document. Word has other predefined bookmarks if you’re interested.
Create the empty macro
Since we're starting from scratch, a previous post covers creating an empty macro in the VBA editor. The result is:
The single quote on the second line tells VBA to ignore the rest of the text on that line, so our VBA command will be placed on the empty line.
A bookmark? Really?
Intuitively, you probably think of a bookmark as just a location in a document since that’s how we physically use them in a real book, but in Word, a bookmark includes a Range of content as well as other data (called properties) and actions (called methods). If it helps with the physical analogy, you might think of a bookmark as marking the pages of content rather than just a specific location.
The command to select our heading is not as obvious as some we’ve covered previously, but it's only one line, and it's more versatile than doing it by a more obvious, direct way.
What is a document Range?
Ranges have a specific meaning in VBA including various properties and methods. We previously covered them in more detail, but at a basic level, a Range contains Start and End positions (as character positions) in the document indicating the spanned document content.
Bookmarks and document Ranges
How can we get the heading range using a bookmark? Word stores all bookmarks in a Bookmarks collection of the Selection.
Technically, the Bookmarks collection stores only those bookmarks contained within the currently selected content, but this automatically includes the standard Word bookmarks. We provide the collection with a bookmark name \HeadingLevel as plain text in double quotes.
A backslash \ is the first character indicates it is a standard hidden Word bookmark. The parentheses are needed since we’re going to do something with the bookmark below.
Now we need to select the document range corresponding to the pre-defined Word Bookmark. As mentioned, a Bookmark is essentially a Range in the document, so it contains a convenient Select method to select it without having to refer to its document range as an intermediate step:
That's it. Just one line.
Final select heading macro
The final macro is wonderfully simple:
I use this more when editing blog or newsletter content, but it also finds use in my novels and notes documents. The insertion point can start at any location within the heading level, and it still selects all the heading content, including any subordinate headings under the current heading level. It’s especially handy when the heading content extends off the current screen.
I assigned this macro to Command+Option+Numpad5 in Word for Mac or Control+Alt+Numpad5 in Word for Windows. These shortcuts are a little odd for most people, but I wanted something relatively easy to remember while not conflicting with any of my other editing shortcuts.
Works for derived headings
The best thing here is this works even for derived style headings not just the standard Word styles Heading 1 – 9.
I personally include a lot of headings in my novels as derived styles using novel-like divisions of Act, Chapter, Scene, and Subscene. Without getting too far afield, this allows me to better watch the progression of my story as it develops. When I get ready to publish the novel or give it to a beta reader, I use another macro to strip all the heading related paragraphs other than chapters from my document.
Improvements and enhancements
What are some interesting, useful variations or enhancements?
We could jump between headings, select the just the heading content or just the remainder of the heading text. Quickly expanding or extending which heading is selected is also useful. That’s one of the nice things about macros. We can mold them to make Word work our way.
I sometimes want to select the heading content without including the main heading paragraph, so let's work on that variation now. It's a simple change.
Select only the heading content
We can strip away the first paragraph (the main heading) at the beginning of the heading. It’s slightly more complicated than it sounds, but it's still only one extra step.
Where is the second paragraph?
If we want to exclude the first "heading paragraph," we need to know when it ends. The Selection has a collection of Paragraphs (capital P) fully or partially contained within the spanned document range.
We can access the first paragraph's Range using the convenient First property.
A Paragraph (capital P) is an object in VBA with contains or spans a document range, but it also contains other properties and methods that allow us to manipulate them as needed. As such, we need to specifically refer to the Paragraph's range for our purposes in this macro.
But we don’t want the first paragraph. We want the beginning of the second paragraph. The easiest way is to use the End of the first paragraph range because this position coincides with the Start of the second paragraph range.
Huh?
But … I want the second paragraph!
For those questioning the solution above, Word considers a content unit range (a paragraph in this case) to extend all the way to the beginning of the next unit of that type. The first paragraph range extends up to the beginning of the second paragraph range, so the End position of the first paragraph range has the same document position as the Start of the second paragraph range.
Makes sense once you think about it a bit. Our living room extends all the way to the kitchen. The end of one constitutes the beginning of the other.
If that explanation still bothers you, you could instead refer to the beginning of the second paragraph using an index of 2 in parentheses on the Paragraphs collection like so:
We refer to the Start position of the second paragraph's range because that's where we want to mark the beginning of the "heading content." It's not as clean as just referring to the End of the First paragraph, but it's more intuitive, and it works ... most of the time.
But there is a problem.
What if the heading is just the heading paragraph?
If the heading consists of just the heading paragraph with no content paragraphs underneath it, we can still refer to the first paragraph and get it's End position. However, if this is the case, VBA will say it's an error to attempt to refer to a non-existent second paragraph.
As a result, it's safer to use the First Paragraph version. No one likes a macro to crash in their face, and the difference is not worth adding conditional statements (annoying extra steps) or an error handler (way overkill). The simple reference to the First Paragraph neatly solves the problem.
Adjust the Selection Start position
Now we adjust the beginning of our heading content Selection to exclude the heading paragraph. We’ve already selected the heading range above using the standard Bookmark \HeadingLevel, so now we’re changing its Start position instead. The Selection has a shorthand way to refer to the Start position of its range.
The Start and End positions of a range are literally just character positions as counted from the beginning of the document. We need to reset the Start of the current Selection to the End of the first paragraph we identified above. Since they are just numbers, we can literally just set the two numbers equal = to each other.
We only needed one extra macro step to isolate the heading content, but it did require a little more understanding of Ranges and how Word views them to find the best approach.
Final macro variant
Just in case you don’t quite feel confident enough yet to assemble the pieces, here is the revised final macro.
Have fun while you're editing faster.