+ Reply to Thread
Results 1 to 7 of 7

loop trough all folders and subfolders and merge pdf files

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    08-02-2012
    Location
    Austria
    MS-Off Ver
    Excel 2007
    Posts
    457

    loop trough all folders and subfolders and merge pdf files

    Hi all,

    I found the below code:
     Sub Button1_Click()
    
        Dim AcroApp As Acrobat.CAcroApp
    
        Dim Part1Document As Acrobat.CAcroPDDoc
        Dim Part2Document As Acrobat.CAcroPDDoc
    
        Dim numPages As Integer
    
        Set AcroApp = CreateObject("AcroExch.App")
    
        Set Part1Document = CreateObject("AcroExch.PDDoc")
        Set Part2Document = CreateObject("AcroExch.PDDoc")
    
        Part1Document.Open ("C:\temp\Part1.pdf")
        Part2Document.Open ("C:\temp\Part2.pdf")
    
        ' Insert the pages of Part2 after the end of Part1
        numPages = Part1Document.GetNumPages()
    
        If Part1Document.InsertPages(numPages - 1, Part2Document,
    		0, Part2Document.GetNumPages(), True) = False Then
            MsgBox "Cannot insert pages"
        End If
    
        If Part1Document.Save(PDSaveFull, "C:\temp\MergedFile.pdf") = False Then
            MsgBox "Cannot save the modified document"
        End If
    
        Part1Document.Close
        Part2Document.Close
    
        AcroApp.Exit
        Set AcroApp = Nothing
        Set Part1Document = Nothing
        Set Part2Document = Nothing
    
        MsgBox "Done"
    
    End Sub
    on this link:
    http://khkonsulting.com/2009/03/adob...-introduction/

    I need to adjust it to get the path of the file from excel sheet, starting at row 2 and down. It is possible that it will have only a few or a lot and with different names.

    how to create a loop that will set the part 1, part 2 and so on depending on how many files there are in the folder and subfolders.
    and, the maybe harder part would be to add also the names of the files as bookmarks in the merged pdf file.

    greetings.
    Last edited by Megatronixs; 10-23-2018 at 09:49 AM.

  2. #2
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,486

    Re: loop trough all folders and subfolders and merge pdf files

    [CODE][/Sub Bu
    ........
    End SubCODE]





    The code needs to be in between
     your code
    codeBtweenCode.jpg
    Last edited by davesexcel; 10-23-2018 at 08:47 AM.

  3. #3
    Forum Contributor
    Join Date
    08-02-2012
    Location
    Austria
    MS-Off Ver
    Excel 2007
    Posts
    457

    Re: loop trough all folders and subfolders and merge pdf files

    fixed, no clue what happened.

  4. #4
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,486

    Re: loop trough all folders and subfolders and merge pdf files

    Attaching a sample workbook enables others to work on your problem:

    To attach a sample workbook. Make sure there is just enough data to make it clear what is needed. Include BEFORE/AFTER sheets if needed to show the process you're trying to complete or automate. Remember to desensitize the data.

    Click on GO ADVANCED and click "manage attachments" to open the upload window.


    To add a file to a post

  5. #5
    Forum Contributor
    Join Date
    08-02-2012
    Location
    Austria
    MS-Off Ver
    Excel 2007
    Posts
    457

    Re: loop trough all folders and subfolders and merge pdf files

    Hi,

    this is locked at our work, I'm not able to upload things :-(

    all it comes down that the one macro gets all the pdf files in a folder and list them on a sheet. Then I want to pass it as this part, but it needs to loop trough them:
        Part1Document.Open ("C:\temp\Part1.pdf")
        Part2Document.Open ("C:\temp\Part2.pdf")
    Greetings.

  6. #6
    Forum Contributor
    Join Date
    08-02-2012
    Location
    Austria
    MS-Off Ver
    Excel 2007
    Posts
    457

    Re: loop trough all folders and subfolders and merge pdf files

    Hi all,

    I found some other code that works fine too, but I get stuck on the array part :-(

    This is the code that is almost working now:

    Sub PopulatingArrayVariable()
    'PURPOSE: Dynamically Create Array Variable based on a Given Size
    
    'Dim myArray() As Variant
    Dim myArray() As String
    Dim DataRange As Range
    Dim cell As Range
    Dim x As Long
    Dim strPDFs As String
    
    'Determine the data you want stored
      Set DataRange = ActiveSheet.UsedRange
    
    'Resize Array prior to loading data
      ReDim myArray(DataRange.Cells.Count)
    
    'Loop through each cell in Range and store value in Array
      For Each cell In DataRange.Cells
        myArray(x) = cell.Value
        x = x + 1
      Next cell
    
    'Print values to Immediate Window (Ctrl + G to view)
      For x = LBound(myArray) To UBound(myArray)
        Debug.Print myArray(x)
      Next x
    
    bSuccess = MergePDFs(myArray, "C:\Documents\PutThemThere\MyNewPDF.pdf")
    
    If bSuccess = False Then MsgBox "Failed to combine all PDFs", vbCritical, "Failed to Merge PDFs"
    
    End Sub
    
    
    Private Function MergePDFs(arrFiles() As String, strSaveAs As String) As Boolean
    '---------------------------------------------------------------------------------------------------
    '---PROGRAM: MergePDFs------------------------------------------------------------------------------
    '---DEVELOPER: Ryan Wells (wellsr.com)--------------------------------------------------------------
    '---DATE: 09/2017-----------------------------------------------------------------------------------
    '---DESCRIPTION: This function uses Adobe Acrobat (won't work with just the Reader!) to-------------
    '---             combine PDFs into one PDF and save the new PDF with its own file name.-------------
    '---INPUT: The function requires two arguments.-----------------------------------------------------
    '---       1) arrFiles is an array of strings containing the full path to each PDF you want to------
    '---          combine in the order you want them combined.------------------------------------------
    '---       2) strSaveAs is a string containing the full path you want to save the new PDF as.-------
    '---REQUIREMENTS: 1) Must add a reference to "Adobe Acrobat X.0 Type Library" or "Acrobat"----------
    '---                 under Tools > References. This has been tested with Acrobat 6.0 and 10.0.------
    '---CAUTION: This function won't work unless you have the full Adobe Acrobat. In other words,-------
    '            Adobe Reader will not work.------------------------------------------------------------
    '---------------------------------------------------------------------------------------------------
     
    Dim objCAcroPDDocDestination As Acrobat.CAcroPDDoc
    Dim objCAcroPDDocSource As Acrobat.CAcroPDDoc
    Dim i As Integer
    Dim iFailed As Integer
     
    On Error GoTo NoAcrobat:
    'Initialize the Acrobat objects
    Set objCAcroPDDocDestination = CreateObject("AcroExch.PDDoc")
    Set objCAcroPDDocSource = CreateObject("AcroExch.PDDoc")
     
    'Open Destination, all other documents will be added to this and saved with
    'a new filename
    objCAcroPDDocDestination.Open (arrFiles(LBound(arrFiles))) 'open the first file
    
     
    'Open each subsequent PDF that you want to add to the original
      'Open the source document that will be added to the destination
        For i = LBound(arrFiles) + 1 To UBound(arrFiles)
            objCAcroPDDocSource.Open (arrFiles(i))
            If objCAcroPDDocDestination.InsertPages(objCAcroPDDocDestination.GetNumPages - 1, objCAcroPDDocSource, 0, objCAcroPDDocSource.GetNumPages, 0) Then
              MergePDFs = True
            Else
              'failed to merge one of the PDFs
              iFailed = iFailed + 1
            End If
            objCAcroPDDocSource.Close
        Next i
    objCAcroPDDocDestination.Save 1, strSaveAs 'Save it as a new name
    objCAcroPDDocDestination.Close
    Set objCAcroPDDocSource = Nothing
    Set objCAcroPDDocDestination = Nothing
     
    NoAcrobat:
    If iFailed <> 0 Then
        MergePDFs = False
    End If
    On Error GoTo 0
    End Function
    The "myArray" is the part that actually does not build up the array and then the merge pdf files is empty and stops.

    Any way to find the error above?

    Greetings.

  7. #7
    Forum Contributor
    Join Date
    08-02-2012
    Location
    Austria
    MS-Off Ver
    Excel 2007
    Posts
    457

    Re: loop trough all folders and subfolders and merge pdf files

    ok, so the problem here is that the array has an extra blank value at the end. this causes the pdf merging to fail.
    how to get rid of this extra blank value there?

    Greetings.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. VBA code to loop through folders and subfolders under date time conditions ?
    By hopeful_positive in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-19-2018, 11:45 AM
  2. how to copy and paste folders / subfolders and files
    By ColemanJames in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-10-2018, 07:32 PM
  3. Loop through folders and subfolders - Tweaking existing code
    By dluhut in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 07-29-2016, 09:20 AM
  4. [SOLVED] Search for files in folders and subfolders
    By Rick_Stanich in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 04-16-2014, 08:43 AM
  5. Loop trough all folders and subfolders and copy data
    By forfiett in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-20-2013, 10:22 AM
  6. Map/List of folders, subfolders & files
    By Bogdan in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 06-11-2006, 01:10 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1