Jean-Pascal
Jean-Pascal

Reputation: 1

Merge pdf files with VBA and Foxit

I use Foxit Phantompdf, the complete version and ACCESS. In our program, we have to save multiple pdf files, some of them should be merged in single files when saved. Here is the code I use;

Dim phApp As PhantomPDF.Application

Dim n1 As String

Dim n2 As String

n1 = "c:\Temp\F3769-190136-GROUPE OCÉAN.pdf"

n2 = "c:\Temp\f3769-190136-GROUPE OCÉAN- facture.pdf"

Set phApp = CreateObject("PhantomPDF.Application")

Dim phCreator As PhantomPDF.Creator

Set phCreator = phApp.Creator

***'Call phCreator.CombineFiles("c:\Temp\F3769-190136-GROUPE OCÉAN.pdf|c:\Temp\f3769-190136-GROUPE OCÉAN- facture.pdf", "c:\Temp\F3769-190136-GROUPE OCÉAN.pdf", COMBINE_ADD_CONTENTS)***

Call phCreator.CombineFiles("""c:\Temp\" & n1 & "|'" & n2 & """" & ", " & """c:\Temp\F3769-190136-GROUPE OCÉAN.pdf"""" &", COMBINE_ADD_CONTENTS)

phApp.Exit

When I try it with the complete files names (in bold) the code works perfectly. However, when I try to use variables, I get a

"Argument not optional"

error. Can somebody help me ? Thanks

Upvotes: 0

Views: 2943

Answers (3)

Mike M
Mike M

Reputation: 1

The code referenced above does not work in Foxit vs 10 and higher. The new api version is much simpler.

Here's a link to the current API: Foxit API reference guide

The following sample code to combine a list of files. This uses the pipe-delimited file name list. (example:

"C:\source\file01.pdf|C:\source\file02.pdf|C:\source\file03.pdf").

You can also have it combine all files in a named directory. See the API reference guide for other samples.

Private Sub pdfCombineFiles()
    Dim phCreator As FoxitPhantomPDF.Creator
    Set phCreator = CreateObject("FoxitExch.Creator")
    Dim nCombinedCnt As Integer

    Dim mergeList As String
    mergeList = Range("mergeList").Value
    
    Dim finalFile As String
    finalFile = Range("finalFile").Value
    
    nCombinedCnt = phCreator.CombineFiles(mergeList, finalFile)

    MsgBox "DONE: " & nCombinedCnt & " files combined"

End Sub

Upvotes: 0

Jean-Pascal
Jean-Pascal

Reputation: 1

I tried it, but got the same error message. So I took advantage of the fact that the function works when the file names are in plain text. I copied the files to be merged in a temporary folder and rename them. The renamed files are the then used in the merge function. It works perfectly, but Foxit adds a table of content page, and I don't know, yet, how to remove it. Here is my solution:

Private Sub Command4_Click()

Dim addi As String 'file to be merged to main file

Dim princi As String 'main file

Dim phApp As PhantomPDF.Application



'A temporary folder, in this case c:\t2, should be present

'In this example c:\Temp is the working folder



addi = "c:\Temp\filetomerge.pdf" 'full path of file to be merged

princi = "c:\Temp\mainfile.pdf" 'full path of main file



'fadd,pdf and fmain.pdf are the temporay files used in Foxit's function

FileCopy addi, "c:\t2\fadd.pdf" 'temporary file to be merged in temporary folder

FileCopy princi, "c:\t2\fmain.pdf" 'temporary main file in temporary folder



'Merge action

Set phApp = CreateObject("PhantomPDF.Application")

Dim phCreator As PhantomPDF.Creator

Set phCreator = phApp.Creator

Call phCreator.CombineFiles("c:\t2\fmain.pdf|c:\t2\fadd.pdf", "c:\t2\fmain.pdf", COMBINE_ADD_CONTENTS)



phApp.Exit



'Save merged file in working folder under main file name

Kill princi



FileCopy "c:\t2\fmain.pdf", princi



'delete temporary files

Kill "c:\t2\fadd.pdf"

Kill "c:\t2\fmain.pdf"

End Sub

Upvotes: 0

Nicklas
Nicklas

Reputation: 1

Your string definitions in call line is incorrect.

You have defined n1 and n2 with the c:\temp already, and in your string conversion you add this again. I do not know if this is the route cause to your issue.

Furthermore I do not know the actual needed syntax for this phcreator.combine() But is it not possible using only:

call pHcreator.combine(n1 & "|" & n2, …

The 'Argument not option' might imply you should add another input to your pHcreator, I would guess it could have something to do with FOXIT's combine function page settings. Try adding a input variable integer at the end of the function maybe?

But the fact that it works when writing strings in clear text would maybe suggest that the string manipulations is not correct?

I'm not a vba professional, but interested in the outcome, working myself with Foxit and also want to combine with vba. I'm currently not using version 9 som I seem to be out of luck, and only upgrading it I know what I want to do is possible.

Upvotes: 0

Related Questions