Link to home
Create AccountLog in
Avatar of Luis Diaz
Luis DiazFlag for Colombia

asked on

VBA PowerPoint: review macro to create table of content as of slide 2 and create related table based on color of the presentation

Hello experts,

I have the follwing VBA PowerPoint macro:

I was wondering if there is a way to:

1-Insert table of content as of slide number 2 after the cover page

2-Adapt the color table based on the color used by the user at the design/View=>slide master.

'***************************************************************************
'#
' Purpose: create a table of contents slide.
' Method:  Create a new slide and add a table of contents to it based on the
'          text in title placeholders across all slides in the active presentation
'          and add hyperlinks to the slide numbers within the table.
'EE question: 29178609
' Validation date 2020/04/27
'***************************************************************************
Sub Create_Table_Of_Content()
  Dim oTOC As Slide
  Dim oSld As Slide
  Dim tTitle As String
  Dim oTable As Table
  Dim oTR As TextRange
  
  On Error Resume Next
  
  ' Create a new slide based on the layout of your choice
  With ActivePresentation
    ' Change the custom layout index to the one you want to use for the TOC slide
    Set oTOC = .Slides.AddSlide(1, .SlideMaster.CustomLayouts(2))
  End With
  
  ' Add new table to the new slide
  Set oTable = oTOC.Shapes.AddTable(1, 2).Table
  
  ' Create column headers
  With oTable
    .Cell(1, 1).Shape.TextFrame.TextRange.Text = "Slide Title"
    .Cell(1, 2).Shape.TextFrame.TextRange.Text = "Slide Number"
  End With
  
  ' Add slide titles and linked slide numbers to the table
  For Each oSld In ActivePresentation.Slides
    If oSld.Shapes.HasTitle And oSld.SlideIndex > 1 Then
      With oSld.Shapes.Title.TextFrame.TextRange
        tTitle = IIf(.Text = "", "No title", .Text)
        With oTable
          .Rows.Add
          .Cell(.Rows.Count, 1).Shape.TextFrame.TextRange.Text = tTitle
          With .Cell(.Rows.Count, 2).Shape.TextFrame
            Set oTR = .TextRange.InsertAfter(oSld.SlideIndex)
            With oTR.ActionSettings.Item(ppMouseClick)
              .Hyperlink.SubAddress = oSld.SlideID & "," & oSld.SlideIndex & "," & tTitle
            End With
          End With
        End With
      End With
    End If
  Next
  
  oTOC.Select
  
End Sub

Open in new window


SOLUTION
Avatar of Jamie Garroch (MVP)
Jamie Garroch (MVP)
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Avatar of Luis Diaz

ASKER

Noted. I have a lot of things to do today as I am a PM. Would you mind to send me the revised version to be use with the table contents in second slides. The table color we drop.
Hello Jamie, sorry for my last comment, I apoligise. I was in a hurry but now I have time.
If I want to modify
 Set oTOC = .Slides.AddSlide(1, .SlideMaster.CustomLayouts(2))

Open in new window

In order to add an input box with the following question:
In which slide do you want to add your Table of content
"We recomend to enter 2 in order to have Table of content after the Cover page".
Make choice Number limited from 1 to 3.
Concerning the table of content format we can keep like that.
SOLUTION
Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
Hello Jamie,

Thank you for your comment. I realised that it doesn't make sense to propose multiple choice.
The best is just to put in slide 2. I modify accordingly.
Set oTOC = .Slides.AddSlide(2, .SlideMaster.CustomLayouts(2))

Open in new window

However I don't know how to add as title in slide Number 2: "Table of content" and kype the same format (color and size police related to the slide master) and how to avoid in the table of content the cover page. I don't need "No title" in the table of content.

ASKER CERTIFIED SOLUTION
Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
Noted. Thank you very much for your help.