Microsoft KB Archive/211463

From BetaArchive Wiki
Knowledge Base


WD2000: How to Write a Macro That Creates a Table of Contents Entry Automatically

Article ID: 211463

Article Last Modified on 11/23/2006



APPLIES TO

  • Microsoft Word 2000 Standard Edition



This article was previously published under Q211463


SUMMARY

The following sample macro allows you to designate selected text as a table of contents entry without removing the text from the body of the document and without having to type the table of contents entry twice.

NOTE: Microsoft Word does provide a method to mark table of contents entries. To do this, select the text that you want to mark as a table of contents entry, press ALT+SHIFT+O. However, you can also use the macro described in this article.

MORE INFORMATION

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 visit the following Microsoft Web site:

For more information about the support options that are available and about how to contact Microsoft, visit the following Microsoft Web site:


For more information about how to use the sample code in this article, click the article number below to view the article in the Microsoft Knowledge Base:

212536 OFF2000: How to Run Sample Code from Knowledge Base Articles


You can assign the following macro to a menu, a toolbar, or a key sequence to quickly designate text to be included in a table of contents.

   Sub EnterTCField()
      Dim SelectedText As String
      ' Selection must be text.
      If Selection.Type = wdSelectionNormal Then
         ' Get selected text and place it within quotation marks.
         SelectedText = Chr$(34) & Selection.Text & Chr$(34)
         ' Move insertion point to end of selection.
         Selection.Collapse (wdCollapseEnd)
         ' Insert TC field.
         Selection.Fields.Add Range:=Selection.Range, _
         Type:=wdFieldTOCEntry, Text:=SelectedText
      Else
         MsgBox "The selected text is not valid for a TC field."
      End If
   End Sub
                

When you have designated all of the table of contents entries with this macro, use the following procedure to build the table of contents:

  1. On the Insert menu, click Index and Tables.
  2. Click the Table of Contents tab.
  3. Click Options.
  4. Click to select the Table entry fields check box.
  5. Click OK twice.

Table of contents entries or TC fields are automatically formatted as hidden text. To edit these entries, you must select Hidden text.

  1. On the Tools menu, click Options.
  2. On the View tab, under Show, click to select the Field codes check box
  3. Under Formatting marks, click to select the Hidden text check box. Click OK.


REFERENCES

For more information about Table of Contents Entry fields, click Microsoft Word Help on the Help menu, type Field codes: TC (Table of Contents Entry) field in the Office Assistant or the Answer Wizard, and then click Search to view the topic.

For more information about adding a command to a menu, click Microsoft Word Help on the Help menu, type Add a command or other item to a menu in the Office Assistant or the Answer Wizard, and then click Search to view the topic.

For additional information about creating custom toolbars and buttons, click the article number below to view the article in the Microsoft Knowledge Base:

191178 WD2000: How to Create Custom Toolbars, Toolbar Buttons


For additional informationabout getting help with Visual Basic for Applications, click the article numbers below to view the articles in the Microsoft Knowledge Base:

212623 WD2000: Macro Programming Resources


226118 OFF2000: Programming Resources for Visual Basic for Applications



Additional query words: vb vba vbe

Keywords: kbdtacode kbhowto KB211463