discover + control which headings are expanded+collapsed in Navigation Pane from Word VBA? RRS feed

  • Question

  • Is there a way to do this from Word VBA?

    Often when I'm working, I like to have certain headings in the Navigation Pane collapsed at certain levels, and others open, depending on the project. It is a great help when working on large documents.

    However, after I save and close my document, when I reopen it, Word doesn't remember which were collapsed or expanded, it just opens with them all expanded. And then I have to expand/collapse them all over again to get them the way I want, wasting precious time.

    Right-clicking on the navigation pane shows menu options including Expand All, Collapse All, Show Heading Levels > (All, Show Heading 1 thru 9). I'm looking for more granular control, as it would be helpful if Word would remember the state of the collapsed/expanded headings for a document (or better yet multiple states!).

    I would be interested in creating this feature with VBA macros. Such a macro would iterate the expanded/collapsed state of each heading in the Navigation Pane, save that information somewhere[1], and recall it later. In this way you could even let the user save this configuration under a name, and save/recall different "snapshots" of the Navigation Pane state per document.

    If a heading is no longer present the macro could just skip to the next one, notify the user, or even auto-delete it from the metadata, whatever.

    So my question is, does anyone have any information on how this might be accomplished? I haven't found anything about a navigation pane object in the object browser or googling MSDN.

    Any help appreciated...


    [1] (There are a bunch of different ways to save custom metadata from/for your macros in VBA, anything from storing them in a "settings" Word table within the document, an external text file, a hidden ActiveX textbox in the document, in the registry, maybe some other document fields, an external database, even out on the cloud, doesn't matter.)


    Wednesday, September 11, 2019 2:04 PM

All replies

  • I haven't attempted this, but as far as I can tell, all the object model really gives you is the capability to display/hide the navigation pane (by setting the relevant Window's .DocumentMap to True or False) and use SendKeys to navigate. E.g. with the default key assignments, you can send ^f to get to the search box, then you may be able to navigate from there.

    Since the Selection moves as you move through the Headings (assuming you can reliably get to the headings) you could record the Selection.Start related to each heading. Then, to restore the navpane, I think you would have to use send keys again to step through the Headings, and decide whether or not to collapse a tree depending on your saved data. 

    Since Word probably maintains all the data it needs to do exactly what you are suggesting, if you think this would be useful for others it might be worth suggesting a new feature on .

    Peter Jamieson

    Thursday, September 12, 2019 8:34 AM