PowerPoint中单个拆分文件的文件名

时间:2014-12-16 09:19:30

标签: vba powerpoint save-as

我在MS PowerPoint中遇到问题。我目前正在处理大约十几个文件,每个文件至少有100张幻灯片。我需要创建单独的幻灯片作为单个文件以进行上载。我有一个代码将文件拆分成单独的幻灯片。查询是如何使用VBA代码自动重命名这些文件?

每张幻灯片在文本框中都有一个标题,标题文本框的形状和大小在各个套牌中是一致的。如何确保创建的单个文件与幻灯片的标题一起保存"作为文件名?

截至目前,该文件已保存为"原始文件名。 n - n "

非常感谢你的帮助。

以下是我在互联网上找到的将幻灯片分成多个单独文件的代码(一个幻灯片,为了我的目的):

Sub SplitFile()

    Dim lSlidesPerFile As Long
    Dim lTotalSlides As Long
    Dim oSourcePres As Presentation
    Dim otargetPres As Presentation
    Dim sFolder As String
    Dim sExt As String
    Dim sBaseName As String
    Dim lCounter As Long
    Dim lPresentationsCount As Long     ' how many will we split it into
    Dim x As Long
    Dim lWindowStart As Long
    Dim lWindowEnd As Long
    Dim sSplitPresName As String

    On Error GoTo ErrorHandler

    Set oSourcePres = ActivePresentation
    If Not oSourcePres.Saved Then
        MsgBox "Please save your presentation then try again"
        Exit Sub
    End If

    lSlidesPerFile = CLng(InputBox("How many slides per file?", "Split Presentation"))
    lTotalSlides = oSourcePres.Slides.Count
    sFolder = ActivePresentation.Path & "\"
    sExt = Mid$(ActivePresentation.Name, InStr(ActivePresentation.Name, ".") + 1)
    sBaseName = Mid$(ActivePresentation.Name, 1, InStr(ActivePresentation.Name, ".") - 1)

    If (lTotalSlides / lSlidesPerFile) - (lTotalSlides \ lSlidesPerFile) > 0 Then
        lPresentationsCount = lTotalSlides \ lSlidesPerFile + 1
    Else
        lPresentationsCount = lTotalSlides \ lSlidesPerFile
    End If

    If Not lTotalSlides > lSlidesPerFile Then
        MsgBox "There are fewer than " & CStr(lSlidesPerFile) & " slides in this presentation."
        Exit Sub
    End If

    For lCounter = 1 To lPresentationsCount

        ' which slides will we leave in the presentation?
        lWindowEnd = lSlidesPerFile * lCounter
        If lWindowEnd > oSourcePres.Slides.Count Then
            ' odd number of leftover slides in last presentation
            lWindowEnd = oSourcePres.Slides.Count
            lWindowStart = ((oSourcePres.Slides.Count \ lSlidesPerFile) * lSlidesPerFile) + 1
        Else
            lWindowStart = lWindowEnd - lSlidesPerFile + 1
        End If

        ' Make a copy of the presentation and open it
        sSplitPresName = sFolder & sBaseName & _
               "_" & CStr(lWindowStart) & "-" & CStr(lWindowEnd) & "." & sExt
        oSourcePres.SaveCopyAs sSplitPresName, ppSaveAsDefault
        Set otargetPres = Presentations.Open(sSplitPresName, , , True)

        With otargetPres
            For x = .Slides.Count To lWindowEnd + 1 Step -1
                .Slides(x).Delete
            Next
            For x = lWindowStart - 1 To 1 Step -1
                .Slides(x).Delete
            Next
            .Save
            .Close
        End With

    Next    ' lpresentationscount

NormalExit:
    Exit Sub
ErrorHandler:
    MsgBox "Error encountered"
    Resume NormalExit
End Sub

1 个答案:

答案 0 :(得分:0)

这将从标题中获取文本并将其用作输出演示文稿名称:

sSplitPresName = sFolder _
  & oSourcePres.Slides(lWindowStart).Shapes.Title.TextFrame.TextRange.Text _
  & "." & sExt

如果幻灯片没有标题,您将收到错误,因此您需要对此进行测试并决定如何命名拆分演示文稿。也许:

sSplitPresName = sFolder _
  & "Slide-" & Cstr(lWindowStart) _
  & "." & sExt

您还希望通过一个功能运行幻灯片标题,该功能可以删除在查看演示文稿的操作系统中无效的文件名字符。