Solved

Generate Table of Contents for Visio 2010

Posted on 2011-02-24
2
3,171 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
Comment Utility
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
Comment Utility
Brillinat Scott, absolutely spot on. Just made a few adjustments to delete existing TOC if it exists and it works perfectly.
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

A recent Visio zone question asked how to use the data in an Excel file to create a Visio Gantt chart -- and then to dynamically update the Gantt chart. The good news is that the Viso Gantt Chart wizard can be automated. The bad news is that the…
David Parker’s latest book, Microsoft Visio 2010: Business Process Diagramming and Validation, will give you the tools to turn flowcharts and other business diagrams into valuable, data-driven corporate assets. Armed with the knowledge you’ll gain f…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

743 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

17 Experts available now in Live!

Get 1:1 Help Now