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.
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.
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...