VBA-Powerpoint演示文稿(和PDF)合并

时间:2020-03-31 15:54:28

标签: vba pdf powerpoint

我正在使用以下代码将许多其他PowerPoint演示文稿中的Powerpoint演示文稿放在一起:

Sub InsertFromOtherPres()
    Dim xlApp As Object
    Dim xlWorkBook As Object
    Dim i, j As Byte
    Dim wbname As String
    Dim sldB, sldE As Byte

    Set xlApp = CreateObject("Excel.Application")
    xlApp.Visible = True

    On Error Resume Next

    Set xlWorkBook = xlApp.Workbooks.Open("C:\Users\----\OneDrive\Desktop\Roli PPT\Book - Pages - Macro.xlsm", True, False)

    On Error GoTo 0

    j = 3

    For i = 2 To 154
        wbname = "C:\Users\----\OneDrive\Desktop\Roli PPT\" & xlWorkBook.Sheets("Sheet1").Cells(i, "K").Value

        sldB = xlWorkBook.Sheets("Sheet1").Cells(i, "L").Value
        sldE = xlWorkBook.Sheets("Sheet1").Cells(i, "L").Value

        ActivePresentation.Slides.InsertFromFile FileName:=wbname, Index:=j, SlideStart:=sldB, SlideEnd:=sldE

        j = j + 1
    Next i

    Set xlApp = Nothing
    Set xlWorkBook = Nothing

    MsgBox "Ready"
End Sub

在excel文件中,“ K”列是源ppts的名称,在“ L”列中是需要复制的幻灯片编号。但是,当宏到达“ L”列中的数字大于26的行时,我会收到一条错误消息(这意味着所需的幻灯片在源ppt中大于26)
Run-time error '-213718860 (80048240)': Slides (unknown member) : Integer out of range. 27 is not in the valid range of 1 to 26.

有人可以帮忙吗?

我也在寻找一个简单的宏,该宏可以类似于上面的内容,可以将pdf文件的给定页面复制到另一个pdf文件,同时还给出要复制的确切位置(页码)。

1 个答案:

答案 0 :(得分:0)

我没有机会对其进行测试,但是这段代码应该将许多幻灯片从Source演示文稿复制到Destination演示文稿。

如果给出无效数字(例如“复制0张幻灯片” ),它将出错,并且会自动进行溢出调整(例如,“复制7张幻灯片中的1至10张幻灯片” < / em>或“在幻灯片20的15中插入” )-我认为这两个都是您可能遇到的错误。

Private Function CopySlidesToPresentation(ByRef Source As Presentation, ByVal CopyStart As Long, ByVal CopySlides As Long, _
    ByRef Destination As Presentation, Optional ByVal InsertAt As Long = -1) As Boolean
    'Source: Presentation to copy from
    'CopyStart: First slide to copy
    'CopySlides: How many slides to copy
    'Destination: Presentation to copy to
    '~~OPTIONAL~~
    'InsertAt: Position to insert at.  If omitted, will insert at the end of the Presentation
    '~~RETURNS~~
    'TRUE if all slides copy successfully
    'FALSE if unable to copy slides

    Dim CurrentSlide As Long

    CopySlidesToPresentation = False
    If CopyStart < 1 Then Exit Function 'Cannot start before the First Slide
    If CopySlides < 1 Then Exit Function 'Cannot copy No or Negative Slides
    If CopyStart > Source.Slides.Count Then Exit Function 'Cannot copy after the Last Slide
    If InsertAt < 1 Then Exit Function 'Cannot Insert before the Presentation starts

    If CopyStart + CopySlides > Source.Slides.Count Then CopySlides = 1 + Source.Slides.Count - CopyStart 'Trim to Presentation Length
    If InsertAt > Destination.Slides.Count Then InsertAt = -1 'Trim to Presentation Length

    On Error GoTo FunctionError

    For CurrentSlide = 0 To CopySlides - 1 'Copy each slide in turn
        Source.Slides(CopyStart + CurrentSlide).Copy
        If InsertAt > 0 Then
            Destination.Slides.Paste InsertAt + CurrentSlide
        Else
            Destination.Slides.Paste 'Put it at the end
        End If
    Next CurrentSlide

    CopySlidesToPresentation = True 'Success!

FunctionError:
    On Error GoTo -1 'Clear the Error Handler
End Function