Microsoft KB Archive/97909

{|
 * width="100%"|

WD: Macro to Select AutoText Entry for Envelope Mailing Address

 * }

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 Partner or the Microsoft fee-based consulting line at (800) 936-5200. For more information about Microsoft Certified Partners, please see the following page on the World Wide Web:

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

"http://support.microsoft.com/directory/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 &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.

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, &quot;Microsoft Word&quot; Text 21, 8, 191, 13, &quot;Choose a Glossary Entry:&quot; 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, &quot;Microsoft Word&quot; Text 21, 8, 191, 13, &quot;Choose a Glossary Entry:&quot; 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 &quot;To this&quot; 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)