The way to use VBA to doc customized types in a Microsoft Phrase doc

Picture: monticellllo/Adobe Inventory

Kinds are an enormous a part of working effectively in Microsoft Phrase. When you work with information or templates that include quite a lot of customized types, you’ll welcome an inventory of them, however there’s no built-in Phrase function that shows an inventory of all types. On this Phrase tutorial, I’ll present you easy methods to create a VBA process that inserts the identify of all types – customized, built-in or each – within the present doc. You may print the doc out for documentation functions or for referencing.

SEE: Learn the way rather more Microsoft can do for you with this coaching bundle (TechRepublic Academy)

I’m utilizing Microsoft 365 desktop on a Home windows 64-bit system, however you should utilize earlier variations. In your comfort, you possibly can obtain the demonstration .docm and .doc information. Phrase for the online doesn’t help VBA procedures.

What you must know concerning the VBA process in Phrase

The next VBA process, aka macro, does rather a lot. First, the process prompts the person to decide on between an inventory of built-in types, customized types or all types. After the person responds, the process opens a brand new doc and generates an inventory of the suitable types. Lastly, the process shows the variety of types generated. The process doesn’t save this Phrase doc. It doesn’t sound like a lot contemplating the variety of traces within the process.

Sub ListStyles()

'Generate an inventory of customized (1), built-in (2), or all Phrase types (3)

'within the energetic doc.

Dim doc As Doc

Dim sty As Model

Dim inp As Integer

Dim i As Integer

i = 0

'Request enter worth from person.

On Error GoTo errHandler: 'Catches Cancel or X.

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

& vbLf & vbLf & _

"1. Customized types" & vbLf & _

"2. Constructed-in types" & vbLf & _

"3. All types", _

"Print an inventory of types", 1)

'Examine for invalid enter worth.

If inp >= 4 Or inp < 1 Then

MsgBox "Please enter a legitimate enter worth: 1, 2, or 3.", vbOKOnly, _

"Enter Error"

Exit Sub

Finish If

On Error GoTo errHandler

Set doc = Paperwork.Add

'Cycle by means of types in energetic doc.

With doc

For Every sty In .Kinds

If inp = 1 And sty.BuiltIn = False Then

.Vary.InsertAfter sty.NameLocal & vbCr

i = i + 1

ElseIf inp = 2 And sty.BuiltIn = True Then

.Vary.InsertAfter sty.NameLocal & vbCr

i = i + 1

ElseIf inp = 3 Then

.Vary.InsertAfter sty.NameLocal & vbCr

i = i + 1

'No Else as a result of earlier code catches any worth aside from 1, 2, or 3.

Finish If

Subsequent sty

Finish With

MsgBox i & " types discovered.", vbOKOnly

Set doc = Nothing

Exit Sub

errHandler:

If Err.Quantity = 13 Then Exit Sub

MsgBox Err.Quantity & " " & Err.Description

Set doc = Nothing

Finish Sub

About half of the code checks for enter errors. After declaring a number of variables, the code shows an enter field prompting the person to enter one of many three values:

  • Customized types
  • Constructed-in types
  • All types

The primary On Error assertion catches the error thrown if the person clicks Cancel or X. On this case, the errHandler block exits the process:

If Err.Quantity = 13 Then Exit Sub.

By clicking Cancel or X, the code assumes the person doesn’t wish to run the process in any case so the code doesn’t show an explanatory message field.

If the person responds to the message field by getting into a worth, the process shops that enter worth in an integer variable named inp. If inp equals something aside from 1, 2 or 3, the code shows a message field requesting that the person enter one of many specified values – 1, 2 or 3 – after which the process quits.

When inp equals 1, 2 or 3, a For Every block runs extra checks utilizing If statements:

  • If inp equals 1, the person desires to print an inventory of customized types. When the subsequent verify, sty.BuiltIn = False is true, sty is a customized fashion, so the assertion provides the fashion identify to the record and provides 1 to the i counter.
  • If inp equals 2, the person desires to print an inventory of built-in types. When the verify, sty.BuiltIn = True is true, sty is a built-in fashion, so the assertion provides the fashion to the record and provides 1 to the i counter.
  • If inp equals 3, the person desires to print an inventory of all types so there’s no verify; the code provides the fashion to the record and provides 1 to the i counter.

The For Every ends when the process has cycled by means of all of the types within the energetic doc. The previous couple of statements show a message with the variety of types listed after which destroys the doc object and exits the Sub process. The error dealing with shows the quantity and outline of any error. Many of the code is catching invalid enter values, which is why there’s a lot of it.

Now that you simply’ve had a take a look at the process, it’s time to enter it right into a Phrase doc.

SEE: Home windows, Linux, and Mac instructions everybody must know (free PDF) (TechRepublic)

The way to enter the VBA process in Phrase

You’ll doubtless wish to run this process in a lot of paperwork and never simply the identical one. If that’s the case, contemplate making a template file (.dotm). One other different is so as to add the process to the Private.xlsb workbook, which grants entry to the process from any open workbook file. The demonstration file is a macro-enabled file, .docm, as a result of it’s the simplest to make use of for our functions.

If you’re utilizing a ribbon model, remember to save the workbook as a macro-enabled file. When you’re working within the menu model, you possibly can skip this step. When you’re working by yourself and aren’t utilizing the downloadable demonstration file, add a customized fashion to your file so you possibly can see how all three selections work.

To enter the process, press Alt + F11 to open the Visible Fundamental Editor. Within the Venture Explorer to the left, choose ThisDocument. When you’ve got a couple of Phrase file open, remember to choose the suitable one. You may enter the code manually or import the downloadable .cls file. As well as, the process is within the downloadable .docm file. When you enter the code manually, don’t paste from this internet web page. As a substitute, copy the code right into a textual content editor after which paste that code into ThisDocument. It will take away any phantom internet characters which may trigger errors.

Save the file and return to the Phrase doc. You’re able to run the VBA process to see the way it works.

The way to run the VBA process in Phrase

Now comes the straightforward half: Working the process. To take action, click on the Developer tab after which click on Macro within the Macros group. Within the ensuing dialog, select ListStyles (Determine A) and click on Run.

Determine A

Run the VBA process ListStyles in Microsoft Phrase

The message field defaults to the primary alternative, 1. Customized types (Determine B). Click on OK with out altering that enter worth.

Determine B

In Word, choose 1. Custom styles and click OK.
In Microsoft Phrase, select 1. Customized types and click on OK.

As you possibly can see in Determine C, the demonstration file has one customized fashion: MyCustomStyle. Remember to save this file manually if you wish to hold it for documentation functions.

Determine 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 process inserts the customized fashion into the energetic doc after which shows the variety of customized types discovered within the present doc—1.

Run the process once more and select 2 for built-in types. Determine D exhibits the greater than 300 outcomes, When you don’t see ListStyles, you is likely to be within the new doc the process created through the first run. Remember to return to your macro-enabled file.

Determine D

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

Proceed to run the VBA process to see the way it all works with different enter values: Enter 3, click on Cancel, enter 0 and 4, and so forth.

You don’t wish to work by means of all these steps to run this macro every time you want it. I like to recommend that you simply add it to the Fast Entry Toolbar or a customized group. When you need assistance with that, learn my TechRepublic articleHow so as to add Workplace macros to the QAT toolbar for fast entry.

When you don’t know easy methods to enter this process in Private.xlsb so it’s accessible to all workbooks, learn my TechRepublic article, The way to create a VBA process that closes all open workbooks in Excel, which incorporates directions for utilizing Private.xlsb.

Keep linked with us on social media platform for on the spot replace click on right here to affix our  Twitter, & Fb