Saving Styles in a Macro

Written by Allen Wyatt (last updated January 1, 2022)
This tip applies to Word 2007, 2010, 2013, 2016, 2019, Word in Microsoft 365, and 2021


Raj wonders whether styles can be saved in a Word macro and run later as part of a "sanity check" to make sure that only the template styles are applied to a document and nothing else.

The answer to this question is actually much more complicated than you might surmise. If Raj is wanting to stop the user from adding styles either directly (by creating new styles) or indirectly (by using copy and paste from other sources), then there are actually several things you can try, including a few macros. These have been covered in the following (em>WordTip:

https://wordribbon.tips.net/T012698

Beyond what is covered in that tip, if all you want to do is check to see whether the styles used in a document are on an "approved list" of styles, then it is a relatively simple task. The names of styles can easily be stored in a string array and then compared to the styles applied to each paragraph in a document. You can step through the paragraphs in a document and examine whether the styles are in the approved list using a structure similar to this one:

sTemp = ""
For Each p In ActiveDocument.Content.Paragraphs
    sPStyle = p.Style
    bNotFound = True
    For J = 1 To iNumValid
        If sPStyle = sValidStyles(J) Then bNotFound = False
    Next J
    If bNotFound Then
        sTemp = sTemp & sPStyle & vbCrLf
    End If
Next p

This example is simply a code snippet; your full macro would need to set up a string array (sValidStyles) that contains all the valid style names along with iNumValid which is the number of array elements. When the snippet is complete, the sTemp variable contains the names of all the styles used in the document that aren't in the sValidStyles array. (If an invalid style appears in the document more than once, it will appear in sTemp more than once.)

If you are looking for a macro that will actually do a "sanity check" to see if a style has been modified, then the problem is astronomically more complex. Styles can have scores or hundreds of possible settings and storing each of those settings for each valid style would be a nightmare. In that situation it is better to go with the protection-based approaches detailed in the tip linked to above.

Note:

If you would like to know how to use the macros described on this page (or on any other page on the WordTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

WordTips is your source for cost-effective Microsoft Word training. (Microsoft Word is the most popular word processing software in the world.) This tip (12959) applies to Microsoft Word 2007, 2010, 2013, 2016, 2019, Word in Microsoft 365, and 2021.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Strikethrough Shortcut Key

One common type of formatting is strikethrough, which is normally applied from the Font dialog box. There is no built-in ...

Discover More

Getting Rid of the Paste Options Box

Paste something in a Word document, and you may notice a dynamic little set of options appear right next to what you ...

Discover More

Moving the Insertion Point to the Beginning of a Line

If you need to move the insertion point within your macro, then you'll want to note the HomeKey method, described in this ...

Discover More

Comprehensive VBA Guide Visual Basic for Applications (VBA) is the language used for writing macros in all Office programs. This complete guide shows both professionals and novices how to master VBA in order to customize the entire Office suite for their needs. Check out Mastering VBA for Office 2010 today!

More WordTips (ribbon)

Reformatting a Document with Messed-Up Styles

If there are lots of hands that touch a document, there are lots of ways those hands can mess up the document. You may be ...

Discover More

Can't Find or Delete a Style

Using the shortcuts available in the Styles pane to select text can be very helpful. It may, however, be quite ...

Discover More

Deleting a Large Number of Styles

Styles are a fantastic tool for formatting documents. As you work with documents created by others, you may want to get ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] (all 7 characters, in the sequence shown) in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is 7 - 6?

There are currently no comments for this tip. (Be the first to leave your comment—just use the simple form above!)


This Site

Got a version of Word that uses the ribbon interface (Word 2007 or later)? This site is for you! If you use an earlier version of Word, visit our WordTips site focusing on the menu interface.

Videos
Subscribe

FREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.