WD: Macros to Test for an Existing AutoText Entry Name

ID: Q86441

The information in this article applies to:

SUMMARY

When you write macros in Microsoft Word for Windows, it may be necessary to test for the existence of an AutoText ("glossary" 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 "as is" 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 "test" 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 "test" 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$ = "test"
      'AutoText exists?
      If GetAutoText$(AutoTextName$) = "" Then
         MsgBox "AutoText Name, " + AutoTextName$ + ", Does Not Exist"
      Else
         EditAutoText .Name = AutoTextName$
      End If
   End Sub

Word 2.x

If the "test" 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 "test" 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$ = "test"
      'Glossary exists?
      If GetGlossary$(GlosName$) = "" Then
         MsgBox "Glossary Name " + GlosName$ + " Does Not Exist "
      Else
         EditGlossary .Name = GlosName$
      End If
   End Sub

The following WordBasic macro tests for an existing glossary entry named "test" in the active template (template other than Normal):

   Sub Main
      GlosName$ = "test"
      If GetGlossary$(GlosName$, 1) = "" Then
         MsgBox "Glossary Name " + GlosName$ + " Does Not Exist "
      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.

REFERENCES

"Using WordBasic," by WexTech Systems and Microsoft, pages 183 and 228

Additional query words: winword2 winword word95 word6 GetGlossary Insert Glossary AutoText GetAutoText macword word7 winword

Keywords          : kbmacro kbmacroexample 
Version           : WINDOWS:1.0,1.1,1.1a,2.0,2.0a,2.0a- CD,2.0b,2.0c,6.0,6.0a,6.0c,7.0,7.0a; MACINTOSH:6.0,6.0.1,6.0.1a
Platform          : MACINTOSH Win95 WINDOWS winnt
Issue type        : kbhowto

Last Reviewed: February 3, 1998