Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vba 如何将特定页面从一个pdf附加到另一个pdf?_Vba_Excel - Fatal编程技术网

Vba 如何将特定页面从一个pdf附加到另一个pdf?

Vba 如何将特定页面从一个pdf附加到另一个pdf?,vba,excel,Vba,Excel,目前我有一个将PDF组合在一起的代码。 它从我在A3:A5列中指定的每个文件中获取所有页面,并附加到A2 假设我所有的PDF都有5页。但是,如果我只想拿前3页A3,整整5页A4和1页A5怎么办 此外,我不需要在页面之间指定,即A3的第2、4和5页。 一切都会井然有序,如1-3、1-5或1-2 我有一个计数器,可以得到页数 Dim i As Long, pgnumber As Range For Each pgnumber In Range("A2:A100") If Not

目前我有一个将PDF组合在一起的代码。 它从我在A3:A5列中指定的每个文件中获取所有页面,并附加到A2

假设我所有的PDF都有5页。但是,如果我只想拿前3页A3,整整5页A4和1页A5怎么办

此外,我不需要在页面之间指定,即A3的第2、4和5页。 一切都会井然有序,如1-3、1-5或1-2

我有一个计数器,可以得到页数

  Dim i As Long, pgnumber As Range
    For Each pgnumber In Range("A2:A100")
    If Not IsEmpty(pgnumber) Then
    i = i + 1
    AcroDoc.Open pgnumber
    PageNum = AcroDoc.GetNumPages
    Cells(pgnumber.Row, 4) = PageNum
    End If
    AcroDoc.Close
    Next pgnumber
完整代码:

Sub main3()

    Set app = CreateObject("Acroexch.app")

    Dim FilePaths As Collection
    Set FilePaths = New Collection
    Dim AcroDoc As Object
    Set AcroDoc = New AcroPDDoc

    'Counts # of pages in each pdf, loads to column D.

    Dim i As Long, pgnumber As Range
    For Each pgnumber In Range("A2:A100")
    If Not IsEmpty(pgnumber) Then
    i = i + 1
    AcroDoc.Open pgnumber
    PageNum = AcroDoc.GetNumPages
    Cells(pgnumber.Row, 4) = PageNum
    End If
    AcroDoc.Close
    Next pgnumber


    'Append to this file, ideally will be a front page to append to, commented out for now.

    'FilePaths.Add "\path\name\here"

    'Active or not feature in Column B, Specify Yes to include in combination, no to exclude

    Dim cell As Range
    For Each cell In Range("A2:A100")
    If cell.Offset(0, 1).Value2 <> "No" Then FilePaths.Add cell.Value2
    Next cell


    'Combine files which are listed in Column A.

    Set primaryDoc = CreateObject("AcroExch.PDDoc")
    OK = primaryDoc.Open(FilePaths(1))
    Debug.Print "PRIMARY DOC OPENED & PDDOC SET: " & OK

    For colIndex = 2 To FilePaths.Count
        numPages = primaryDoc.GetNumPages() - 1

        Set sourceDoc = CreateObject("AcroExch.PDDoc")
        OK = sourceDoc.Open(FilePaths(colIndex))
        Debug.Print "(" & colIndex & ") SOURCE DOC OPENED & PDDOC SET: " & OK

        numberOfPagesToInsert = sourceDoc.GetNumPages

        OK = primaryDoc.InsertPages(numPages, sourceDoc, 0, numberOfPagesToInsert, False)
        Debug.Print "(" & colIndex & ") PAGES INSERTED SUCCESSFULLY: " & OK

        Set sourceDoc = Nothing
    Next colIndex

    OK = primaryDoc.Save(PDSaveFull, FilePaths(1))
    Debug.Print "PRIMARYDOC SAVED PROPERLY: " & OK

    Set primaryDoc = Nothing
    app.Exit
    Set app = Nothing
    MsgBox "DONE"
