irvinew
01-25-2017, 03:30 PM
I have a merging pdf access problem. The fillable form part works; it cycles through the recordset fine.
What doesn't work is that when it loops though the set; it doesn't merge correctly. It keeps merging the same file over and over. For example if Jane Doe was the first record, it would fill out the fillable form correctly with her and then to fill the form with the other people as the recordset loops correctly. When it merges the final file later in the program execution it has the same name over and over.
In summary, the merged file has Jane Doe and Jane Doe.
I don't know why the merged copy doesn't have the right record in it and just the same name. The most recent fillable form has a different name so I am unclear why the final merge has the same record over and over
I have been tinkering with the insert fxn with no luck; any ideas?
This is my code:
Dim FileNm1, FileNm2, gApp, avDoc, pdDoc, jso
Dim rst1 As Recordset
Dim rst2 As Recordset
Dim strsql2
Dim strsql1
' sql omitted from post.
strsql1 = """
Dim x As Variant
Dim myStudentName
FileNm1 = "C:\SVN\954_T4A\fillable\2016_T4A_Form.pdf" 'File location
Set gApp = CreateObject("AcroExch.app")
Set avDoc = CreateObject("AcroExch.AVDoc")
Dim counter
counter = 1
If avDoc.Open(FileNm1, "") Then
Set pdDoc = avDoc.GetPDDoc()
Set jso = pdDoc.GetJSObject
Set rst1 = CurrentDb.OpenRecordset(strsql1)
rst1.MoveLast
Do While Not rst1.BOF
myStudentName = rst1(3) & " " & rst1(4) & " " & rst1(2)
Set x = jso.getField("student_name")
x.Value = myStudentName
pdDoc.Save PDSaveIncremental, FileNm1 'Save changes to the PDF document
If counter = 1 Then
' Do nothing except create the master file
FileCopy "C:\SVN\954_T4A\fillable\2016_T4A_Form.pdf", "C:\SVN\954_T4A\fillable\2016_T4A_Form_Merged.pdf"
Else
FileCopy "C:\SVN\954_T4A\fillable\2016_T4A_Form.pdf", "C:\SVN\954_T4A\fillable\2016_T4A_Form_Main.pdf"
' Combining
Call CombineDocs
End If
rst1.MovePrevious
counter = counter + 1
Loop
pdDoc.Close
End If
'Close the PDF; the True parameter prevents the Save As dialog from showing
avDoc.Close (True)
'Some cleaning
Set gApp = Nothing
Set avDoc = Nothing
Set pdDoc = Nothing
Set jso = Nothing
MsgBox "done"
Merge vba code
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:\SVN\954_T4A\fillable\2016_T4A_Form_merged.pdf")
Part2Document.Open ("C:\SVN\954_T4A\fillable\2016_T4A_Form_Main.pdf")
' Insert the pages of Part2 after the end of Part1
numPages = Part1Document.GetNumPages()
If Part1Document.InsertPages(Part1Document.GetNumPages - 1, Part2Document, 0, 1, True) = False Then
MsgBox "Cannot insert pages"
End If
If Part1Document.Save(PDSaveFull, "C:\SVN\954_T4A\fillable\2016_T4A_Form_merged.pdf") = False Then
MsgBox "Cannot save the modified document"
End If
'Part1Document.Save 1, "C:\SVN\954_T4A\fillable\2016_T4A_Form_merged.pdf"
'Part1Document.Save 1, "C:\SVN\954_T4A\fillable\MergedFile.pdf"
Part1Document.Close
Part2Document.Close
AcroApp.Exit
Set AcroApp = Nothing
Set Part1Document = Nothing
Set Part2Document = Nothing
Kill "C:\SVN\954_T4A\fillable\2016_T4A_Form_Main.pdf"
FileCopy "C:\SVN\954_T4A\fillable\2016_T4A_Form.pdf", "C:\SVN\954_T4A\fillable\2016_T4A_Form_Main.pdf"
What doesn't work is that when it loops though the set; it doesn't merge correctly. It keeps merging the same file over and over. For example if Jane Doe was the first record, it would fill out the fillable form correctly with her and then to fill the form with the other people as the recordset loops correctly. When it merges the final file later in the program execution it has the same name over and over.
In summary, the merged file has Jane Doe and Jane Doe.
I don't know why the merged copy doesn't have the right record in it and just the same name. The most recent fillable form has a different name so I am unclear why the final merge has the same record over and over
I have been tinkering with the insert fxn with no luck; any ideas?
This is my code:
Dim FileNm1, FileNm2, gApp, avDoc, pdDoc, jso
Dim rst1 As Recordset
Dim rst2 As Recordset
Dim strsql2
Dim strsql1
' sql omitted from post.
strsql1 = """
Dim x As Variant
Dim myStudentName
FileNm1 = "C:\SVN\954_T4A\fillable\2016_T4A_Form.pdf" 'File location
Set gApp = CreateObject("AcroExch.app")
Set avDoc = CreateObject("AcroExch.AVDoc")
Dim counter
counter = 1
If avDoc.Open(FileNm1, "") Then
Set pdDoc = avDoc.GetPDDoc()
Set jso = pdDoc.GetJSObject
Set rst1 = CurrentDb.OpenRecordset(strsql1)
rst1.MoveLast
Do While Not rst1.BOF
myStudentName = rst1(3) & " " & rst1(4) & " " & rst1(2)
Set x = jso.getField("student_name")
x.Value = myStudentName
pdDoc.Save PDSaveIncremental, FileNm1 'Save changes to the PDF document
If counter = 1 Then
' Do nothing except create the master file
FileCopy "C:\SVN\954_T4A\fillable\2016_T4A_Form.pdf", "C:\SVN\954_T4A\fillable\2016_T4A_Form_Merged.pdf"
Else
FileCopy "C:\SVN\954_T4A\fillable\2016_T4A_Form.pdf", "C:\SVN\954_T4A\fillable\2016_T4A_Form_Main.pdf"
' Combining
Call CombineDocs
End If
rst1.MovePrevious
counter = counter + 1
Loop
pdDoc.Close
End If
'Close the PDF; the True parameter prevents the Save As dialog from showing
avDoc.Close (True)
'Some cleaning
Set gApp = Nothing
Set avDoc = Nothing
Set pdDoc = Nothing
Set jso = Nothing
MsgBox "done"
Merge vba code
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:\SVN\954_T4A\fillable\2016_T4A_Form_merged.pdf")
Part2Document.Open ("C:\SVN\954_T4A\fillable\2016_T4A_Form_Main.pdf")
' Insert the pages of Part2 after the end of Part1
numPages = Part1Document.GetNumPages()
If Part1Document.InsertPages(Part1Document.GetNumPages - 1, Part2Document, 0, 1, True) = False Then
MsgBox "Cannot insert pages"
End If
If Part1Document.Save(PDSaveFull, "C:\SVN\954_T4A\fillable\2016_T4A_Form_merged.pdf") = False Then
MsgBox "Cannot save the modified document"
End If
'Part1Document.Save 1, "C:\SVN\954_T4A\fillable\2016_T4A_Form_merged.pdf"
'Part1Document.Save 1, "C:\SVN\954_T4A\fillable\MergedFile.pdf"
Part1Document.Close
Part2Document.Close
AcroApp.Exit
Set AcroApp = Nothing
Set Part1Document = Nothing
Set Part2Document = Nothing
Kill "C:\SVN\954_T4A\fillable\2016_T4A_Form_Main.pdf"
FileCopy "C:\SVN\954_T4A\fillable\2016_T4A_Form.pdf", "C:\SVN\954_T4A\fillable\2016_T4A_Form_Main.pdf"