Visio

Create table of contents

Ease of Use

Easy

Version tested with

2003 

Submitted by:

Brandtrock

Description:

This routine creates a table of contents on the first page. Each shape can be double clicked to take you to the page it displays. 

Discussion:

Easily navigate to different parts of a Visio document by adding a shape based table of contents to the front page. 

Code:

instructions for use

			

Option Explicit Sub TableOfContents() ' creates a shape for each page in the drawing on the first page of the drawing ' then add a dbl-clk GoTo to each shape so you can double click and go to that Page Dim PageObj As Visio.Page Dim TOCEntry As Visio.Shape Dim CellObj As Visio.Cell Dim PosY As Double Dim PageCnt As Double ' ActiveDocument.Pages.Count will give the number of pages, but we are interested ' the number of foreground pages PageCnt = 0 For Each PageObj In ActiveDocument.Pages If PageObj.Background = False Then PageCnt = PageCnt + 1 Next ' loop through all the pages For Each PageObj In ActiveDocument.Pages If PageObj.Background = False Then ' Only foreground pages ' where to put the entry on the page? PosY = (PageCnt - PageObj.Index) / 4 + 1 ' draw a rectangle for each page to hold the text Set TOCEntry = ActiveDocument.Pages(1).DrawRectangle(1, PosY, 4, PosY + 0.25) ' write the page name in the rectangle TOCEntry.Text = PageObj.Name ' add a link to point to the page to you can just go there with a Double Click Set CellObj = TOCEntry.CellsSRC(visSectionObject, visRowEvent, visEvtCellDblClick) 'Start CellObj.Formula = "GOTOPAGE(""" + PageObj.Name + """)" End If Next 'Clean Up Set CellObj = Nothing Set TOCEntry = Nothing Set PageObj = Nothing End Sub

How to use:

  1. Open your Visio document.
  2. Press Alt + F11 to launch the Visual Basic Editor (VBE).
  3. Select Insert>Module from the Menu Bar in the VBE.
  4. Copy the code from above.
  5. Paste the code in the right hand window of the VBE.
  6. Close the VBE.
  7. From the Visio document, select Tools>Macro>Macros (or press Alt + F8).
  8. Highlight TableOfContents in the list of macros.
  9. Press run.
 

Test the code:

  1. Open the example file.
  2. Insert a page in the example file.
  3. Run the TableOfContents macro per the instructions above.
  4. Notice that the page you added is now listed in the Table of Contents.
  5. When you are convinced that it works right, place the code in your document and run it.
 

Sample File:

VBAX_Visio1.zip 174.01KB 

Approved by mdmackillop


This entry has been viewed 134 times.

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