End Sub
'attempt to do start and end page in col E and F.

    startPage = Range("E" & colIndex)
    endPage = Range("F" & colIndex)
    OK = sourceDoc.DeletePages(1, startPage - 1)
    OK = sourceDoc.DeletePages(endPage - startPage + 2, sourceDoc.GetNumPages)
Option Explicit

Sub AppendPDF()
Dim app                             As Object
Dim acroDoc                         As Object
Dim filePaths                       As Collection
Dim pathwayIterator                 As Range
Dim primaryDoc                      As Object
Dim OK                              As String
Dim numPages                        As Long
Dim colIndex                        As Long
Dim sourceDoc                       As Object
Const finalPage = 2

    Set app = CreateObject("Acroexch.app")
    Set acroDoc = New AcroPDDoc
    Set filePaths = New Collection

    For Each pathwayIterator In Range("A2:A100")
        If pathwayIterator.Value <> "" Then
            filePaths.Add pathwayIterator.Value2
        End If
    Next pathwayIterator

    Set primaryDoc = CreateObject("AcroExch.PDDoc")
    OK = primaryDoc.Open(filePaths(1))
    Debug.Print "PRIMARY DOC OPENED & PDDOC SET: " & OK

    For colIndex = 2 To filePaths.Count
        numPages = primaryDoc.GetNumPages() - 1

        Set sourceDoc = CreateObject("AcroExch.PDDoc")
        OK = sourceDoc.Open(filePaths(colIndex))
        Debug.Print "(" & colIndex & ") SOURCE DOC OPENED & PDDOC SET: " & OK

        OK = primaryDoc.InsertPages(numPages, sourceDoc, 0, finalPage, False)
        Debug.Print "(" & colIndex & ") PAGES INSERTED SUCCESSFULLY: " & OK

        sourceDoc.Close
        Set sourceDoc = Nothing
    Next colIndex

    OK = primaryDoc.Save(PDSaveFull, filePaths(1))
    Debug.Print "PRIMARYDOC SAVED PROPERLY: " & OK

    Set primaryDoc = Nothing
    app.Exit
    Set app = Nothing
    MsgBox "DONE"
End Sub

您可以先尝试删除每个pdf中不需要的部分,然后再将它们与
sourceDoc.DeletePages(startPage,endPage)
一起追加,例如:

OK = sourceDoc.Open(FilePaths(colIndex))

startPage = Range("C" & colIndex)
endPage = Range("D" & colIndex)
OK = sourceDoc.DeletePages(1, startPage - 1)
OK = sourceDoc.DeletePages(endPage - startPage + 2, sourceDoc.GetNumPages) ' just some arithmetic

Debug.Print "(" & colIndex & ") SOURCE DOC OPENED & PDDOC SET: " & OK

您只需为C&D列中的每一列指定
startPage
endPage
。。。或者,您可以更改此片段,并根据自己的喜好对其进行详细说明。下面有一个更接近完整的答案

见我对你问题的评论。如果这是正确的,这可能会解决问题:

加:

改变

If cell.Offset(0, 1).Value2 <> "No" Then FilePaths.Add cell.Value2
致:


更接近完整的答案

好吧,我知道我不该这么做,但我们开始吧。我已经修改了您的代码,使其按照我认为应该的方式工作。这不是一个完整的修订,因为整个过程可以在一个过程中完成,并且可以消除集合对象。以下代码中可能有bug,因为我没有AdobeAcrobatSDK。但是,我认为这会让你比以前更亲近,让一切都井然有序。您应该可以在此执行任何调试:

