Microsoft has awarded Lene Fredborg, DocTools, the Microsoft MVP award for Word each year since 2008. Link to info about the MVP Award at Microsoft.com DocTools - document automation - add-ins, macros and document templates for Microsoft Word (logo) Document Automation
Add-ins & Macros
Document Templates
For Microsoft Word
Document automation, templates, add-ins, macros, for Microsoft Word (graphic)



Bookmark this page | Tip a friend about DocTools


Search thedoctools.com:

Custom Search



Macro ready for use...

Macro – Create or Edit AutoText via VBA

You may elsewhere find information 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. Please note that the methods described are only well suited for AutoTexts that consist of plain text only and that do not exceed 255 characters.

Terms of use

You are welcome to use the macros and Word tips provided on this website. The contents is copyright. You may not reproduce any of contents (with or without modification or credits) elsewhere on the web, or on CD. Please link to the contents on this website instead.

Please note that all free Word tips, macros, add-ins and demos are provided "as is" and without obligation. See also detailed Terms of use.


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

NOTE: 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 links below.

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

IMPORTANT: 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 links 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 links below include both macros described above.


Related information

See general information about VBA macros and code snippets, incl. information about how to install macros.



Top of page

Back to Macros...




Copyright © 2006-2017 Lene Fredborg – DocTools ApS – all rights reserved | Microsoft Word Templates – add-ins – VBA macros | Save time and work more efficiently in Microsoft Word
Privacy policy | Terms of use | Cookies policy | Bookmark this page | Tip a friend | Links | Contact