<< StephenTurbek.com

Automatically Create Table of Contents in Visio

AddThis Social Bookmark Button

I use Visio to create wireframe documents, and as updating Tables of Contents in Visio is very dull, I wrote a macro (a small program) that automatically generates a Table of Contents list for a Visio document. It is stored in a Stencil.

The Table of Contents format is:
1 First Page
2 Another Page
3 Etc Page

(Where the page title comes from the page title seen in the tabs at the bottom of the page.)

How to use:


  • Set Security to Medium (to allow Visio to run Macros)




  • Close Visio
  • Open a Visio Document
  • Select a text box that you want to put the table of contents in
  • Open the Table_of_contents_creator_macro.vss stencil
  • Choose to enable macros
  • Select the text box you want the Table of Contents to appear in (or it will create a new box)
  • Select Tools > Macros > Table_of_contents_creator_macro > Module1 > Table_of_contents_creator
  • That’s it!


Here is the code itself (you would write this in the visual basic editor in Microsoft Visio)

Sub table_of_contents_creator()
'this macro creates a table of contents in a visio document by
'going through the pages in the document and adds the page number and page title

'by stephen turbek s@stephenturbek.com
'written for use in microsoft visio 2003 SP1

'adapted from http://www.greenonions.com/tocscript
'I added allowing user to select a text box and replace the contents, rather than build lots of little boxes
'this way you can style the text easily, and simply replace the contents when you update the doc
'note: this is my first VB script


' define a shape to use for the Table of Contents (TOC)
Dim TOCEntry As Visio.Shape


'get selection
Dim selectedShapes As Selection
Set selectedShapes = ActiveWindow.Selection



'is any shape selected to put the ToC in?
If selectedShapes.Count > 0 Then
'take the selected shape to put the table of contents in
Set TOCEntry = ActiveWindow.Selection.Item(1)
Else
'nothing is selected, create a shape
Set TOCEntry = ActiveDocument.Pages(1).DrawRectangle(1, 1, 7.5, 10)

TOCEntry.Cells("VerticalAlign").Formula = "0" 'make text box top vertically aligned
TOCEntry.Cells("Para.HorzAlign").Formula = visHorzLeft 'make text box left aligned


End If



'clear out the shape's text
TOCEntry.Text = ""

'a variable to hold the page array
Dim PageToIndex As Visio.Page


' loop through all the pages
For Each PageToIndex In Application.ActiveDocument.Pages

'exit when it hits the first background page (don't want those in the ToC)
If PageToIndex.Background Then Exit For

'append the page number, a tab, the page name, and a return to the ToC text shape
TOCEntry.Text = TOCEntry.Text + CStr(PageToIndex.Index) + vbTab + PageToIndex.Name + vbNewLine

Next


End Sub

Labels: , ,

Comments

Anonymous Cathy: This was exactly what I was looking for and easy to use.

THANKS!!  
Anonymous Beth: I second Cathy's comments. Thank you so much for sharing this.  
Anonymous Anonymous: It is nice macro and very helpful when you have a 50 page Visio doc  
Blogger Randy Moss: Thanks so much! This macro is awesome and is exactly what I was looking for to make my lengthy wireframe documents more accessible. Big thanks to you and Dan Brown for all the legwork!  
Anonymous Kathy: Thank you, this is a great tip for a large Visio. Thanks for sharing.  
Anonymous Anonymous: thank you so much for sharing this. it's just what i needed!  
Blogger Chris: Hey - very cool. Challenge to community: I would love to combine this with another macro that enables linkages from the TOC to the actual pages. Wonder if anyone is up for the challenge - link to code below.

Also, in this code I was able to change the code to allow the TOC to work successfully from Page 2 instead of Page 1, but when I deleted they physical structure on the page 1, it wouldn't work..help. Page.(1) to Page(2) more normal place for TOC and it worked for the existing example pages. But when I inserted new pages it was broken. Help!

I can't locate the original author, but the original code is at VBAexpress.com specifically:
http://www.vbaexpress.com/kb/getarticle.php?kb_id=505

Please keep me informed if anyone successfully combines these. Thanks!!!

Send me an email if so, netmaker2@yahoo.com, thanks  
Anonymous Anonymous: Thanks for this - it's a big help and (if you put the code directly into the VB editor) it works in Visio 2000 too!  
Blogger Christophe: Excellent, I was looking for something like that for a long time.
Really easy to use and safe lot of time.
Thank you  

Post a Comment