Add Table of Contents

Ease of Use


Version tested with


Submitted by:



Creates a table of contents for a web page based upon the h1 and h2 tags in the html code. 


Automatically inserts a Table of Contents at the top of a lengthy web page to take users to the various sections on the page by hyperlink. 


instructions for use


Option Explicit Dim FpVM As FpPageViewMode Dim objBody As FPHTMLBody Dim objUL As FPHTMLUListElement Dim objHdEl As FPHTMLHeaderElement Dim strUL As String Dim strTemp As String Dim i As Integer Const DQ As String = """" Sub TOC() ' remember the viewmode FpVM = ActivePageWindow.ViewMode ' go to normal view ActivePageWindow.ViewMode = fpPageViewNormal ' save reference to body element Set objBody = ActiveDocument.body ' save reference to ul element where id = "TOC" (if it exists) For Each objUL In objBody.all.tags("ul") If objUL.Id = "TOC" Then Exit For Else Set objUL = Nothing End If Next objUL ' start building new TOC strUL = "<ul id=" & DQ & "TOC" & DQ & ">" ' loop through all "h2" tags For Each objHdEl In objBody.all.tags("h2") ' counter to give a unique name to each "a" tag i = i + 1 ' if objHdEl already contains an "a" tag, delete it If Not objHdEl.all.tags("a").Item(0) Is Nothing Then objHdEl.all.tags("a").Item(0).outerHTML = "" End If ' save the innerHTML of the "h2" tag strTemp = objHdEl.innerHTML ' change the innerHTML of the "h2" tag objHdEl.innerHTML = "<a name=" & DQ & "id" & i & DQ & "></a>" & strTemp ' append new list item to the TOC strUL = strUL & vbCrLf & " <li><a href=" & DQ & "#id" & i strUL = strUL & DQ & ">" & strTemp strUL = strUL & "</a></li>" Next objHdEl ' closing the "ul" element strUL = strUL & vbCrLf & "</ul>" & vbCrLf If objUL Is Nothing Then ' add TOC (after first "h1" tag) On Error Resume Next 'continue if no tags are present. objBody.all.tags("h1").Item(0).insertAdjacentHTML "afterEnd", vbCrLf & strUL Else ' update TOC (wherever it is located) objUL.outerHTML = strUL End If ' return to previous viewmode ActivePageWindow.ViewMode = FpVM End Sub

How to use:

  1. With FrontPage running, open the Visual Basic Editor (VBE) by pressing Alt+F11.
  2. Add a module under Insert>Module from the VBE menu bar.
  3. Copy the code from above and paste it into the module.
  4. Run the macro from the FrontPage tools menu: Tools>Macro>Macros (Alt+F8).
  5. Select TOC from the list of macros.
  6. Press Run.
  7. Save the web.

Test the code:

  1. To test the code, run it from the tools menu in FrontPage.
  2. Tools>Macro>Macros (Alt+F8).
  3. Select TOC from the list of macros.
  4. Press Run.
  5. Save the web.
  6. If no header tags (h1 or h2) exist, the code will do nothing.
  7. The example file contains identical pages with and without tags for testing the code. Running it from the page with tags results in a TOC being created; running from the page without tags results in nothing being changed.

Sample File:

Frontpage 1.78KB 

Approved by mdmackillop

This entry has been viewed 85 times.

Please read our Legal Information and Privacy Policy
Copyright @2004 - 2020 VBA Express