TL/DR: I have a game plan on how to do this below; however, I am wondering if my plan is going to prove to be too complicated, and what additional considerations I need to take into account before diving into building this project. Although I am not an experienced programmer, I am NOT asking for code; I am asking for feedback from experienced Word VBA programmers as to whether my entire idea/approach is one huge mistake.
I have a document "template" (not yet a template file type - I hope to create that as described below) for a report. The report is broken up into different sections:
Letter to the Client
Table of Contents
Section I
Title Page
Body
1.0
2.0
Section II
Title Page
Body
1.0
2.0
Appendix A
Title Page
Body
Appendix B
Title Page
Body
I want each major "metasection" (such as Letter, Section I, Section II, Appendices) to have different styling and formatting. This could be accomplished by having multiple styles for each metasection, e.g.:
Normal-Letter
Normal-SectionI
Normal-Appendices
Heading1-Letter
Heading1-SectionI
Heading1-Appendices
This would quickly become unmanageable.
In order to avoid users having to wade through a huge number of styles to find the correct one (and it is worth noting that if users of this report have to do this, they will likely not use styles AT ALL), it would be nice if I could have the same style name (e.g, Normal
) be different depending on which section of the document it is found in. Or said another way, I would like for a document to have multiple style sets depending on the section.
The goals for the user experience are:
- The user simply applies the
Normal
style,Heading1
style, etc, as necessary. - Registered section-specific style definitions are updated when styles are edited via the
Modify Style
dialog box, or other ways. - The styles are applied automatically and transparently when styles are changed, or when the document is opened, saved, or printed.
- ALTERNATIVE: If automatic/transparent style application proves too difficult, execute the style-application routine with a simple command button.
My initial idea on how I might do this in VBA is:
- Write VBA code (probably a class) such that there is a style registry of
Normal
s andHeading1
s, etc., for each document section. - Write a style-application subroutine which iterates through the registered document sections, selects all the parts with each registered style, and applies the section-specific style from the style registry (preserving any styling that deviates from the style definition).
- Write a style-update subroutine that automatically and transparently updates the registered style definitions
- The style-application subroutine executes any time styling is applied anywhere in one of the registered sections (so I'll need to tie into Events here).
- The style-update subroutine executes any time a style definition in a registered section is changed (so here's another Event I'll need to monitor).
I previously asked a similar question about this topic on Superuser. The feedback I received has led me to believe that I can only accomplish the behavior I want using VBA, so I am now asking a follow-up question here on Stackoverflow.
My question is: am I making a mistake here? I have a feeling there is a better way to solve this problem (perhaps using VBA, perhaps not) than this.
Sections
which are obvious.ContentControls
to easier manage portion of documents andBuildingBlocks
for quick insertion of schemas/themes/styles. – Kazimierz JaworBookmarks
in my opinion are designed mostly to get quick reference to a certain part of document. User can easily remove it. If you replacebookmark
content with VBA bookmark, by default, will be remove which is a (small) problem. BothBuildingBlocks
andContentControls
has some events onDocument object level
which give you a lot of opportunities to control, bloc, trigger actions as a result of user action. Check this link for Document Events – Kazimierz JaworContentControls
and related events. There you will find aBookmark
solution as well. – Kazimierz Jawor