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

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

How to use VBA to document custom styles in a Microsoft Word document – TechRepublic 0 0 Alan Dickson

Register for your free TechRepublic membership or if you are already a member, sign in using your preferred method below.
We recently updated our Terms and Conditions for TechRepublic Premium. By clicking continue, you agree to these updated terms.
Invalid email/username and password combination supplied.
An email has been sent to you with instructions on how to reset your password.
By registering, you agree to the Terms of Use and acknowledge the data practices outlined in the Privacy Policy.
You will also receive a complimentary subscription to TechRepublic’s News and Special Offers newsletter and the Top Story of the Day newsletter. You may unsubscribe from these newsletters at any time.
All fields are required. Username must be unique. Password must be a minimum of 6 characters and have any 3 of the 4 items: a number (0 through 9), a special character (such as !, $, #, %), an uppercase character (A through Z) or a lowercase (a through z) character (no spaces).
How to use VBA to document custom styles in a Microsoft Word document
Your email has been sent
This VBA macro generates a list of Microsoft Word styles that are custom, built-in or both.
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.
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:
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:
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)
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.
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
The message box defaults to the first choice, 1. Custom styles (Figure B). Click OK without changing that input value.
Figure B
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
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
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.
Are you interested in learning more about VBA in Excel? See these resources in TechRepublic Academy:
Be your company’s Microsoft insider by reading these Windows and Office tips, tricks, and cheat sheets.
How to use VBA to document custom styles in a Microsoft Word document
Your email has been sent
Your message has been sent
TechRepublic Premium content helps you solve your toughest IT issues and jump-start your career or next project.
Windows 11 gets an annual update on September 20 plus monthly extra features. In enterprises, IT can choose when to roll those out.
Edge AI offers opportunities for multiple applications. See what organizations are doing to incorporate it today and going forward.
This is a complete guide for Apple’s iPadOS. Find out more about iPadOS 16, supported devices, release dates and key features with our cheat sheet.
Discover data intelligence solutions for big data processing and automation. Read more to explore your options.
Whether you are a Microsoft Excel beginner or an advanced user, you’ll benefit from these step-by-step tutorials.
This document helps make sure that you address data governance practices for an efficient, comprehensive approach to data management. This checklist from TechRepublic Premium includes: an introduction to data governance, a data governance checklist and how to manage a data governance checklist. From this checklist’s introduction: Data governance is the process by which an organization …
Recruiting a Scrum Master with the right combination of technical expertise and experience will require a comprehensive screening process. This hiring kit provides a customizable framework your business can use to find, recruit and ultimately hire the right person for the job. This hiring kit from TechRepublic Premium includes a job description, sample interview questions …
Knowing the terminology associated with Web 3.0 is going to be vital to every IT administrator, developer, network engineer, manager and decision maker in business. This quick glossary will introduce and explain concepts and terms vital to understanding Web 3.0 and the technology that drives and supports it.
While the perfect color palette or the most sublime button shading or myriad of other design features play an important role in any product’s success, user interface design is not enough. Customer engagement and retention requires a strategic plan that attempts to measure, quantify and ultimately create a complete satisfying user experience on both an …

source

    Would you like to receive notifications on latest updates? No Yes