各スライドのタイトルを使用して目次を生成する機能です。
Power Addin VBA版に収録されています。
以下のリンク先からアドインをダウンロードしてお使いください。
機能
ボタンを押すと、今開いているプレゼンテーションの2スライド目に目次スライドが挿入されます。
目次は2スライド目以降のスライドタイトルを1項目として作成します。
レイアウトは元の2スライド目のものを流用します。
ソースコード
Sub TOC_From_Title()<br /> Dim ppt As Presentation Dim agLO As CustomLayout Dim agSL As Slide, hinagataSL As Slide Dim Shp As Shape Dim arrTitle() As String Dim i As Long, titleCont As Long, txtFlameCont As Long Set ppt = ActivePresentation For i = 0 To ppt.Slides.Count - 1 If i > 0 Then If ppt.Slides(i + 1).CustomLayout.Shapes.HasTitle = msoTrue And ppt.Slides(i + 1).CustomLayout.Shapes.Placeholders.Count > 1 Then Set hinagataSL = ppt.Slides(i + 1) Exit For End If End If Next For i = 0 To ppt.Slides.Count - 1 If i > 0 Then If ppt.Slides(i + 1).Shapes.HasTitle = msoTrue Then If Not Not arrTitle Then ReDim Preserve arrTitle(UBound(arrTitle) + 1) Else ReDim arrTitle(0) End If arrTitle(UBound(arrTitle)) = ppt.Slides(i + 1).Shapes.Title.TextFrame.TextRange.Text End If End If Next If hinagataSL Is Nothing Then Set agSL = ppt.Slides.Add(2, ppLayoutText) Else Set agLO = hinagataSL.CustomLayout Set agSL = ppt.Slides.AddSlide(2, agLO) End If If agSL.Shapes.HasTitle = msoFalse Then agSL.Shapes.AddTitle End If agSL.Shapes.Title.TextFrame.TextRange.Text = "Table of contents" If Not Not arrTitle Then For i = 0 To agSL.Shapes.Count - 1 If agSL.Shapes(i + 1).TextFrame.TextRange.Text <> "Table of contents" Then agSL.Shapes(i + 1).TextFrame.TextRange.Text = Join(arrTitle, vbCrLf) Exit For End If Next End If End Sub
コード解説
コメントを書かない悪癖があるので、完成したコードをChatGPTに投げて解説させました。
-
変数の宣言:
ppt
: PowerPointプレゼンテーションを表すオブジェクト。agLO
: カスタムレイアウトを表すオブジェクト。agSL
: 新しいスライドを表すオブジェクト。hinagataSL
: 目次用のスライドのテンプレート。Shp
: Shapeオブジェクト。arrTitle()
: タイトルを格納するための文字列配列。i
: ループ用のカウンター。titleCont
: タイトルの数。txtFlameCont
: テキストフレームの数。
-
プレゼンテーションオブジェクトの取得:
ActivePresentation
を使用して、アクティブなプレゼンテーションオブジェクトを取得。
-
目次用のテンプレートスライドの検索:
- プレゼンテーション内の各スライドをループし、目次用のテンプレートスライド(
hinagataSL
)を見つける。
- プレゼンテーション内の各スライドをループし、目次用のテンプレートスライド(
-
タイトルの収集:
- プレゼンテーション内の各スライドのタイトルを収集し、
arrTitle
配列に格納。
- プレゼンテーション内の各スライドのタイトルを収集し、
-
目次用スライドの作成:
hinagataSL
が見つからない場合は、新しいテキストレイアウトのスライドを作成。そうでない場合は、hinagataSL
のカスタムレイアウトを使用して新しいスライドを作成。
-
目次用スライドにタイトルを追加:
- 目次用スライドにタイトルがない場合、タイトルを追加し、”Table of contents”というテキストを設定。
-
目次用スライドにタイトル一覧を追加:
arrTitle
が空でない場合、目次用スライドにタイトル一覧を追加。