简体   繁体   中英

Using VBA to manage multiple styles definitions in the same Word document

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, eg:

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 (eg, 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:

  1. The user simply applies the Normal style, Heading1 style, etc, as necessary.
  2. Registered section-specific style definitions are updated when styles are edited via the Modify Style dialog box, or other ways.
  3. The styles are applied automatically and transparently when styles are changed, or when the document is opened, saved, or printed.
  4. 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:

  1. Write VBA code (probably a class) such that there is a style registry of Normal s and Heading1 s, etc., for each document section.
  2. 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).
  3. Write a style-update subroutine that automatically and transparently updates the registered style definitions
  4. 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).
  5. 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.

Yes, in my opinion, you are making a mistake. I have just recently finished a project where I have created a document template for a company. My experiences:

  • Users vary in knowledge level (obviously)
  • High level users don't like over-engineered files, because they can't use their own macros as they might conflict with the file's own macros, they can't use their doc properties or their own building blocks etc., as these likely won't be compatible with the macros (or at least they think they won't work, and fiddle around until they actually manage to break them)
  • low level users are intimidated by the automatisms, and keep avoiding them as long as they can (which means as long as their bosses don't order them to use the file), after which point they start hating the file and the work
  • Complex solutions like this one usually get abandoned after a few years. Eg. the original developer changes jobs, or moves to another department, and nobody understands the code enough to keep managing it (especially if it is not a well-documented, well-written code, which it won't be, as you are not an experienced VBA programmer).
  • The developer (you) will be inundated with (sometimes false) bug reports and questions and minor change requests, which gets really annoying after a few weeks (trust me on that :) ). They won't dare change even a font size without consulting you, and in the end, they will ask you to do it. Or, even worse, they try to change something, break it, and then tell you to fix your bug.
  • Your users would have to remember to use section brakes or other kinds of indicators to indicate the next section. This will seem too much for some, too complex, and if they accidentally remove a section indicator (which they inevitably will), all hell brakes lose, and worst of all:
  • Undo function will be disabled after each macro run. This, to most users, is a disaster. You don't do that to your users.

So I would say don't go down the macro route. Don't use Doc properties , that didn't work at the company I was working with. (Actually an IT company, with mostly high-level users :) ) The high-level users will create and use their own doc properties, for others, it is just a hassle. Bookmarks get constantly deleted, so no-go either .

My advice:

  • Use styles. Users will learn to use them quickly.
    • Get a decent document design. Having 4 different sets of title, heading and normal styles in one document is really unprofessional. Consistency is important, especially as this seems to be a letter to you clients. (Yes, I know, your company is different and your bosses are dumb and this is a special case and and and ... Just saying, talk to a designer, and get a professional look for your template.)
    • You can manage the Style gallery (Home tab, centre) drop down list on a template basis - so your template will load the used styles into the dropdown at the top, and remove everything else. This works really good, and even as much as 20 styles is manageable, if they are well-named.
  • Use building blocks: title pages, tables, pre-written and formatted Quick parts (legal mumbo-jumbo, company introduction, contacts, etc.), headers and footers...

And, if you want happy-happy and cooperative users:

  • After creating a blank template, create a full template:
    • Fill up a document template with texts, pre-written paragraphs, pre-written titles, so they will only have to click and rewrite, without the need to format or bother with styles and Cover pages and the lot
  • Educate the users: 2 sessions of 1,5 hour Word class can go a long way. It is a must.

Long post. One last thing: creating a complex Word template, you will be sailing a sea of Word bugs and annoyances. Even without writing macros, this won't be a walk in the park. (I for example gave up on making my TOC work in Office 2013, as after 3 days and 10 versions, it still kept on creating a maximum sized extra paragraph whenever it was inserted. Only in W2013. Still no idea why, but I let it go.)

Whatever you decide to do, best of luck, and have a lot of patience! :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM