asked on
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