Sub CompileDocuments()

    Dim acroExchangeApp as Object   ' Needed because?
    Dim filePaths As Collection     ' Paths for PDFs to append
    Dim fileRows As Collection      ' Row numbers PDFs to append
    Dim fileIndex as Long           ' For walking through the collections
    Dim acroDoc As AcroPDDoc        ' Manages imported PDFs
    Dim sourceDoc as Object         ' Manages imported PDFs (Same as above?)
    Dim primaryDoc As Object        ' Everything gets appended to this
    Dim importPath As Range         ' Cell containing a PDF to append
    Dim pageCount As Long           ' Total pages in an appendable PDF
    Dim insertPoint as Long         ' PDFs will be appended after this page in the primary Doc
    Dim startPage as Long           ' First desired page of appended PDF
    Dim endPage as Long             ' Last desired page of appended PDF  

    ' Initialize
    Set filePaths = New Collection
    Set fileRows = New Collection
    Set acroDoc = New AcroPDDoc
    Set acroExchangeApp = CreateObject("Acroexch.app")
    Set primaryDoc = CreateObject("AcroExch.PDDoc")

    ' Pass through rows setting page numbers and capturing paths
    For Each importPath In Range("A2:A100")

        ' Put the page count of each PDF document in column D
        If Not IsEmpty(importPath) Then
            acroDoc.Open importPath
            pageCount = acroDoc.GetNumPages
            importPath.OffSet(0,3) = pageCount
            acroDoc.Close
        End If
        Set acroDoc = Nothing

        ' Remember which documents to append and the row on which they appear
        ' Skipping any rows with "No" in column B
        If importPath.Offset(0, 1).Value2 <> "No" Then
            filePaths.Add importPath.Value2
            fileRows.Add  importPath.Row
        End If

    Next importPath

    ' Combine all file listed in Column A.
    ' Start by opening the file in A2.
    OK = primaryDoc.Open(filePaths(1))
    Debug.Print "PRIMARY DOC OPENED & PDDOC SET: " & OK

    ' Loop through the remaining files, appending pages to A2
    ' Note that columns E and F define the desired pages to extract from
    '   the appended document.

    For fileIndex = 2 To filePaths.Count

        ' Pages will be added after this insert point
        insertPoint = primaryDoc.GetNumPages() - 1

        ' Open the source document
        Set sourceDoc = CreateObject("AcroExch.PDDoc")
        OK = sourceDoc.Open(filePaths(fileIndex))
        Debug.Print "(" & fileIndex & ") SOURCE DOC OPENED & PDDOC SET: " & OK

        ' Get start and end pages
        startPage = Range("E" & CStr(fileRows(fileIndex))).Value
        endPage = Range("F" & CStr(fileRows(fileIndex))).Value

        OK = primaryDoc.InsertPages(insertPoint, sourceDoc, startPage, endPage-startPage+1, False)
        Debug.Print "(" & fileIndex & ") " & endPage-startPage+1 & " PAGES INSERTED SUCCESSFULLY: " & OK

        Set sourceDoc = Nothing

    Next fileIndex

    OK = primaryDoc.Save(PDSaveFull, filePaths(1))
    Debug.Print "primaryDoc SAVED PROPERLY: " & OK

    Set primaryDoc = Nothing
    acroExchangeApp.Exit
    Set acroExchangeApp = Nothing

    MsgBox "DONE"

