Microsoft KB Archive/86441

{|
 * width="100%"|

WD: Macros to Test for an Existing AutoText Entry Name

 * }

Q86441

-

The information in this article applies to:


 * Microsoft Word for Windows, versions 1.0, 1.1, 1.1a, 2.0, 2.0a, 2.0a-CD, 2.0b, 2.0c, 6.0, 6.0a, 6.0c
 * Microsoft Word for Windows NT, version 6.0
 * Microsoft Word for Windows, versions 7.0, 7.0a
 * Microsoft Word for the Macintosh, versions 6.0, 6.0.1

-

SUMMARY
When you write macros in Microsoft Word for Windows, it may be necessary to test for the existence of an AutoText (&quot;glossary&quot; prior to Word 6.0) entry before inserting the contents of the entry. If your macro does not check for the existence of an AutoText entry before attempting to insert the contents into the document, you may receive one of the following error messages:

Word 6.x, 7.x
The name you typed is not a valid AuotText entry. Use the AutoText button on the Standard toolbar to define AutoText entries that can be inserted as a long piece of text or a graphic.

Word 1.x, 2.x
This glossary entry does not exist.

MORE INFORMATION
The following WordBasic macros test for an existing AutoText (glossary) entry.

Word 6.x, 7.x
WARNING: ANY USE BY YOU OF THE CODE PROVIDED IN THIS ARTICLE IS AT YOUR OWN RISK. Microsoft provides this macro code &quot;as is&quot; without warranty of any kind, either express or implied, including but not limited to the implied warranties of merchantability and/or fitness for a particular purpose.

If the &quot;test&quot; AutoText entry resides in the Normal template, the following macro inserts the contents of the AutoText entry at the insertion position in the document. If the &quot;test&quot; entry does not exist in the Normal template, this macro example posts a message indicating that it does not exist.

  Sub Main 'Assign AutoText name to string AutoTextName$ = &quot;test&quot; 'AutoText exists? If GetAutoText$(AutoTextName$) = &quot;&quot; Then MsgBox &quot;AutoText Name, &quot; + AutoTextName$ + &quot;, Does Not Exist&quot; Else EditAutoText .Name = AutoTextName$ End If  End Sub

Word 2.x
If the &quot;test&quot; Glossary entry resides in the Normal template, the following macro inserts the contents of the Glossary entry at the insertion position in the document. If the &quot;test&quot; entry does not exist in the Normal template, this macro example posts a message indicating that it does not exist.

  Sub Main 'Assign glossary name to string GlosName$ = &quot;test&quot; 'Glossary exists? If GetGlossary$(GlosName$) = &quot;&quot; Then MsgBox &quot;Glossary Name &quot; + GlosName$ + &quot; Does Not Exist &quot; Else EditGlossary .Name = GlosName$ End If  End Sub The following WordBasic macro tests for an existing glossary entry named &quot;test&quot; in the active template (template other than Normal):

  Sub Main GlosName$ = &quot;test&quot; If GetGlossary$(GlosName$, 1) = &quot;&quot; Then MsgBox &quot;Glossary Name &quot; + GlosName$ + &quot; Does Not Exist &quot; Else EditGlossary .Name = GlosName$, .Context = 1 End If  End Sub The context of the GetGlossary and EditGlossary commands above is set to template instead of global.

These commands operate the same if the word Glossary is replaced by the word AutoText when Word 6.0 is used.