Solved

Generate Table of Contents for Visio 2010

Posted on 2011-02-24
2
3,262 Views
Last Modified: 2012-05-11
I've searched online and in EE but haven't been able to find any solutions for this. I have a Visio document which contains around 100 process maps. These are regularly reviewed by our Business Analysis team as we are in the middle of a big project to implement a new system. Each process is on a different page in Visio and it is difficult to keep track of which workflows are in the document and also to find workflows quickly (other than using Control + F and searching all pages, which isn't great as many workflows may contain the same text even though they have different titles).
I could set up a contents page by adding a shape for each page and manually assigning a hyperlink, but this is clearly time consuming and will require the contents to be rearranged each time a process is inserted.
Is there a way of doing this using VBA?
0
Comment
Question by:Karl_mark
2 Comments
 
LVL 30

Accepted Solution

by:
Scott Helmers earned 500 total points
ID: 34975584
So this is quick and dirty but it gets the job done...

It extracts all page names, sorts them, deposits them on a new page called Table of Contents  and then creates hyperlinks to each page.

It doesn't have any niceties like checking whether Table of Contents already exists (it will generate an error) and it will run page names right off the bottom of the page.

If you're familiar with VBA, the last problem is relatively easy to solve. The first requires a bit more knowledge about programming Visio.

To use the code, open the VB Editor (Alt+F11) and paste the code into the ThisDocument module.
Sub GenerateTOC()

    Dim arrPages() As String
    
    Dim pg As Visio.Page
    Dim shp As Visio.Shape
    Dim iPageCount As Integer
    Dim dX As Double, dY As Double
    Dim dDeltaY As Double
    Dim HL As Visio.Hyperlink
    
    iPageCount = 0
    ReDim arrPages(250)
    For Each pg In ActiveDocument.Pages
        If Not pg.Background Then
            iPageCount = iPageCount + 1
            arrPages(iPageCount) = pg.Name
        End If
    Next
    
    Call SortAscend_x1(arrPages, 1, iPageCount)

    Set pg = ActiveDocument.Pages.Add
    pg.Name = "Table of Contents"
    'make this the first page
    pg.Index = 1
    ActiveWindow.Page = pg.Name

    dX = 1
    ' set vertical location for first TOC entry
    dY = pg.PageSheet.Cells("PageHeight").Result(visInches) - 0.25
    dDeltaY = 0.15
    
    For i = 1 To iPageCount
        ' draw rectangle
        dY = dY - dDeltaY
        Set shp = pg.DrawRectangle(dX, dY, dX + 3, dY + dDeltaY * 0.9)
        shp.Text = arrPages(i)
        ' set font size, text alignment, border, fill and shadow
        shp.CellsSRC(visSectionCharacter, 0, visCharacterSize).FormulaU = "8 pt"
        shp.Cells("Para.HorzAlign") = 0
        shp.Cells("LinePattern").Formula = 0
        shp.Cells("FillPattern").Formula = 0
        shp.Cells("ShdwPattern").Formula = 0
        Set HL = shp.Hyperlinks.Add
        HL.SubAddress = arrPages(i)
    Next i

    ActiveWindow.DeselectAll

End Sub
Private Sub SortAscend_x1(ByRef arr, SortStart, SortEnd)
' SortKey identifies column in array by which to sort
' SortStart and SortEnd allow flexibility to sort only selected rows within the array

    Dim i, j As Integer
    Dim Temp1
    
    If SortEnd - SortStart <= 0 Then Exit Sub

    ' bubble sort
    For i = SortEnd - 1 To SortStart Step -1
        For j = SortStart To i
            If arr(j) > arr(j + 1) Then ' Compare neighboring elements
               Temp1 = arr(j)
               arr(j) = arr(j + 1)
               arr(j + 1) = Temp1
            End If
        Next j
    Next i

End Sub

Open in new window

0
 

Author Closing Comment

by:Karl_mark
ID: 34978732
Brillinat Scott, absolutely spot on. Just made a few adjustments to delete existing TOC if it exists and it works perfectly.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Topology_issues 3 50
Why the page is too small in Visio 2013 ? 7 244
Export Visio Labels to Excel 4 104
How to Upload data on VPS ? 4 73
As the Marketing Director for the company I work for, time is a commodity that I have relatively little of.  My time is divided between coaching my team, creating a direction for our company's future AND actually getting stuff done.  As a result, I …
The ability to add structure to Visio diagrams using containers, lists and callouts is one of my favorite features in Visio 2010. In this article I’ll describe a mashup of containers and lists that meet a real-world need. Prior to reading this ar…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now