I'm using VBA in powerpoint : VBA code for a PowerPoint macro that renames each slide with the text contained in the rectangle shape on the slide and exports all slides in image format to a user-selected folder
here is what I tried. I manage to select the folder for the image export but then I have an error on the line :
strName = strPath & "\Slide" & i & "_" & sld.Shapes.Title.TextFrame.TextRange.Text & ".jpg"
Sub ExportSlidesAsImages()
'Déclaration des variables
Dim i As Integer ' Variable pour stocker l'index de la diapositive en cours de traitement.
Dim sld As slide ' Variable pour stocker la diapositive en cours de traitement.
Dim strPath As String ' Variable pour stocker le chemin d'accès au dossier sélectionné par l'utilisateur
Dim strName As String ' Variable pour stocker le nom de fichier complet pour chaque diapositive exportée en tant qu'image
'Sélection du dossier de destination
With Application.FileDialog(msoFileDialogFolderPicker)
.Title = "Sélectionner un dossier pour sauvegarder les images"
.AllowMultiSelect = False
If .Show <> -1 Then Exit Sub
strPath = .SelectedItems(1)
End With
'Exportation de chaque diapositive en tant qu'image
For Each sld In ActivePresentation.Slides
i = i + 1
strName = strPath & "\Slide" & i & "_" & sld.Shapes.Title.TextFrame.TextRange.Text & ".jpg"
sld.Export strName, "jpg", 800, 600
Next sld
'Message de confirmation
MsgBox "Les diapositives ont été exportées avec succès dans le dossier sélectionné."
End Sub
With sld.Shapes
you have access to all shapes of a slide. You access single shapes using the index: sld.Shapes(1)
or via its name: sld.Shapes("Subtitle 2")
. sld.Shapes.Count
tells you how many shapes you have on that slide.
Every slide in Powerpoint might have a title, but it doesn't need to. If you create a new slide, usually you see a shape that says "Click to add Title" - this will be the title of the slide. However, it's possible to delete this title shape or leave it blank and the slide will have to title.
sld.Shapes.Title
will give the the title shape. However, if a slide has no title at all, you will get a runtime error "Object does not exist". You can check if the title exists with sld.Shapes.HasTitle
.
So it is likely that you have a presentation where at least one slide has no title set. The following function will check if a title exist, if not, it will return the text of the first shape:
Function getSlideTitleText(sld As Slide) As String
If sld.Shapes.HasTitle Then
getSlideTitleText = sld.Shapes.Title.TextFrame.TextRange.Text
Exit Function
End If
Dim sh As Shape
For Each sh In sld.Shapes
If sh.HasTextFrame Then
getSlideTitleText = sh.TextFrame.TextRange.Text
Exit Function
End If
Next
getSlideTitleText = "(No title)"
End Function
Now all you have to do is replace the line where you set the file name with
strName = strPath & "\Slide" & i & "_" & getSlideTitleText(sld) & ".jpg"
If you want to check titles manually to find out which slide(s) are missing the title: On the "Review" ribbon, click "Check Accessibility":
Read here for more information