Finally! Creating a Table of Contents in Visio (how did I not find this until now?!)
Call me crazy, but I feel as though I’ve just uncovered the holy grail of Visio.
I don’t know how many people I’ve asked about ways that you can create a Table of Contents in Visio. It seems like it should be sooooo easy, but no one has ever been able to help me out.
So I had a bit of ‘R&D time’ this afternoon and was playing with the Annotation Hiding Macro that Donna posted yesterday, then poked around a few of her other Visio pages, and there, in amongst some comments, I found it! A Macro that creates a Table of Contents in Visio for you.
Hoorah! Celebrations all round!
(Or did you all already know how to do this and I’ve been living under a Visio rock?!)
Now… it’s not all that pretty. But, for now, it does the trick.
Without any further ado, here it is (with many thanks to Bram!):
//
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 CellOjb As Visio.Cell
Dim PosY As Double
Dim PageCnt As Double
Dim PageNr 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
PageNr = PageNr + 1
‘ 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, 5, PosY + 0.25)
‘ write the page name in the rectangle
TOCEntry.Text = PageObj.Name & vbTab & PageNr
‘ 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
End Sub
//
Isn’t it beautiful!
Is it just me, or are those interaction designers in The Netherlands particularly clever with Visio?



I like Dan’s TOC better. It is here: http://www.greenonions.com/archives/2005/10/20/stupid-visio-tricks-128-tables-of-contents/