@xgrove,
Thanks for sharing. It's always nice when people give back a little bit from what the get in the forum.
@ xgrove and subPlanner,
Deleting everything on the TOC page can be a solution, but not if you have other "decorations" on it.
Better would be to mark the TOCEntries as such, then delete only them.
I had a little time to exercise and thought, I'd share as well:
Option Explicit
Sub TableOfContents()
' -----------------------------------------------------------------
' Creates stacked page references on first page of the drawing
' Double-click the "button" label to go to that page
'
' REMEMBER: Set the theme to NO THEME (first option) on Design Menu
' -----------------------------------------------------------------
Dim PageObj As Visio.Page ' sheet
Dim TOCEntry As Visio.Shape ' shape containing sheet name
Dim CellObj As Visio.Cell ' jump point
Dim PosY As Double ' starting point on the vertical axis
Dim pageCount As Double ' number of pages
Dim loopCount As Integer ' times going through For Each statement
Dim shp As Shape
Dim userRow As Integer
' ActiveDocument.Pages.Count will give the number of pages
' Notice: We're only interested in the number of foreground pages
For Each PageObj In ActiveDocument.Pages ' count the number of sheets
If PageObj.Background = False Then pageCount = pageCount + 1
Next
' Delete existing Entries
ActiveWindow.Page = ActiveDocument.Pages(1)
For Each shp In ActivePage.Shapes
If shp.CellExistsU("User.type", False) Then
If shp.Cells("user.type").ResultStr("") = "TOCEntry" Then
shp.Delete
End If
End If
Next shp
' Set variables
loopCount = 0
For Each PageObj In ActiveDocument.Pages
If loopCount > 0 Then ' skip the [Table of Contents] page
If PageObj.Background = False Then ' Only foreground pages
' PosY = startPoint - (pass x (height + gap)) <-- Chose the vertical starting point on the page
PosY = 7.75 - (PageObj.Index * 0.4)
Set TOCEntry = ActiveDocument.Pages(1).DrawRectangle(4.6, PosY, 4, PosY + 0.5)
' Write the page name in the rectangle
TOCEntry.Text = PageObj.Name
TOCEntry.Cells("VerticalAlign").Formula = "1" 'middle align
TOCEntry.Cells("Para.HorzAlign").Formula = visHorzLeft 'make text box left aligned
TOCEntry.Cells("Width").Formula = "6 in"
TOCEntry.Cells("Height").Formula = "0.36 in"
TOCEntry.Cells("Char.Size").Formula = "16 pt."
' Make the TOC Entry "recognizable"
userRow = TOCEntry.AddRow(visSectionUser, visRowLast, visTagDefault)
TOCEntry.Section(visSectionUser).Row(userRow).NameU = "Type"
TOCEntry.Cells("user.Type").FormulaU = Chr(34) & "TOCEntry" & Chr(34)
' Alternate the foreground color
If (loopCount Mod 2 = 0) Then
TOCEntry.Cells("FillForegnd").FormulaU = "RGB(195, 215, 232)"
Else
TOCEntry.Cells("FillForegnd").FormulaU = "RGB(224, 230, 205)"
End If
' Add a link to the sheets
Set CellObj = TOCEntry.CellsSRC(visSectionObject, visRowEvent, visEvtCellDblClick)
CellObj.Formula = "GOTOPAGE(""" + PageObj.Name + """)"
End If
End If
loopCount = loopCount + 1 ' increment the loop counter
Next
' Clean Up Environment
Set CellObj = Nothing
Set TOCEntry = Nothing
Set PageObj = Nothing
End Sub