End Sub
子编译文档()
是否需要将ExchangeApp设置为对象“”,因为?
Dim FilePath作为要附加的PDF的集合路径
Dim fileRows作为集合的行号要附加的PDF
Dim fileIndex为“长”,用于遍历集合
Dim acroDoc作为AcroPDDoc管理导入的PDF
Dim sourceDoc as Object'管理导入的PDF(同上?)
Dim primaryDoc作为对象“所有内容都附加到此对象”
Dim importPath As Range“包含要附加的PDF的单元格
可追加PDF中的Dim pageCount As Long“总页数
Dim insertPoint as Long’PDF将附加在主文档中此页面之后
Dim startPage作为附加PDF的第一页
Dim endPage作为附加PDF的最后一页
“初始化
Set filepath=新集合
Set fileRows=新集合
设置AcrodDoc=新建AcrodDoc
设置acroExchangeApp=CreateObject(“Acroexch.app”)
Set primaryDoc=CreateObject(“AcroExch.PDDoc”)
'通过行设置页码和捕获路径
对于范围内的每个入口路径(“A2:A100”)
'将每个PDF文档的页数放在D列中
如果不是IsEmpty(导入路径),则
acroDoc.打开导入路径
pageCount=acroDoc.GetNumPages
导入路径偏移量(0,3)=页面计数
acroDoc.关闭
如果结束
设置acroDoc=Nothing
'记住要附加哪些文档以及它们出现的行
'跳过B列中有“否”的任何行
如果导入路径偏移量(0,1).Value2为“否”,则
文件路径。添加importPath.Value2
fileRows.addimportPath.Row
如果结束
下一个导入路径
'合并列A中列出的所有文件。
'首先在A2中打开文件。
OK=primaryDoc.Open(文件路径(1))
调试。打印“主文档打开和PDDOC集:”&确定
'循环浏览其余文件,将页面附加到A2
'请注意,列E和F定义了要从中提取的所需页面
"附文。
对于fileIndex=2到filepath.Count
'页面将添加到此插入点之后
insertPoint=primaryDoc.GetNumPages()-1
'打开源文档
设置sourceDoc=CreateObject(“AcroExch.PDDoc”)
确定=sourceDoc.Open(文件路径(文件索引))
Debug.Print“(“&fileIndex&”)打开源文档并设置PDDOC:”&确定
'获取起始页和结束页
起始页=范围(“E”和CStr(文件行(文件索引))。值
endPage=Range(“F”&CStr(fileRows(fileIndex))).Value
OK=primaryDoc.InsertPages(插入点、sourceDoc、起始页、结束页起始页+1,False)
Debug.Print“(“&fileIndex&”)和endPage startPage+1&”页面插入成功:“&OK”
设置sourceDoc=Nothing
下一个文件索引
OK=primaryDoc.Save(PDSaveFull,文件路径(1))
调试。打印“primaryDoc已正确保存:”&确定
设置primaryDoc=Nothing
acroExchangeApp.出口
设置acroExchangeApp=Nothing
MsgBox“完成”
端接头

说明:

Sub main3()

    Set app = CreateObject("Acroexch.app")

    Dim FilePaths As Collection
    Set FilePaths = New Collection
    Dim AcroDoc As Object
    Set AcroDoc = New AcroPDDoc

    'Counts # of pages in each pdf, loads to column D.

    Dim i As Long, pgnumber As Range
    For Each pgnumber In Range("A2:A100")
    If Not IsEmpty(pgnumber) Then
    i = i + 1
    AcroDoc.Open pgnumber
    PageNum = AcroDoc.GetNumPages
    Cells(pgnumber.Row, 4) = PageNum
    End If
    AcroDoc.Close
    Next pgnumber


    'Append to this file, ideally will be a front page to append to, commented out for now.

    'FilePaths.Add "\path\name\here"

    'Active or not feature in Column B, Specify Yes to include in combination, no to exclude

    Dim cell As Range
    For Each cell In Range("A2:A100")
    If cell.Offset(0, 1).Value2 <> "No" Then FilePaths.Add cell.Value2
    Next cell


    'Combine files which are listed in Column A.

    Set primaryDoc = CreateObject("AcroExch.PDDoc")
    OK = primaryDoc.Open(FilePaths(1))
    Debug.Print "PRIMARY DOC OPENED & PDDOC SET: " & OK

    For colIndex = 2 To FilePaths.Count
        numPages = primaryDoc.GetNumPages() - 1

        Set sourceDoc = CreateObject("AcroExch.PDDoc")
        OK = sourceDoc.Open(FilePaths(colIndex))
        Debug.Print "(" & colIndex & ") SOURCE DOC OPENED & PDDOC SET: " & OK

        numberOfPagesToInsert = sourceDoc.GetNumPages

        OK = primaryDoc.InsertPages(numPages, sourceDoc, 0, numberOfPagesToInsert, False)
        Debug.Print "(" & colIndex & ") PAGES INSERTED SUCCESSFULLY: " & OK

        Set sourceDoc = Nothing
    Next colIndex

    OK = primaryDoc.Save(PDSaveFull, FilePaths(1))
    Debug.Print "PRIMARYDOC SAVED PROPERLY: " & OK

    Set primaryDoc = Nothing
    app.Exit
    Set app = Nothing
    MsgBox "DONE"
End Sub
'attempt to do start and end page in col E and F.

    startPage = Range("E" & colIndex)
    endPage = Range("F" & colIndex)
    OK = sourceDoc.DeletePages(1, startPage - 1)
    OK = sourceDoc.DeletePages(endPage - startPage + 2, sourceDoc.GetNumPages)
Option Explicit

Sub AppendPDF()
Dim app                             As Object
Dim acroDoc                         As Object
Dim filePaths                       As Collection
Dim pathwayIterator                 As Range
Dim primaryDoc                      As Object
Dim OK                              As String
Dim numPages                        As Long
Dim colIndex                        As Long
Dim sourceDoc                       As Object
Const finalPage = 2

    Set app = CreateObject("Acroexch.app")
    Set acroDoc = New AcroPDDoc
    Set filePaths = New Collection

    For Each pathwayIterator In Range("A2:A100")
        If pathwayIterator.Value <> "" Then
            filePaths.Add pathwayIterator.Value2
        End If
    Next pathwayIterator

    Set primaryDoc = CreateObject("AcroExch.PDDoc")
    OK = primaryDoc.Open(filePaths(1))
    Debug.Print "PRIMARY DOC OPENED & PDDOC SET: " & OK

    For colIndex = 2 To filePaths.Count
        numPages = primaryDoc.GetNumPages() - 1

        Set sourceDoc = CreateObject("AcroExch.PDDoc")
        OK = sourceDoc.Open(filePaths(colIndex))
        Debug.Print "(" & colIndex & ") SOURCE DOC OPENED & PDDOC SET: " & OK

        OK = primaryDoc.InsertPages(numPages, sourceDoc, 0, finalPage, False)
        Debug.Print "(" & colIndex & ") PAGES INSERTED SUCCESSFULLY: " & OK

        sourceDoc.Close
        Set sourceDoc = Nothing
    Next colIndex

    OK = primaryDoc.Save(PDSaveFull, filePaths(1))
    Debug.Print "PRIMARYDOC SAVED PROPERLY: " & OK

    Set primaryDoc = Nothing
    app.Exit
    Set app = Nothing
    MsgBox "DONE"
End Sub
对于第一个代码,我删除了所有内容,但不包括基本内容:附加到文档的文件路径和要附加到主文档的页面的文件路径

我为我们设置了一个常数,并将其设置为2。我们可以将其设置为3或5等。该常量将在insertpage函数的页到端部分中传递。我有一种感觉,你会说pdf中的总页数和要追加的页数之间有某种关系,但这在OP中并不清楚

分解INSERTPAGES():

Sub main3()

    Set app = CreateObject("Acroexch.app")

    Dim FilePaths As Collection
    Set FilePaths = New Collection
    Dim AcroDoc As Object
    Set AcroDoc = New AcroPDDoc

    'Counts # of pages in each pdf, loads to column D.

    Dim i As Long, pgnumber As Range
    For Each pgnumber In Range("A2:A100")
    If Not IsEmpty(pgnumber) Then
    i = i + 1
    AcroDoc.Open pgnumber
    PageNum = AcroDoc.GetNumPages
    Cells(pgnumber.Row, 4) = PageNum
    End If
    AcroDoc.Close
    Next pgnumber


    'Append to this file, ideally will be a front page to append to, commented out for now.

    'FilePaths.Add "\path\name\here"

    'Active or not feature in Column B, Specify Yes to include in combination, no to exclude

    Dim cell As Range
    For Each cell In Range("A2:A100")
    If cell.Offset(0, 1).Value2 <> "No" Then FilePaths.Add cell.Value2
    Next cell


    'Combine files which are listed in Column A.

    Set primaryDoc = CreateObject("AcroExch.PDDoc")
    OK = primaryDoc.Open(FilePaths(1))
    Debug.Print "PRIMARY DOC OPENED & PDDOC SET: " & OK

    For colIndex = 2 To FilePaths.Count
        numPages = primaryDoc.GetNumPages() - 1

        Set sourceDoc = CreateObject("AcroExch.PDDoc")
        OK = sourceDoc.Open(FilePaths(colIndex))
        Debug.Print "(" & colIndex & ") SOURCE DOC OPENED & PDDOC SET: " & OK

        numberOfPagesToInsert = sourceDoc.GetNumPages

        OK = primaryDoc.InsertPages(numPages, sourceDoc, 0, numberOfPagesToInsert, False)
        Debug.Print "(" & colIndex & ") PAGES INSERTED SUCCESSFULLY: " & OK

        Set sourceDoc = Nothing
    Next colIndex

    OK = primaryDoc.Save(PDSaveFull, FilePaths(1))
    Debug.Print "PRIMARYDOC SAVED PROPERLY: " & OK

    Set primaryDoc = Nothing
    app.Exit
    Set app = Nothing
    MsgBox "DONE"
End Sub
'attempt to do start and end page in col E and F.

    startPage = Range("E" & colIndex)
    endPage = Range("F" & colIndex)
    OK = sourceDoc.DeletePages(1, startPage - 1)
    OK = sourceDoc.DeletePages(endPage - startPage + 2, sourceDoc.GetNumPages)
Option Explicit

Sub AppendPDF()
Dim app                             As Object
Dim acroDoc                         As Object
Dim filePaths                       As Collection
Dim pathwayIterator                 As Range
Dim primaryDoc                      As Object
Dim OK                              As String
Dim numPages                        As Long
Dim colIndex                        As Long
Dim sourceDoc                       As Object
Const finalPage = 2

    Set app = CreateObject("Acroexch.app")
    Set acroDoc = New AcroPDDoc
    Set filePaths = New Collection

    For Each pathwayIterator In Range("A2:A100")
        If pathwayIterator.Value <> "" Then
            filePaths.Add pathwayIterator.Value2
        End If
    Next pathwayIterator

    Set primaryDoc = CreateObject("AcroExch.PDDoc")
    OK = primaryDoc.Open(filePaths(1))
    Debug.Print "PRIMARY DOC OPENED & PDDOC SET: " & OK

    For colIndex = 2 To filePaths.Count
        numPages = primaryDoc.GetNumPages() - 1

        Set sourceDoc = CreateObject("AcroExch.PDDoc")
        OK = sourceDoc.Open(filePaths(colIndex))
        Debug.Print "(" & colIndex & ") SOURCE DOC OPENED & PDDOC SET: " & OK

        OK = primaryDoc.InsertPages(numPages, sourceDoc, 0, finalPage, False)
        Debug.Print "(" & colIndex & ") PAGES INSERTED SUCCESSFULLY: " & OK

        sourceDoc.Close
        Set sourceDoc = Nothing
    Next colIndex

    OK = primaryDoc.Save(PDSaveFull, filePaths(1))
    Debug.Print "PRIMARYDOC SAVED PROPERLY: " & OK

    Set primaryDoc = Nothing
    app.Exit
    Set app = Nothing
    MsgBox "DONE"
End Sub
INSERTPAGES(插入开始的页码(在primaryDoc内部)、作为插入页面源的PDF的路径(sourcedoc路径)、起始页面(sourcedoc)、结束页面(sourcedoc)、是否插入书籍

编码裸骨:

Sub main3()

    Set app = CreateObject("Acroexch.app")

    Dim FilePaths As Collection
    Set FilePaths = New Collection
    Dim AcroDoc As Object
    Set AcroDoc = New AcroPDDoc

    'Counts # of pages in each pdf, loads to column D.

    Dim i As Long, pgnumber As Range
    For Each pgnumber In Range("A2:A100")
    If Not IsEmpty(pgnumber) Then
    i = i + 1
    AcroDoc.Open pgnumber
    PageNum = AcroDoc.GetNumPages
    Cells(pgnumber.Row, 4) = PageNum
    End If
    AcroDoc.Close
    Next pgnumber


    'Append to this file, ideally will be a front page to append to, commented out for now.

    'FilePaths.Add "\path\name\here"

    'Active or not feature in Column B, Specify Yes to include in combination, no to exclude

    Dim cell As Range
    For Each cell In Range("A2:A100")
    If cell.Offset(0, 1).Value2 <> "No" Then FilePaths.Add cell.Value2
    Next cell


    'Combine files which are listed in Column A.

    Set primaryDoc = CreateObject("AcroExch.PDDoc")
    OK = primaryDoc.Open(FilePaths(1))
    Debug.Print "PRIMARY DOC OPENED & PDDOC SET: " & OK

    For colIndex = 2 To FilePaths.Count
        numPages = primaryDoc.GetNumPages() - 1

        Set sourceDoc = CreateObject("AcroExch.PDDoc")
        OK = sourceDoc.Open(FilePaths(colIndex))
        Debug.Print "(" & colIndex & ") SOURCE DOC OPENED & PDDOC SET: " & OK

        numberOfPagesToInsert = sourceDoc.GetNumPages

        OK = primaryDoc.InsertPages(numPages, sourceDoc, 0, numberOfPagesToInsert, False)
        Debug.Print "(" & colIndex & ") PAGES INSERTED SUCCESSFULLY: " & OK

        Set sourceDoc = Nothing
    Next colIndex

    OK = primaryDoc.Save(PDSaveFull, FilePaths(1))
    Debug.Print "PRIMARYDOC SAVED PROPERLY: " & OK

    Set primaryDoc = Nothing
    app.Exit
    Set app = Nothing
    MsgBox "DONE"
End Sub
'attempt to do start and end page in col E and F.

    startPage = Range("E" & colIndex)
    endPage = Range("F" & colIndex)
    OK = sourceDoc.DeletePages(1, startPage - 1)
    OK = sourceDoc.DeletePages(endPage - startPage + 2, sourceDoc.GetNumPages)
Option Explicit

Sub AppendPDF()
Dim app                             As Object
Dim acroDoc                         As Object
Dim filePaths                       As Collection
Dim pathwayIterator                 As Range
Dim primaryDoc                      As Object
Dim OK                              As String
Dim numPages                        As Long
Dim colIndex                        As Long
Dim sourceDoc                       As Object
Const finalPage = 2

    Set app = CreateObject("Acroexch.app")
    Set acroDoc = New AcroPDDoc
    Set filePaths = New Collection

    For Each pathwayIterator In Range("A2:A100")
        If pathwayIterator.Value <> "" Then
            filePaths.Add pathwayIterator.Value2
        End If
    Next pathwayIterator

    Set primaryDoc = CreateObject("AcroExch.PDDoc")
    OK = primaryDoc.Open(filePaths(1))
    Debug.Print "PRIMARY DOC OPENED & PDDOC SET: " & OK

    For colIndex = 2 To filePaths.Count
        numPages = primaryDoc.GetNumPages() - 1

        Set sourceDoc = CreateObject("AcroExch.PDDoc")
        OK = sourceDoc.Open(filePaths(colIndex))
        Debug.Print "(" & colIndex & ") SOURCE DOC OPENED & PDDOC SET: " & OK

        OK = primaryDoc.InsertPages(numPages, sourceDoc, 0, finalPage, False)
        Debug.Print "(" & colIndex & ") PAGES INSERTED SUCCESSFULLY: " & OK

        sourceDoc.Close
        Set sourceDoc = Nothing
    Next colIndex

    OK = primaryDoc.Save(PDSaveFull, filePaths(1))
    Debug.Print "PRIMARYDOC SAVED PROPERLY: " & OK

    Set primaryDoc = Nothing
    app.Exit
    Set app = Nothing
    MsgBox "DONE"
End Sub
选项显式
附属附件PDF(
Dim应用程序作为对象
将acroDoc作为对象
将文件路径设置为集合
Dim Pathway迭代器作为范围
Dim primaryDoc作为对象
暗淡OK