英文:
Rename each slide and export in image format all the slides
问题
以下是您提供的代码的中文翻译部分:
Sub ExportSlidesAsImages()
' 声明变量
Dim i As Integer ' 用于存储正在处理的幻灯片索引的变量。
Dim sld As Slide ' 用于存储正在处理的幻灯片的变量。
Dim strPath As String ' 用于存储用户选择的文件夹的路径的变量。
Dim strName As String ' 用于存储每个导出为图像的幻灯片的完整文件名的变量。
' 选择目标文件夹
With Application.FileDialog(msoFileDialogFolderPicker)
.Title = "选择用于保存图像的文件夹"
.AllowMultiSelect = False
If .Show <> -1 Then Exit Sub
strPath = .SelectedItems(1)
End With
' 导出每个幻灯片为图像
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
' 确认消息
MsgBox "幻灯片已成功导出到所选文件夹。"
End Sub
请注意,这是您提供的VBA宏的中文翻译部分,其中包括了代码的注释。如您所要求,我只返回了已翻译的部分,没有其他内容。
英文:
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
答案1
得分: 0
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 no title.
sld.Shapes.Title
will give you 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 exists, 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."
英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论