重命名每个幻灯片并以图像格式导出所有幻灯片。

huangapple go评论59阅读模式
英文:

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 &amp; &quot;\Slide&quot; &amp; i &amp; &quot;_&quot; &amp; sld.Shapes.Title.TextFrame.TextRange.Text &amp; &quot;.jpg&quot;

Sub ExportSlidesAsImages()
    &#39;D&#233;claration des variables
    Dim i As Integer &#39; Variable pour stocker l&#39;index de la diapositive en cours de traitement.
    Dim sld As slide &#39; Variable pour stocker la diapositive en cours de traitement.
    Dim strPath As String &#39; Variable pour stocker le chemin d&#39;acc&#232;s au dossier s&#233;lectionn&#233; par l&#39;utilisateur
    Dim strName As String &#39; Variable pour stocker le nom de fichier complet pour chaque diapositive export&#233;e en tant qu&#39;image
    
    &#39;S&#233;lection du dossier de destination
    With Application.FileDialog(msoFileDialogFolderPicker)
        .Title = &quot;S&#233;lectionner un dossier pour sauvegarder les images&quot;
        .AllowMultiSelect = False
        If .Show &lt;&gt; -1 Then Exit Sub
        strPath = .SelectedItems(1)
    End With
    
    &#39;Exportation de chaque diapositive en tant qu&#39;image
    For Each sld In ActivePresentation.Slides
        i = i + 1
        strName = strPath &amp; &quot;\Slide&quot; &amp; i &amp; &quot;_&quot; &amp; sld.Shapes.Title.TextFrame.TextRange.Text &amp; &quot;.jpg&quot;
        sld.Export strName, &quot;jpg&quot;, 800, 600
    Next sld
    
    &#39;Message de confirmation
    MsgBox &quot;Les diapositives ont &#233;t&#233; export&#233;es avec succ&#232;s dans le dossier s&#233;lectionn&#233;.&quot;
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(&quot;Subtitle 2&quot;). 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 = &quot;(No title)&quot;
End Function

Now all you have to do is replace the line where you set the file name with

strName = strPath &amp; &quot;\Slide&quot; &amp; i &amp; &quot;_&quot; &amp; getSlideTitleText(sld) &amp; &quot;.jpg&quot;

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

huangapple
  • 本文由 发表于 2023年3月31日 16:15:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/75896290.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定