vbapowerpointpowerpoint-2013

VBA to export images from PowerPoint with Section and Title as filename


I'm currently working on a solution for a group within our business that will allow them to create slides using PowerPoint 2013 from a PowerPoint presentation at HD resolution with specific filenames that will be used as digital signage through a different system that doesn't support PowerPoint files.

I've been looking for a solution using VBA to export the files as required, but haven't quite hit the mark. I'm not a VBA programmer myself, and have done my best to compile something that is close to my needs.

Exact requirements:

Here is the code I have so far:

Option Explicit
Const ImageBaseName As String = "Slide_"
Const ImageWidth As Long = 1920
Const ImageHeight As Long = 1080
Const ImageType As String = "PNG"

Function fileExists(s_directory As String, s_fileName As String) As Boolean

    Dim obj_fso As Object

    Set obj_fso = CreateObject("Scripting.FileSystemObject")
    fileExists = obj_fso.fileExists(s_directory & "\" & s_fileName)

End Function

Sub ExportSlides()

    Dim oSl As Slide
    Dim Path As String
    Dim File As String
    Dim i As Long

    If ActivePresentation.Path = "" Then
        MsgBox "Please save the presentation then try again"
        Exit Sub
    End If

    Application.FileDialog(msoFileDialogFolderPicker).ButtonName = "Select Path"

    Path = GetSetting("FPPT", "Export", "Default Path")

    With Application.FileDialog(msoFileDialogFolderPicker)
        .AllowMultiSelect = False
        .Title = "Select destination folder"
        If .Show = -1 And .SelectedItems.Count = 1 Then
            Path = .SelectedItems(1)
        Else: Exit Sub
        End If
    End With

    With ActivePresentation.SectionProperties
        For i = 1 To .Count
            For Each oSl In ActivePresentation.Slides
                If Not oSl.Shapes.HasTitle Then
                    File = .Name(i) & ImageBaseName & Format(oSl.SlideIndex, "0000") & "." & ImageType
                    Else: File = .Name(i) & oSl.Shapes.Title.TextFrame.TextRange.Text & Format(oSl.SlideIndex, "0000") & "." & ImageType
                End If
                If Not fileExists(Path, File) Then
                    oSl.Export Path & "\" & File, ImageType, ImageWidth, ImageHeight
                End If
            Next
        Next
    End With
End Sub

The code currently generates the files, but duplicates every slide with every section name, instead of just the slides within those sections.


Solution

  • One approach for sequential numbering:

    Dim dict As Object, sName As String
    Set dict = CreateObject("scripting.dictionary")
    
    
    With ActivePresentation.SectionProperties
        For i = 1 To .Count
            For Each oSl In ActivePresentation.Slides
    
                If Not oSl.Shapes.HasTitle Then
                    sName = .Name(i) & ImageBaseName
                Else
                    sName = .Name(i) & oSl.Shapes.Title.TextFrame.TextRange.Text
                End If
    
                dict(sName) = dict(sName) + 1
                File = sName & Format(dict(sName), "0000") & "." & ImageType
    
                If Not fileExists(Path, File) Then
                    oSl.Export Path & "\" & File, ImageType, ImageWidth, ImageHeight
                End If
            Next
        Next
    End With