Tech

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


how to picture Microsoft Word
Image: monticello / Adobe Stock

Styles are an important part of being productive in Microsoft Word. If you work with files or templates that contain multiple custom styles, you’ll welcome a list of them, but none of the built-in Word features displays a list of all styles. In this Word tutorial, I’ll show you how to create a VBA procedure that inserts the names of all the styles – custom, built-in, or both – in the current document. You can print the document out for reference or documentation.

UNDERSTAND: Learn more how Microsoft can help you with this training package (TechRepublic Academy)

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

What you need to know about the VBA workflow in Word

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

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 three values:

  • Custom style
  • Preset 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 process:

If Err.Number = 13 Then Exit Sub.

By clicking Cancel or X, the code assumes that the user doesn’t want to run the final process, so the code doesn’t show an explanatory message box.

If the user replies 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 asking the user to enter one of the specified values ​​- 1, 2 or 3 – and then the procedure exits.

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

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

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

Now that you’ve gone through the process, it’s time to import it into the Word document.

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

How to enter a VBA procedure in Word

You will probably want to run this procedure in multiple documents, not just the same document. If that’s the case, consider creating a template (.dotm) file. Another alternative is to add the procedure to the Personal.xlsb workbook, granting access to the procedure from any open workbook file. The presentation file is a macro-enabled file, .docm, because it’s the easiest to use for our purposes.

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

To enter the procedure, press Alt + F11 to open the Visual Basic Editor. In the Project Explorer on the left, select ThisDocument. If you open more than one Word file, be sure to select the correct file. You can enter the code manually or import a downloadable .cls file. Additionally, the procedure is in a downloadable .docm file. If you enter the code manually, do not paste from this website. Instead, copy the code into a text editor and then paste it into ThisDocument. This will remove any virtual web characters that might be causing the error.

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

How to run a VBA procedure in Word

Now comes the easy part: Run the procedure. To do so, click the Developer tab, and then click Macro in the Macros group. In the dialog box that appears, select ListStyles (Picture A) and click Run.

Picture A

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

The default message box is the first choice, 1. Custom style (Figure BUT). Click OK without changing that input value.

Figure BUT

In Word, select 1. Custom Style and click OK.
In Microsoft Word, select 1. Custom Style and click OK.

As you can see in SIZE, the demo file has a custom style: MyCustomStyle. Remember to save this file manually if you want to keep it for documentation purposes.

SIZE

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

Run the process again and choose 2 for the preset styles. Visualization showing more than 300 results, If you don’t see ListStyles, you are probably in the new document where the procedure was created on the first run. Make sure to go back to your macro enabled file.

Visualization

In Microsoft Word, there are more than 300 preset styles.
In Microsoft Word, there are more than 300 preset styles.

Continue running the VBA process to see how it all works with other input values: Enter 3, click Cancel, enter 0 and 4, etc.

You don’t want to go through all those steps to run this macro every time you need it. I recommend adding 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 import this process into Personal.xlsb so that it is available to all workbooks, read my TechRepublic article, How to create a VBA procedure that closes all open workbooks in ExcelPersonal.xlsb user guide included.



Source link

news7g

News7g: Update the world's latest breaking news online of the day, breaking news, politics, society today, international mainstream news .Updated news 24/7: Entertainment, Sports...at the World everyday world. Hot news, images, video clips that are updated quickly and reliably

Related Articles

Back to top button