WD: Macro to Select AutoText Entry for Envelope Mailing Address

ID: Q97909


The information in this article applies to:
  • Microsoft Word for Windows, versions 2.0, 2.0a, 2.0a-CD, 2.0b, 2.0c, 6.0, 6.0a, 6.0c
  • Microsoft Word for Windows 95, versions 7.0, 7.0a
  • Microsoft Word 97 for Windows
  • Microsoft Word 98 Macintosh Edition
  • Microsoft Word for the Macintosh, versions 6.0, 6.0.1


SUMMARY

The following Microsoft Visual Basic for Applications and WordBasic macro examples allow you to select an AutoText (or glossary) entry for an envelope mailing address.

The macro displays the names of all of the available AutoText entries, allowing you to select one for your envelope mailing address, and it then inserts the contents of that entry into the Create Envelope dialog box.

Prior to running the following macro, create AutoText (or glossary) entries for the mailing addresses that you use most often.

Visual Basic for Applications

Microsoft provides programming examples for illustration only, without warranty either expressed or implied, including, but not limited to, the implied warranties of merchantability and/or fitness for a particular purpose. This article assumes that you are familiar with the programming language being demonstrated and the tools used to create and debug procedures. Microsoft support professionals can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific needs. If you have limited programming experience, you may want to contact a Microsoft Certified Solution Provider or the Microsoft fee-based consulting line at (800) 936-5200. For more information about Microsoft Certified Solution Providers, please see the following page on the World Wide Web:

http://www.microsoft.com/mcsp/
For more information about the support options available from Microsoft, please see the following page on the World Wide Web:

http://www.microsoft.com/support/supportnet/overview/overview.asp

   Sub GetAutoTextForEnvelope()
      Dim strATName As String
      Dim strATValue As String
      ' Display AutoText list.
      With Dialogs(wdDialogEditAutoText)
         .Display
         strATName = .Name
      End With
      ' Extract AutoText Value from selected entry.
      strATValue = ActiveDocument.AttachedTemplate. _
      AutoTextEntries(strATName).Value
      ' Show Envelope dialog.
      With Dialogs(wdDialogToolsEnvelopesAndLabels)
         ' Set value for delivery address to selected AutoText
         ' entry's value.
         .AddrText = strATValue
         .Show
      End With
   End Sub 

WordBasic

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.

Word 6.x, Word 7.x:

   Sub MAIN
      num = CountAutoTextEntries(0)
      Dim gloss$(num - 1)
      For x = 1 To num
         gloss$(x - 1) = AutoTextName$(x, 0)
      Next x
      Begin Dialog UserDialog 320, 144, "Microsoft Word"
         Text 21, 8, 191, 13, "Choose a Glossary Entry:"
         ListBox 16, 39, 160, 84, gloss$(), .ListBox1
         OKButton 199, 45, 88, 21
         CancelButton 198, 74, 88, 21
      End Dialog
      Dim dlg1 As UserDialog
      n = Dialog(dlg1)
      choice = dlg1.listbox1
      glosstext$ = GetAutoText$(gloss$(choice), 0)
      Dim dlg As ToolsCreateEnvelope
      GetCurValues dlg
      dlg.EnvAddress = glosstext$
      n = Dialog(dlg)
      If n <> 0 Then ToolsCreateEnvelope dlg
   End Sub 
Word 2.x:

   Sub MAIN
      num = CountGlossaries(0)
      Dim gloss$(num - 1)
      For x = 1 To num
         gloss$(x - 1) = GlossaryName$(x, 0)
      Next x
      Begin Dialog UserDialog 320, 144, "Microsoft Word"
         Text 21, 8, 191, 13, "Choose a Glossary Entry:"
         ListBox 16, 39, 160, 84, gloss$(), .ListBox1
         OKButton 199, 45, 88, 21
         CancelButton 198, 74, 88, 21
      End Dialog
      Dim dlg1 As UserDialog
      n = Dialog(dlg1)
      choice = dlg1.listbox1
      glosstext$ = GetGlossary$(gloss$(choice), 0)
      Dim dlg As ToolsCreateEnvelope
      GetCurValues dlg
      dlg.EnvAddress = glosstext$
      n = Dialog(dlg)
      If n <> 0 Then ToolsCreateEnvelope dlg
   End Sub 
To display and choose from AutoText (glossary) entries in the current template rather than global entries, change the context parameter for the following commands from 0 (zero) to 1:
CountAutoTextEntries (CountGlossaries)
AutoTextName$ (GlossaryName$)
GetAutoText$ (GetGlossary$)
The correct command to use is listed in the "To this" column of the following table.

   Version
   of Word   Change this                   To this
   ---------------------------------------------------------------------
   6.x, 7.x  num = CountAutoTextEntries(0) num = CountAutoTextEntries(1)

   2.x       num = CountGlossaries(0)      num = CountGlossaries(1) 


REFERENCES

"Using WordBasic," by WexTech Systems and Microsoft, pages 168, 228, 229, and 306.

Additional query words: choice addressee word95 winword winword2 cd envelope return address AutoText macword word7 word6 glossary glossaries word8 word97 macword98

Keywords : kbmacro kbualink97
Version : MACINTOSH:6.0,6.0.1,98; WINDOWS:2.0,2.0a,2.0a-CD,2.0b,2.0c,6.0,6.0a,6.0c,7.0,7.0a,97
Platform : MACINTOSH WINDOWS
Issue type : kbhowto kbinfo


Last Reviewed: November 13, 1999
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.