How to use VBA to document custom styles in a Microsoft Word document


Image: monticellllo/Adobe Stock

Styles are a huge part of working efficiently in Microsoft Word. If you work with files or templates that contain a lot of custom styles, you would welcome a list of them, but there’s no built-in Word feature that displays a list of all styles. In this Word tutorial, I’ll show you how to create a VBA procedure that inserts the name of all styles – custom, built-in or both – in the current document. You can print the document out for documentation purposes or for referencing.

SEE: Learn how much more Microsoft can do for you with this training bundle (TechRepublic Academy)

I’m using Microsoft 365 desktop on a Windows 64-bit system, but you can use earlier versions. For your convenience, you can download the demonstration .docm and .doc files. Word for the web doesn’t support VBA procedures.

What you need to know about the VBA procedure in Word

The following VBA procedure, aka macro, does a lot. First, the procedure prompts the user to choose between a list of built-in styles, custom styles or all styles. After the user responds, the procedure opens a new document and generates a list of the appropriate styles. Finally, the procedure displays the number of styles generated. The procedure does not save this Word document. It doesn’t sound like much considering the number of lines in the procedure.

Sub ListStyles()

'Generate a list of custom (1), built-in (2), or all Word styles (3)

'in the active document.

Dim doc As Document

Dim sty As Style

Dim inp As Integer

Dim i As Integer

i = 0

'Request input value from user.

On Error GoTo errHandler: 'Catches Cancel or X.

inp = InputBox("Please enter one of the following values 1, 2, or 3." _

& vbLf & vbLf & _

"1. Custom styles" & vbLf & _

"2. Built-in styles" & vbLf & _

"3. All styles", _

"Print a list of styles", 1)

'Check for invalid input value.

If inp >= 4 Or inp < 1 Then

MsgBox "Please enter a valid input value: 1, 2, or 3.", vbOKOnly, _

"Input Error"

Exit Sub

End If

On Error GoTo errHandler

Set doc = Documents.Add

'Cycle through styles in active document.

With doc

For Each sty In .Styles

If inp = 1 And sty.BuiltIn = False Then

.Range.InsertAfter sty.NameLocal & vbCr

i = i + 1

ElseIf inp = 2 And sty.BuiltIn = True Then

.Range.InsertAfter sty.NameLocal & vbCr

i = i + 1

ElseIf inp = 3 Then

.Range.InsertAfter sty.NameLocal & vbCr

i = i + 1

'No Else because earlier code catches any value other than 1, 2, or 3.

End If

Next sty

End With

MsgBox i & " styles found.", vbOKOnly

Set doc = Nothing

Exit Sub

errHandler:

If Err.Number = 13 Then Exit Sub

MsgBox Err.Number & " " & Err.Description

Set doc = Nothing

End Sub

About half of the code checks for input errors. After declaring a few variables, the code displays an input box prompting the user to enter one of the three values:

  • Custom styles
  • Built-in styles
  • All styles

The first On Error statement catches the error thrown if the user clicks Cancel or X. In this case, the errHandler block exits the procedure:

If Err.Number = 13 Then Exit Sub.

By clicking Cancel or X, the code assumes the user doesn’t want to run the procedure after all so the code doesn’t display an explanatory message box.

If the user responds to the message box by entering a value, the procedure stores that input value in an integer variable named inp. If inp equals anything other than 1, 2 or 3, the code displays a message box requesting that the user enter one of the specified values – 1, 2 or 3 – and then the procedure quits.

When inp equals 1, 2 or 3, a For Each block runs more checks using If statements:

  • If inp equals 1, the user wants to print a list of custom styles. When the next check, sty.BuiltIn = False is true, sty is a custom style, so the statement adds the style name to the list and adds 1 to the i counter.
  • If inp equals 2, the user wants to print a list of built-in styles. When the check, sty.BuiltIn = True is true, sty is a built-in style, so the statement adds the style to the list and adds 1 to the i counter.
  • If inp equals 3, the user wants to print a list of all styles so there’s no check; the code adds the style to the list and adds 1 to the i counter.

The For Each ends when the procedure has cycled through all the styles in the active document. The last few statements display a message with the number of styles listed and then destroys the doc object and exits the Sub procedure. The error handling displays the number and description of any error. Most of the code is catching invalid input values, which is why there’s so much of it.

Now that you’ve had a look at the procedure, it’s time to enter it into a Word document.

SEE: Windows, Linux, and Mac commands everyone needs to know (free PDF) (TechRepublic)

How to enter the VBA procedure in Word

You will likely want to run this procedure in lots of documents and not just the same one. If that’s the case, consider creating a template file (.dotm). Another alternative is to add the procedure to the Personal.xlsb workbook, which grants access to the procedure from any open workbook file. The demonstration file is a macro-enabled file, .docm, because it’s the easiest to use for our purposes.

If you are using a ribbon version, be sure to save the workbook as a macro-enabled file. If you’re working in the menu version, you can skip this step. If you’re working on your own and aren’t using the downloadable demonstration file, add a custom style to your file so you can see how all three choices work.

To enter the procedure, press Alt + F11 to open the Visual Basic Editor. In the Project Explorer to the left, select ThisDocument. If you have more than one Word file open, be sure to select the right one. You can enter the code manually or import the downloadable .cls file. In addition, the procedure is in the downloadable .docm file. If you enter the code manually, don’t paste from this web page. Instead, copy the code into a text editor and then paste that code into ThisDocument. This will remove any phantom web characters that might cause errors.

Save the file and return to the Word document. You’re ready to run the VBA procedure to see how it works.

How to run the VBA procedure in Word

Now comes the easy part: Running the procedure. To do so, click the Developer tab and then click Macro in the Macros group. In the resulting dialog, choose ListStyles (Figure A) and click Run.

Figure A

Run the VBA procedure ListStyles in Word.
Run the VBA procedure ListStyles in Microsoft Word

The message box defaults to the first choice, 1. Custom styles (Figure B). Click OK without changing that input value.

Figure B

In Word, choose 1. Custom styles and click OK.
In Microsoft Word, choose 1. Custom styles and click OK.

As you can see in Figure C, the demonstration file has one custom style: MyCustomStyle. Be sure to save this file manually if you want to keep it for documentation purposes.

Figure C

The VBA procedure inserts the custom style into the active document and then displays the number of custom styles found in the current document—1.
The VBA procedure inserts the custom style into the active document and then displays the number of custom styles found in the current document—1.

Run the procedure again and choose 2 for built-in styles. Figure D shows the more than 300 results, If you don’t see ListStyles, you might be in the new document the procedure created during the first run. Be sure to return to your macro-enabled file.

Figure D

In Microsoft Word, there are more than 300 built-in styles.
In Microsoft Word, there are more than 300 built-in styles.

Continue to run the VBA procedure to see how it all works with other input values: Enter 3, click Cancel, enter 0 and 4, and so on.

You don’t want to work through all those steps to run this macro each time you need it. I recommend that you add it to the Quick Access Toolbar or a custom group. If you need help with that, read my TechRepublic articleHow to add Office macros to the QAT toolbar for quick access.

If you don’t know how to enter this procedure in Personal.xlsb so it’s available to all workbooks, read my TechRepublic article, How to create a VBA procedure that closes all open workbooks in Excel, which includes instructions for using Personal.xlsb.



Source link

Latest Articles

More From Us

Leave a reply

Please enter your comment!
Please enter your name here