Resting Anchor

The Anchorage

Personal website of Gregory K. Maxey, Commander USN (Retired)

AutoTextList in the Modern Age
(A Microsoft Word Help & Tip page by Gregory K. Maxey)

DISCLAIMER/TERMS OF USE

The information, illustrations and code contained in my "Microsoft Word Tips" are provided free and without risk or obligation.

Click to acces PayPal Verification Service Click to acces PayPal Verification Service

However, the work is mine. If you use it for commercial purposes or benefit from my efforts through income earned or time saved then a donation, however small, will help to ensure the continued availability of this resource.

If you would like to donate, please use the appropriate donate button to access PayPal. Thank you!

Click to donate British Pound Sterling                   Click to donate US dollars                   Click to donate EU euros

This Microsoft Word Tips & Microsoft Word Help page provides a brief description of the use and construction of the AutoTextList field first introduced with Word 97.  Yes, way back then!

With the introduction of content controls in Word 2007, some advanced Word users may feel that the AutoTextField has no place in the modern age.

I am not here to either promote or diminish its usefulness. My goal is simply to show you a VBA method to quickly create and employ an AutoTextField in your documents.

Background

When introduced, the AutoTextList field provided the welcome advantage of a single item selectable dropdown list.  The only alternative, way back then, was to use a protected formfield dropdown list with all baggage it brought along (e.g., suppressed spellchecking and other document tools).

The AutoTextList field leverages the fact (then and now) that Word, whether obviously or not, can organize AutoText entries (and building blocks) by the style applied to the text when the building block is created.

For example, all of the fruit and vegetable items shown in list below are individual building block entries defined and stored in my Normal template.  They are isolated and filtered from the hundreds of other building blocks define in my Normal template because they were all created from text formatted with a unique style named "FVList."  They are displayed in list format using an AutoTextList field. 

autotextlist_in_the_modern_age

As with all Word fields, the AutoTextList field can display the field result or underlying code:

autotextlist_in_the_modern_age_2

Field result

autotextlist_in_the_modern_age_3

Field Code

Site Note IconNote: When you define an AutoTextList field with the user interface or code, a "literal text" attribute is used to set what the field displays (result) before making a selection.  I don't know why this literal text attribute is not displayed in the field code.

autotextlist_in_the_modern_age_4

Build and Employ AutoTextList Fields

A Google search will provide plenty of links to manual step by step procedures for defining and saving building blocks (autotext) entries and for creating an AutoTextList. Duplicating one of those wouldn't be very much fun!

The following is an automated VBA procedure that enables you to create an AutoTextList field (and save the field itself as a separate building block for reuse) with a few simple steps.

VBA Script:
Sub DefineAutoTextList()
  'Passes as arguments "Literal Text", "Sytle", _
  '"Tip Text" and name of AutoTextList Building Block
  BuildATList "FVList", "Select", _
              "Right-click to make your fruit/vegtable selection", "Fruit_Veggies"
lbl_Exit:
  Exit Sub
End Sub

Sub BuildATList(strStyle As String, strLit As String, _
                strTip As String, strListName As String)
Dim oRng As Range, oATRng As Range
Dim oPar As Paragraph
Dim oFld As Field
  On Error Resume Next
  'Creates style if not already present.
  ActiveDocument.Styles.Add (strStyle)
  On Error GoTo 0
  Set oRng = Selection.Range
  'Apply unique style to list items.
  oRng.Style = strStyle
  For Each oPar In oRng.Paragraphs
    'Isolate and define building blocks.
    Set oATRng = oPar.Range
    oATRng.End = oATRng.End - 1
    NormalTemplate.BuildingBlockEntries.Add oATRng.Text, _
                   wdTypeAutoText, "General", oATRng
  Next oPar
  'Define and insert the field.
  Set oFld = ActiveDocument.Fields.Add(oRng, wdFieldAutoTextList, _
                       "" & Chr(34) + strLit & Chr(34) & _
                       " \s " & Chr(34) + strStyle & Chr(34) & " \t " & _
                       Chr(34) + strTip & Chr(34) & "", False)
  oFld.Code.Paragraphs(1).Style = "Normal"
  Set oRng = oFld.Code.Paragraphs(1).Range
  NormalTemplate.BuildingBlockEntries.Add strListName, _
                                          wdTypeAutoText, "General", oRng
  'If you want to get rig of the "Create AutoText ..." _
  'entry at the bottom of the list then unstet and run KillCat:
  'KillCat
  If MsgBox("Changes have been made to your normal template." & vbCr + vbCr _
          & "Do you want to save Normal templage now?", vbQuestion + vbYesNo, _
          "SAVE CHANGES") = vbYes Then
    NormalTemplate.Save
  End If
lbl_Exit:
  Exit Sub
End Sub

Sub KillCat()
Dim lngIndex As Long
Dim oCB As CommandBar
  Set oCB = ActiveDocument.CommandBars("Field AutoText")
  For lngIndex = oCB.Controls.Count To 1 Step -1
    If oCB.Controls(lngIndex).Caption = "&Create AutoText..." Then
      oCB.Controls(lngIndex).Delete
      Exit For
    End If
  Next
lbl_Exit:
  Set oCB = Nothing
  Exit Sub
End Sub

Final Note:

Advanced users already know or won't be surprised to discover that the AutoTextList displayed in the document is actually a CommandBar.  Yes, a CommandBar even in Word 2007 and higher.  If you don't like the unsightly "Create AutoText ..." at the trail of your AutoTextList, you can delete it by including the call to KillCat in the code above.

autotextlist_in_the_modern_age_6 autotextlist_in_the_modern_age_7

Site Note IconSee: Installing Macros for instructions on how to set up and use the macros provided in this Microsoft Word Help & Microsoft Word Tips page.

Site Note iconNote: This tips page, illustrations and examples were developed using Word 2010. It is wholly functional with Word 2003 trough 2016.

That's it! I hope you have found this tips page useful and informative.

Share

DISCLAIMER/TERMS OF USE

The information, illustrations and code contained in my "Microsoft Word Tips" are provided free and without risk or obligation.

Click to acces PayPal Verification Service Click to acces PayPal Verification Service

However, the work is mine. If you use it for commercial purposes or benefit from my efforts through income earned or time saved then a donation, however small, will help to ensure the continued availability of this resource.

If you would like to donate, please use the appropriate donate button to access PayPal. Thank you!

Click to donate British Pound Sterling                   Click to donate US dollars                   Click to donate EU euros

Search my site or the web using Google Search Engine

Google Search Logo