I'm creating a program to merge pdf files. My problem right now is that for some reason it is not merging correctly. A lot of time it is only adding the second file to be merged into the new file and not adding the first one at all. This was working and I have not changed the code, so I'm not sure what is happening.

This is the code that I'm using to do this. I have already opend Acrobat and I have opened the first file:

' Get the first pdf file in the directory
If lstSelectedFiles.ListCount <> 0 Then
strFileName = lstSelectedFiles.List(n) 'Dir(strPath + PDF_WILDCARD, vbNormal)
End If

' Open the first file in the directory
AcroExchPDDoc.Open strPath + strFileName

' Get the name of the next file in the directory [if any]
If strFileName <> "" Then
n = n + 1
strFileName = lstSelectedFiles.List(n) 'Dir

' Start the loop.
Do While strFileName <> ""
' Get the total pages less one for the last page num [zero based]
iLastPage = AcroExchPDDoc.GetNumPages

Set AcroExchInsertPDDoc = CreateObject("AcroExch.PDDoc")

' Open the file to insert
AcroExchInsertPDDoc.Open strPath + strFileName

' Get the number of pages to insert
iNumberOfPagesToInsert = AcroExchInsertPDDoc.GetNumPages

' Insert the pages
AcroExchPDDoc.InsertPages iLastPage, AcroExchInsertPDDoc, 0, iNumberOfPagesToInsert, True

' Close the document
AcroExchInsertPDDoc.Close

' Get the name of the next file in the directory
n = n + 1

strFileName = lstSelectedFiles.List(n) 'dir
Loop

' Save the entire document as the JOIN_FILENAME using SaveFull [0x0001 = &H1]
AcroExchPDDoc.Save &H1, strPath + "\" + JOIN_FILENAME + ".pdf"

If you see a mistake please let me know.