Create or Edit AutoText via VBA

You may find information elsewhere telling that you can only create an AutoText via VBA if you first insert the desired content in a document. Also, you may find information telling that you cannot change the content of an existing AutoText via VBA without first inserting the AutoText, then editing the content and replacing the old AutoText with a new version.

However, you can actually create an AutoText via VBA and set the value to whatever string you want without first inserting the string in a document. Correspondingly, you can change the content of an existing AutoText directly via VBA without first inserting the old version in a document. The macros available via the link at the end of this article will help you do precisely that.

Note however, that there are some limitations as regards the formatting and length of the AutoTexts that are created or edited via VBA. The limitations are emphasized below.

Macro solution

How to create an AutoText without first inserting the content in a document

IMPORTANT LIMITATIONS

The method used in the macro you can download below results in AutoTexts entirely in Normal style. However, if the AutoText you create does not include any paragraph marks, it will be formatted with the style of the paragraph in which you insert the AutoText (e.g. a heading or a bulleted paragraph).

The Value property of an AutoText can contain max. 255 characters. Therefore, you cannot create AutoTexts longer than 255 using the method described here.

The idea is to first create the AutoText containing whatever is selected in the document (or you could define another range of your wish). Then you can use the Value property of the AutoText to replace the content with the desired string.

An example of such macro can be viewed or downloaded via the link below.

How to edit the content of existing AutoTexts without first inserting the AutoTexts in a document

IMPORTANT LIMITATIONS

The method used in the macro you can download results in AutoTexts in plain text. AutoTexts that include paragraph marks will be in Normal style.

Do not use the method to edit AutoTexts with rich text formatting since the formatting will be lost.

The Value property of an AutoText can contain max. 255 characters. Therefore, you can only edit up to the first 255 characters of longer AutoTexts using the method described here. See also the additional info about the Value property below.

Corresponding to the procedure described above, you can change the contents of any existing AutoText – or you can replace a certain string in any AutoText.

Example: you want to replace the string "abc" in all AutoText entries in a specific template with "12345".

An example of such macro can be viewed or downloaded via the link below.

More about the Value property of AutoText entries

Fields and other types of special content are stored in the Value property using a special syntax. For example a DocProperty field that refers to a custom document property named "Name" and with the value "John Doe" will appear as follows in the Value property:

* DocProperty "Name" *John Doe*

This way, the Value property may contain fewer than 255 characters of the AutoText content as it appears for the user since additional data in the Value property may take up some of the 255 characters as illustrated in this example.

Note that the link below includes both macros described above.

The macros

Below, you will find the macro code. If you need help on installing macros, see How to install a macro.

Option Explicit

Sub CreateAutoTextWithoutInsertingContentInDocument()

    '=========================
    'Macro created 2008 by Lene Fredborg, DocTools - www.thedoctools.com
    'THIS MACRO IS COPYRIGHT. YOU ARE WELCOME TO USE THE MACRO BUT YOU MUST KEEP THE LINE ABOVE.
    'YOU ARE NOT ALLOWED TO PUBLISH THE MACRO AS YOUR OWN, IN WHOLE OR IN PART.    
    '=========================
    'The macro illustrates how you can create an AutoText
    'without first inserting the contents in a document
    'You may need to include error handling
    'NOTE: YOU CAN ONLY CREATE AUTOTEXT WITH PLAIN TEXT UP TO 255 CHARACTERS USING THIS METHOD
    '=========================
    
    Dim oAutoText As AutoTextEntry
  
   'Create an AutoText with the current selection as the content
    'You could instead use any range from the document
    Set oAutoText = Templates(ActiveDocument.AttachedTemplate).AutoTextEntries _
        .Add(Name:="MyName", Range:=Selection.Range)

    'Now replace the content of the AutoText with the desired value
    oAutoText.Value = "ThisIsMyNewValue"
        
    'Clean up
    Set oAutoText = Nothing

End Sub

Sub EditExistingAutoTextsWithoutInsertingContentInDocument()
    
    '=========================
    'Macro created 2008 by Lene Fredborg, DocTools - www.thedoctools.com
    'THIS MACRO IS COPYRIGHT. YOU ARE WELCOME TO USE THE MACRO BUT YOU MUST KEEP THE LINE ABOVE.
    'YOU ARE NOT ALLOWED TO PUBLISH THE MACRO AS YOUR OWN, IN WHOLE OR IN PART.    
    '=========================
    'The macro illustrates how you can replace a string in existing AutoTexts
    'without first inserting the AutoTexts
    'You may need to include error handling
    'IMPORTANT: DO NOT USE FOR AUTOTEXTS WITH RICH TEXT FORMATTING - THE FORMATTING WILL BE LOST
    'YOU CAN ONLY EDIT THE FIRST 255 CHARACTERS OF AN AUTOTEXT VALUE USING THIS METHOD
    '=========================
    
    Dim oAutoText As AutoTextEntry
    Dim oTemplate As Template
    Dim strOld As String
    Dim strNew As String
    
    'Define the string to be replaced - change to the desired value
    strOld = "abc"
    'Define the replacement string - change to the desired value
    strNew = "12345"
    
    'Make sure to identify the correct template - here the attached template
    Set oTemplate = ActiveDocument.AttachedTemplate
    
    'Iterate through all AutoTexts in oTemplate and replace strings
    
    For Each oAutoText In oTemplate.AutoTextEntries
        oAutoText.Value = Replace(oAutoText.Value, strOld, strNew)
    Next oAutoText

    'Clean up
    Set oTemplate = Nothing

End Sub

Related information

See About VBA Macros and Code Snippets and How to Install a Macro for misc. information that may help you in your work with macros and for information about how to install macros.

Free Trial icon
Word Add-In from DocTools
Did you know that...

DocTools Word Add-Ins
can help you save time in Word

On my website wordaddins.com you will find some of the Word Add-Ins I have developed, ready for use:

Generate complete documents in seconds from re-usable text or graphics - read more...

helps you manage comments in Word fast and easy – review comments, extract comments, etc. - read more...

Makes it easier than ever to work with cross-references in Word documents - read more...

Lets you manage document data efficiently with custom document properties and DocProperty fields - read more...

Lets you extract insertions, deletions and comments in full context and including headings - read more..

Lets you apply or remove any highlight color by the click of a button - read more...

Browse pages, headings, tables, graphic, etc. and find text in Word with a single click - read more...

Browse pages, headings, tables, graphic, etc. and find text in Word with a single click - read more...

Lets you quickly and easily create screen tips in Word with up to 2040 characters - read more...