troubleshooting Question

Editing an Acrobat .PDF file to add page footers from Access 2013

Avatar of rogerdjr
rogerdjrFlag for United States of America asked on
Microsoft AccessVisual Basic ClassicAdobe Acrobat
13 Comments1 Solution810 ViewsLast Modified:
I have this subroutine that is supposed to add a page footer on every page of a group of PDF files - it works fine except the footer only appears on the 1st page.

Hopefully its a simple fix to add the footer to every page

I'm using Access 2013 and Acrobat XI Professional

I'd appreciate any help I can get.

Thanks
 ----------------------------
Private Sub TestButton_Click()

    Dim TestFunction As String, PathNm As String, FileNm As String, DestPathNm As String, CountFiles As Integer, MaxCountFiles As Double, MaxPgsPerFile, PathTest As String
    Dim StrSql As String, dbs As Database, rst As Recordset, Rst1 As Recordset
    Dim DoYouWantToSendToPrinter As Integer
    Dim SourcePath As String, DestPath As String, NoFiles As Integer, NoPages As Integer, DoYouWantToInclPathInFtr As Integer
    Dim FontSize As Integer, FontColor As String
   
    MsgBox "Make Sure Printer is not 'OFF-Line'" & vbNewLine & vbNewLine & "Make a Process to add a leader or trailer page" & vbNewLine & vbNewLine & "Make it so it can print in XX (e.g. 50 pages) page batches to take advantage of the stapler" & vbNewLine & vbNewLine & "Put all these options in a table and have a drop down menu"
   
    STRUSERID = GetNetUserName()
   
    StrSql = "SELECT [0_UserSpecificData].UserId, [0_UserSpecificData].PrintPdfLastPathSource, [0_UserSpecificData].PrintPdfLastPathDest, "
    StrSql = StrSql & "[0_UserSpecificData].PrintPdfLastNoFiles, [0_UserSpecificData].PrintPdfLastNoPages FROM 0_UserSpecificData WHERE "
    StrSql = StrSql & "[0_UserSpecificData].UserId="
    StrSql = StrSql & """" & STRUSERID & """"
    StrSql = StrSql & ";"
   
    Set dbs = CurrentDb
   
    Set rst = dbs.OpenRecordset(StrSql)
   
    If Not rst.EOF Then
        If IsNull(rst![PrintPdfLastPathSource]) Then
            SourcePath = "c:\0\"
        Else
            SourcePath = rst![PrintPdfLastPathSource]
        End If
       
        If IsNull(rst![PrintPdfLastPathDest]) Then
            DestPath = "e:\0\"
        Else
            DestPath = rst![PrintPdfLastPathDest]
        End If
       
        If IsNull(rst![PrintPdfLastNoFiles]) Then
            NoFiles = 25
        Else
            NoFiles = rst![PrintPdfLastNoFiles]
        End If
       
        If IsNull(rst![PrintPdfLastNoPages]) Then
            NoPages = 20
        Else
            NoPages = rst![PrintPdfLastNoPages]
        End If
    Else
        SourcePath = "c:\0\"
        DestPath = "e:\0\"
        NoFiles = 25
        NoPages = 20
    End If
   
    DoYouWantToSendToPrinter = MsgBox("Do You Want to Print These Files (Yes) " & vbNewLine & "Or Just Add Footers and Save (No)", vbYesNoCancel)
   
    If DoYouWantToSendToPrinter = 2 Then Exit Sub
   
    PathNmChck = MsgBox("Path with Files to Process" & vbNewLine & SourcePath, vbYesNoCancel)
    If PathNmChck = 6 Then
        PathNm = SourcePath
    ElseIf PathNmChck = 2 Then
        Exit Sub
    Else
   
       Dim strMessage As String
       Dim startDirectory
       strMessage = "Select a directory"
       startDirectory = "My Computer" 'c:\program files"
       Dim objFF As Object
       Set objFF = CreateObject("Shell.Application").BrowseForFolder(0, strMessage, &H1, startDirectory)
       If Not objFF Is Nothing Then
        getdirectory = objFF.Items.Item.Path
           PathNm = objFF.Items.Item.Path
           MsgBox PathNm
       Else
        getdirectory = vbNullString
        MsgBox "No directory selected"
        Exit Sub
       End If
       Set objFF = Nothing
    End If
   
    If Right(PathNm, 1) = "\" Then
    Else
        PathNm = PathNm & "\"
    End If
   
    SourcePath = PathNm
   
    PathNmChck = MsgBox("Souce Path From " & SourcePath & vbneline & vbNewLine & "Destination Path Files Will be Process to" & vbNewLine & DestPath, vbYesNoCancel)
    If PathNmChck = 6 Then
        DestPathNm = DestPath
        PathTest = Dir(DestPath)
On Error Resume Next
        If Len(PathTest) = 0 Then MkDir DestPath
On Error GoTo 0
    ElseIf PathNmChck = 2 Then
        Exit Sub
    Else
   
       strMessage = "Select a directory"
       startDirectory = "My Computer" 'c:\program files"
       Set objFF = CreateObject("Shell.Application").BrowseForFolder(0, strMessage, &H1, startDirectory)
       If Not objFF Is Nothing Then
        getdirectory = objFF.Items.Item.Path
           DestPathNm = objFF.Items.Item.Path
           MsgBox DestPathNm
       Else
        getdirectory = vbNullString
        MsgBox "No directory selected"
        Exit Sub
       End If
       Set objFF = Nothing
    End If
   
    If Right(DestPathNm, 1) = "\" Then
    Else
        DestPathNm = DestPathNm & "\"
    End If
   
    CountFiles = 1
   
    MaxCountFiles = InputBox("How Many Files Do You Want to Print?", , NoFiles)
    MaxPgsPerFile = InputBox("How Many Pages Per File Do You Want to Print?", , NoPages)
    FontSize = InputBox("What Font Size Do You Want for the Footer?" & vbNewLine & vbNewLine & "Default is 8 Point", , 8) '11-11-2011
    FontColor = InputBox("What Font Color Do You Want for the Footer?" & vbNewLine & vbNewLine & "Default is Red", , "Red") '11-11-2011

    FileNm = Dir(PathNm & "*.pdf")
    While FileNm <> "" And CountFiles <= MaxCountFiles
        TestFunction = AddPageNumbers(PathNm, FileNm, DestPathNm, FileNm, 1, MaxPgsPerFile, CountFiles, DoYouWantToSendToPrinter, FontSize, FontColor) '07-25-2011, DoYouWantToInclPathInFtr)
       
        CountFiles = CountFiles + 1
        FileNm = Dir()
    Wend
   
    With rst
        .Edit
            ![PrintPdfLastPathSource] = PathNm
            ![PrintPdfLastPathDest] = DestPathNm
            ![PrintPdfLastNoFiles] = MaxCountFiles
            ![PrintPdfLastNoPages] = MaxPgsPerFile
        .Update
    End With
End Sub

Public Function AddPageNumbers(ByVal SourcePath As String, ByVal SourceFileName As String, ByVal DestPath As String, ByVal DestFileName As String, ByVal FrstPg As Double, ByVal MaxNoPgs As Double, ByVal FileNo As Double, ByVal DoYouWantToSendToPrinter As Integer, ByVal FontSize As Integer, ByVal FontColor As String) As String '07-25-2011, ByVal DoYouWantToInclPathInFtr As Integer) As String

    Dim ex1 As String
    Dim App As Object, AVDoc As Object, AcroPDDoc As Object, AForm As Object
    Dim Ret As Long
    Dim sString As String * 255
    Dim PdfPrint As String, numPages As Integer
       
    Dim sfile As String
    Dim sText As String
    Dim iFileNum As Integer
   
    Dim FileName As String '06-18-2011
   
    Set App = CreateObject("Acroexch.app")
    Set AVDoc = CreateObject("AcroExch.AVDoc")
    Set AForm = CreateObject("AFormAut.App") 'from AFormAPI
   
    If Right(SourcePath, 1) <> "\" Then SourcePath = SourcePath & "\"
    If Right(DestPath, 1) <> "\" Then DestPath = SourcePath & "\"
   
    'FileName = SourcePath & SourceFileName '06-18-2011 - doesn't like symbols in the path either ":" or "\" - need to research
    FileName = SourceFileName '06-18-2011
    booleanresult = AVDoc.Open(SourcePath & SourceFileName, "")
   
    If booleanresult = True Then
        App.Show
        Set AcroPDDoc = AVDoc.GetPDDoc
       
            ex1 = "  // Set Footer PageNo centered  " & vbLf & "  var Box2Width = 500  " & vbLf & "  for (var p = 0; p < this.numPages; p++)   " & vbLf & "   {   " & vbLf & "    var aRect = this.getPageBox(""Crop"",p);  " & vbLf & "    var TotWidth = aRect[2] - aRect[0]  " & vbLf & "     {  var bStart=(TotWidth/2)-(Box2Width/2)  " & vbLf & "         var bEnd=((TotWidth/2)+(Box2Width/2))  " & vbLf & "         var fp = this.addField(String(""xftPage""+p+1), ""text"", p, [bStart,30,bEnd,15]);   " & vbLf & "         fp.value = """
            ex1 = ex1 & FileName & " -- " & Month(Now) & "/" & Day(Now) & "/" & Year(Now) & " " & Hour(Now) & ":" & Minute(Now)
       
        ex1 = ex1 & " -- Page: "" + String(p+1)+ ""/"" + this.numPages;  " & vbLf & "         fp.textSize=" & FontSize & "; fp.textColor = " & FontColor & ";  fp.readonly = true;  " & vbLf & "         fp.alignment=""left"";  " & vbLf & "     }  " & vbLf & "   }  "

       
'this.addWatermarkFromText({
'cText: "Document content expires 7 days from " + util.printd("mmm dd, yyyy", new Date()),
'nTextAlign: app.constants.align.center,
'nHorizAlign: app.constants.align.center,
'nVertAlign: app.constants.align.center,
'nOpacity: 0.5
'});
''---The above script will display the print date and the expiry date is set for 7 days.
       
        AForm.Fields.ExecuteThisJavaScript ex1
    End If
   
   
    On Error Resume Next
   
    AcroPDDoc.Save 1, DestPath & DestFileName
   
    numPages = AcroPDDoc.GetNumPages()
   
'text file listing documents printed
        sfile = DestPath & "Files Printed " & Format$(Now, "YYMMDD") & ".txt"
        sText = IIf(FileNo < 10, "00", IIf(FileNo < 100, "0", "")) & FileNo & " --- " & "Printed " & MaxNoPgs & " of " & IIf(numPages < 10, "00", IIf(numPages < 100, "0", "")) & numPages & " --- " & SourcePath & SourceFileName & " --- " & DestPath & DestFileName
       
        If FileNo = 1 Then Kill DestPath & "Files Printed " & Format$(Now, "YYMMDD") & ".txt"
       
        iFileNum = FreeFile
        Open sfile For Append As iFileNum
        Write #iFileNum, sText
        Close #iFileNum
   
   
    If MaxNoPgs >= numPages Then
        MaxNoPgs = numPages
    End If
   
    If DoYouWantToSendToPrinter = 6 Then
        PdfPrint = PrintPdfFile(DestPath, DestFileName, FrstPg, MaxNoPgs)
        WaitForPrintingToFinish
    End If
   
    AcroPDDoc.Close
    AVDoc.Close (True)
    App.Exit
   
    Ret = FindWindowWild(0, "Adobe*")
    Call GetWindowText(Ret, sString, 255)
   
    Ret = FindWindow(vbNullString, sString)
   
    PostMessage Ret, WM_CLOSE, CLng(0), CLng(0)

    Set AcroPDDoc = Nothing
    Set AVDoc = Nothing
    Set App = Nothing
End Function


'03-01-2011 Superceded by Add Page Numbers     'AddFooterPgNo Sub AddPageNumbers02282011revised03012011()
'03-01-2011 Superceded by Add Page Numbers     'AddFooterPgNo.vbs
'03-01-2011 Superceded by Add Page Numbers     '-----------------------------------------------
'03-01-2011 Superceded by Add Page Numbers     Dim path As String, AcroAVDoc As Object, AcroPDDoc As Object, Path1 As String
'03-01-2011 Superceded by Add Page Numbers
'03-01-2011 Superceded by Add Page Numbers     path = "C:\1\test.pdf"
'03-01-2011 Superceded by Add Page Numbers     Path1 = "C:\1\test-edited.pdf"
'03-01-2011 Superceded by Add Page Numbers
'03-01-2011 Superceded by Add Page Numbers     Filename = "C:/1/This is a Test.pdf"
'03-01-2011 Superceded by Add Page Numbers
'03-01-2011 Superceded by Add Page Numbers     '~~> Establish an existing application object
'03-01-2011 Superceded by Add Page Numbers     On Error Resume Next
'03-01-2011 Superceded by Add Page Numbers     Set App = GetObject(, "Acroexch.app")
'03-01-2011 Superceded by Add Page Numbers
'03-01-2011 Superceded by Add Page Numbers     '~~> If not found then create new instance
'03-01-2011 Superceded by Add Page Numbers     If Err.Number <> 0 Then
'03-01-2011 Superceded by Add Page Numbers         MsgBox Err.Number
'03-01-2011 Superceded by Add Page Numbers         Set App = CreateObject("Acroexch.app")
'03-01-2011 Superceded by Add Page Numbers     End If
'03-01-2011 Superceded by Add Page Numbers     Err.Clear
'03-01-2011 Superceded by Add Page Numbers     On Error GoTo 0
'03-01-2011 Superceded by Add Page Numbers
'03-01-2011 Superceded by Add Page Numbers     App.Show
'03-01-2011 Superceded by Add Page Numbers     Set AVDoc = CreateObject("AcroExch.AVDoc")
'03-01-2011 Superceded by Add Page Numbers     Set AForm = CreateObject("AFormAut.App") 'from AFormAPI
'03-01-2011 Superceded by Add Page Numbers
'03-01-2011 Superceded by Add Page Numbers     booleanresult = AVDoc.Open(path, "")
'03-01-2011 Superceded by Add Page Numbers
'03-01-2011 Superceded by Add Page Numbers     If booleanresult = True Then
'03-01-2011 Superceded by Add Page Numbers         Set AcroAVDoc = App.GetActiveDoc
'03-01-2011 Superceded by Add Page Numbers         Set AcroPDDoc = AcroAVDoc.GetPDDoc
'03-01-2011 Superceded by Add Page Numbers       ex1 = "  // Set Footer PageNo centered  " & vbLf & "  var Box2Width = 500  " & vbLf & "  for (var p = 0; p < this.numPages; p++)   " & vbLf & "   {   " & vbLf & "    var aRect = this.getPageBox(""Crop"",p);  " & vbLf & "    var TotWidth = aRect[2] - aRect[0]  " & vbLf & "     {  var bStart=(TotWidth/2)-(Box2Width/2)  " & vbLf & "         var bEnd=((TotWidth/2)+(Box2Width/2))  " & vbLf & "         var fp = this.addField(String(""xftPage""+p+1), ""text"", p, [bStart,30,bEnd,15]);   " & vbLf & "         fp.value = """
'03-01-2011 Superceded by Add Page Numbers       ex1 = ex1 & Filename & " " & Month(Now) & "/" & Day(Now) & "/" & Year(Now) & " " & Hour(Now) & ":" & Minute(Now)
'03-01-2011 Superceded by Add Page Numbers       ex1 = ex1 & " -- Page: "" + String(p+1)+ ""/"" + this.numPages;  " & vbLf & "         fp.textSize=6; fp.textColor = color.red;  fp.readonly = true;  " & vbLf & "         fp.alignment=""left"";  " & vbLf & "     }  " & vbLf & "   }  "
'03-01-2011 Superceded by Add Page Numbers
'03-01-2011 Superceded by Add Page Numbers       '//Execute JS-Code
'03-01-2011 Superceded by Add Page Numbers        AForm.Fields.ExecuteThisJavaScript ex1
'03-01-2011 Superceded by Add Page Numbers     End If
'03-01-2011 Superceded by Add Page Numbers
'03-01-2011 Superceded by Add Page Numbers     AcroPDDoc.Save 1, Path1
'03-01-2011 Superceded by Add Page Numbers
'03-01-2011 Superceded by Add Page Numbers     AcroPDDoc.Close
'03-01-2011 Superceded by Add Page Numbers     AcroAVDoc.Close (True)
'03-01-2011 Superceded by Add Page Numbers     AVDoc.Close (True)
'03-01-2011 Superceded by Add Page Numbers     App.Exit
'03-01-2011 Superceded by Add Page Numbers
'03-01-2011 Superceded by Add Page Numbers     Set AcroPDDoc = Nothing
'03-01-2011 Superceded by Add Page Numbers     Set AcroAVDoc = Nothing
'03-01-2011 Superceded by Add Page Numbers     Set AVDoc = Nothing
'03-01-2011 Superceded by Add Page Numbers     Set App = Nothing
'03-01-2011 Superceded by Add Page Numbers End Sub
'03-01-2011 Superceded by Add Page Numbers
'03-01-2011 Superceded by Add Page Numbers Sub AddPageNumbers1()
'03-01-2011 Superceded by Add Page Numbers     'AddFooterPgNo.vbs
'03-01-2011 Superceded by Add Page Numbers     '-----------------------------------------------
'03-01-2011 Superceded by Add Page Numbers     Dim path As String, AcroAVDoc As Object, AcroPDDoc As Object, Path1 As String
'03-01-2011 Superceded by Add Page Numbers
'03-01-2011 Superceded by Add Page Numbers     path = "C:\1\test.pdf"
'03-01-2011 Superceded by Add Page Numbers     Path1 = "C:\1\test-edited.pdf"
'03-01-2011 Superceded by Add Page Numbers
'03-01-2011 Superceded by Add Page Numbers     Dim Part1Document As Acrobat.CAcroPDDoc
'03-01-2011 Superceded by Add Page Numbers     Set Part1Document = CreateObject("AcroExch.PDDoc")
'03-01-2011 Superceded by Add Page Numbers     Part1Document.Open (PdfPathFile)
'03-01-2011 Superceded by Add Page Numbers
'03-01-2011 Superceded by Add Page Numbers 'note - Java script doesn't like \ so need to work through the path string and replace \ with / before running java script
'03-01-2011 Superceded by Add Page Numbers     Filename = "C:/1/This is a Test.pdf"
'03-01-2011 Superceded by Add Page Numbers     Set App = CreateObject("Acroexch.app")
'03-01-2011 Superceded by Add Page Numbers     App.Show
'03-01-2011 Superceded by Add Page Numbers     Set AVDoc = CreateObject("AcroExch.AVDoc")
'03-01-2011 Superceded by Add Page Numbers     Set AForm = CreateObject("AFormAut.App") 'from AFormAPI
'03-01-2011 Superceded by Add Page Numbers
'03-01-2011 Superceded by Add Page Numbers     booleanresult = AVDoc.Open(path, "")
'03-01-2011 Superceded by Add Page Numbers
'03-01-2011 Superceded by Add Page Numbers     If booleanresult = True Then
'03-01-2011 Superceded by Add Page Numbers         Set AcroAVDoc = App.GetActiveDoc
'03-01-2011 Superceded by Add Page Numbers '02-25-2011        Set AcroAVDoc = AcroApp.GetActiveDoc
'03-01-2011 Superceded by Add Page Numbers         Set AcroPDDoc = AcroAVDoc.GetPDDoc
'03-01-2011 Superceded by Add Page Numbers '02-22-2011    If avdoc.Open(path, "") Then
'03-01-2011 Superceded by Add Page Numbers       '//write JS-Code on a variable'
'03-01-2011 Superceded by Add Page Numbers 'note - Java script doesn't like \ so need to work through the path string and replace \ with / before running java script
'03-01-2011 Superceded by Add Page Numbers       ex1 = "  // Set Footer PageNo centered  " & vbLf & "  var Box2Width = 500  " & vbLf & "  for (var p = 0; p < this.numPages; p++)   " & vbLf & "   {   " & vbLf & "    var aRect = this.getPageBox(""Crop"",p);  " & vbLf & "    var TotWidth = aRect[2] - aRect[0]  " & vbLf & "     {  var bStart=(TotWidth/2)-(Box2Width/2)  " & vbLf & "         var bEnd=((TotWidth/2)+(Box2Width/2))  " & vbLf & "         var fp = this.addField(String(""xftPage""+p+1), ""text"", p, [bStart,30,bEnd,15]);   " & vbLf & "         fp.value = """
'03-01-2011 Superceded by Add Page Numbers       ex1 = ex1 & Filename & " " & Month(Now) & "/" & Day(Now) & "/" & Year(Now) & " " & Hour(Now) & ":" & Minute(Now)
'03-01-2011 Superceded by Add Page Numbers '      ex1 = ex1 & FileName
'03-01-2011 Superceded by Add Page Numbers       ex1 = ex1 & " -- Page: "" + String(p+1)+ ""/"" + this.numPages;  " & vbLf & "         fp.textSize=6; fp.textColor = color.red;  fp.readonly = true;  " & vbLf & "         fp.alignment=""left"";  " & vbLf & "     }  " & vbLf & "   }  "
'03-01-2011 Superceded by Add Page Numbers
'03-01-2011 Superceded by Add Page Numbers       ex = "  // Set Footer PageNo centered  " & vbLf _
'03-01-2011 Superceded by Add Page Numbers       & "  var Box2Width = 50  " & vbLf _
'03-01-2011 Superceded by Add Page Numbers       & "  for (var p = 0; p < this.numPages; p++)   " & vbLf _
'03-01-2011 Superceded by Add Page Numbers       & "   {   " & vbLf _
'03-01-2011 Superceded by Add Page Numbers       & "    var aRect = this.getPageBox(""Crop"",p);  " & vbLf _
'03-01-2011 Superceded by Add Page Numbers       & "    var TotWidth = aRect[2] - aRect[0]  " & vbLf _
'03-01-2011 Superceded by Add Page Numbers       & "     {  var bStart=(TotWidth/2)-(Box2Width/2)  " & vbLf _
'03-01-2011 Superceded by Add Page Numbers       & "         var bEnd=((TotWidth/2)+(Box2Width/2))  " & vbLf _
'03-01-2011 Superceded by Add Page Numbers       & "         var fp = this.addField(String(""xftPage""+p+1), ""text"", p, [bStart,30,bEnd,15]);   " & vbLf _
'03-01-2011 Superceded by Add Page Numbers       & "         fp.value = ""Page: "" + String(p+1)+ ""/"" + this.numPages;  " & vbLf _
'03-01-2011 Superceded by Add Page Numbers       & "         fp.textSize=6;  fp.readonly = true;  " & vbLf _
'03-01-2011 Superceded by Add Page Numbers       & "         fp.alignment=""center"";  " & vbLf _
'03-01-2011 Superceded by Add Page Numbers       & "     }  " & vbLf _
'03-01-2011 Superceded by Add Page Numbers       & "   }  "
'03-01-2011 Superceded by Add Page Numbers
'03-01-2011 Superceded by Add Page Numbers 'MsgBox ex & vbNewLine & vbNewLine & ex1
'03-01-2011 Superceded by Add Page Numbers       '//Execute JS-Code
'03-01-2011 Superceded by Add Page Numbers        AForm.Fields.ExecuteThisJavaScript ex1
'03-01-2011 Superceded by Add Page Numbers     End If
'03-01-2011 Superceded by Add Page Numbers
'03-01-2011 Superceded by Add Page Numbers '    AcroPDDoc.Save 1, path
'03-01-2011 Superceded by Add Page Numbers '
'03-01-2011 Superceded by Add Page Numbers '    AcroPDDoc.Close
'03-01-2011 Superceded by Add Page Numbers '
'03-01-2011 Superceded by Add Page Numbers '    Set AcroPDDoc = Nothing
'03-01-2011 Superceded by Add Page Numbers '    Set AcroAVDoc = Nothing
'03-01-2011 Superceded by Add Page Numbers '    Set AVDoc = Nothing
'03-01-2011 Superceded by Add Page Numbers '    Set App = Nothing
'03-01-2011 Superceded by Add Page Numbers
'03-01-2011 Superceded by Add Page Numbers
'03-01-2011 Superceded by Add Page Numbers     AcroPDDoc.Save 1, Path1
'03-01-2011 Superceded by Add Page Numbers
'03-01-2011 Superceded by Add Page Numbers     AcroPDDoc.Close
'03-01-2011 Superceded by Add Page Numbers     AcroAVDoc.Close (True)
'03-01-2011 Superceded by Add Page Numbers     AVDoc.Close (True)
'03-01-2011 Superceded by Add Page Numbers     App.Exit
'03-01-2011 Superceded by Add Page Numbers
'03-01-2011 Superceded by Add Page Numbers     Set AcroPDDoc = Nothing
'03-01-2011 Superceded by Add Page Numbers     Set AcroAVDoc = Nothing
'03-01-2011 Superceded by Add Page Numbers     Set AVDoc = Nothing
'03-01-2011 Superceded by Add Page Numbers     Set App = Nothing
'03-01-2011 Superceded by Add Page Numbers End Sub
'03-01-2011 Superceded by Add Page Numbers
'03-01-2011 Superceded by Add Page Numbers Sub AddPageNumbers02232011()
'03-01-2011 Superceded by Add Page Numbers     'AddFooterPgNo.vbs
'03-01-2011 Superceded by Add Page Numbers     '-----------------------------------------------
'03-01-2011 Superceded by Add Page Numbers     Dim path As String, PDDoc
'03-01-2011 Superceded by Add Page Numbers
'03-01-2011 Superceded by Add Page Numbers     path = "C:\1\test.pdf"
'03-01-2011 Superceded by Add Page Numbers
'03-01-2011 Superceded by Add Page Numbers     Set App = CreateObject("Acroexch.app")
'03-01-2011 Superceded by Add Page Numbers     App.Show
'03-01-2011 Superceded by Add Page Numbers     Set AVDoc = CreateObject("AcroExch.AVDoc")
'03-01-2011 Superceded by Add Page Numbers     Set AForm = CreateObject("AFormAut.App") 'from AFormAPI
'03-01-2011 Superceded by Add Page Numbers
'03-01-2011 Superceded by Add Page Numbers     booleanresult = AVDoc.Open(path, "")
'03-01-2011 Superceded by Add Page Numbers     If booleanresult = True Then
'03-01-2011 Superceded by Add Page Numbers         PDDoc = AVDoc.GetPDDoc
'03-01-2011 Superceded by Add Page Numbers '02-22-2011    If avdoc.Open(path, "") Then
'03-01-2011 Superceded by Add Page Numbers       '//write JS-Code on a variable'
'03-01-2011 Superceded by Add Page Numbers       ex = "  // Set Footer PageNo centered  " & vbLf _
'03-01-2011 Superceded by Add Page Numbers       & "  var Box2Width = 50  " & vbLf _
'03-01-2011 Superceded by Add Page Numbers       & "  for (var p = 0; p < this.numPages; p++)   " & vbLf _
'03-01-2011 Superceded by Add Page Numbers       & "   {   " & vbLf _
'03-01-2011 Superceded by Add Page Numbers       & "    var aRect = this.getPageBox(""Crop"",p);  " & vbLf _
'03-01-2011 Superceded by Add Page Numbers       & "    var TotWidth = aRect[2] - aRect[0]  " & vbLf _
'03-01-2011 Superceded by Add Page Numbers       & "     {  var bStart=(TotWidth/2)-(Box2Width/2)  " & vbLf _
'03-01-2011 Superceded by Add Page Numbers       & "         var bEnd=((TotWidth/2)+(Box2Width/2))  " & vbLf _
'03-01-2011 Superceded by Add Page Numbers       & "         var fp = this.addField(String(""xftPage""+p+1), ""text"", p, [bStart,30,bEnd,15]);   " & vbLf _
'03-01-2011 Superceded by Add Page Numbers       & "         fp.value = ""Page: "" + String(p+1)+ ""/"" + this.numPages;  " & vbLf _
'03-01-2011 Superceded by Add Page Numbers       & "         fp.textSize=6;  fp.readonly = true;  " & vbLf _
'03-01-2011 Superceded by Add Page Numbers       & "         fp.alignment=""center"";  " & vbLf _
'03-01-2011 Superceded by Add Page Numbers       & "     }  " & vbLf _
'03-01-2011 Superceded by Add Page Numbers       & "   }  "
'03-01-2011 Superceded by Add Page Numbers       '//Execute JS-Code
'03-01-2011 Superceded by Add Page Numbers        AForm.Fields.ExecuteThisJavaScript ex
'03-01-2011 Superceded by Add Page Numbers     End If
'03-01-2011 Superceded by Add Page Numbers
'03-01-2011 Superceded by Add Page Numbers     PDDoc.Save 1, path
'03-01-2011 Superceded by Add Page Numbers     PDDoc.Close
'03-01-2011 Superceded by Add Page Numbers
'03-01-2011 Superceded by Add Page Numbers     Set PDDoc = Nothing
'03-01-2011 Superceded by Add Page Numbers     Set AVDoc = Nothing
'03-01-2011 Superceded by Add Page Numbers     Set App = Nothing
'03-01-2011 Superceded by Add Page Numbers End Sub
'03-01-2011 Superceded by Add Page Numbers
'03-01-2011 Superceded by Add Page Numbers Sub AddPageNumbersRogerTryingToFigureOut()
'03-01-2011 Superceded by Add Page Numbers
'03-01-2011 Superceded by Add Page Numbers             Dim AcroApp As Acrobat.CAcroApp
'03-01-2011 Superceded by Add Page Numbers
'03-01-2011 Superceded by Add Page Numbers             Dim Part1Document As Acrobat.CAcroPDDoc
'03-01-2011 Superceded by Add Page Numbers             Dim Part2Document As Acrobat.CAcroPDDoc
'03-01-2011 Superceded by Add Page Numbers
'03-01-2011 Superceded by Add Page Numbers             Set AcroApp = CreateObject("AcroExch.App")
'03-01-2011 Superceded by Add Page Numbers
'03-01-2011 Superceded by Add Page Numbers             Set Part1Document = CreateObject("AcroExch.PDDoc")
'03-01-2011 Superceded by Add Page Numbers             Set Part2Document = CreateObject("AcroExch.PDDoc")
'03-01-2011 Superceded by Add Page Numbers
'03-01-2011 Superceded by Add Page Numbers             PdfPathFile = "C:\1\test.pdf"
'03-01-2011 Superceded by Add Page Numbers             Part1Document.Open (PdfPathFile)
'03-01-2011 Superceded by Add Page Numbers             Part1Document.OpenAVDoc (PdfPathFile)
'03-01-2011 Superceded by Add Page Numbers
'03-01-2011 Superceded by Add Page Numbers 'MsgBox "Test - " & AcroApp.GetActiveDoc
'03-01-2011 Superceded by Add Page Numbers            Set currdoc = AcroApp.GetActiveDoc
'03-01-2011 Superceded by Add Page Numbers '            Set CurrDoc = Part1Document.OpenAVDoc(PdfPathFile)
'03-01-2011 Superceded by Add Page Numbers
'03-01-2011 Superceded by Add Page Numbers
'03-01-2011 Superceded by Add Page Numbers       ex = "  // Set Footer PageNo centered  " & vbLf _
'03-01-2011 Superceded by Add Page Numbers       & "  var Box2Width = 50  " & vbLf _
'03-01-2011 Superceded by Add Page Numbers       & "  for (var p = 0; p < this.numPages; p++)   " & vbLf _
'03-01-2011 Superceded by Add Page Numbers       & "   {   " & vbLf _
'03-01-2011 Superceded by Add Page Numbers       & "    var aRect = this.getPageBox(""Crop"",p);  " & vbLf _
'03-01-2011 Superceded by Add Page Numbers       & "    var TotWidth = aRect[2] - aRect[0]  " & vbLf _
'03-01-2011 Superceded by Add Page Numbers       & "     {  var bStart=(TotWidth/2)-(Box2Width/2)  " & vbLf _
'03-01-2011 Superceded by Add Page Numbers       & "         var bEnd=((TotWidth/2)+(Box2Width/2))  " & vbLf _
'03-01-2011 Superceded by Add Page Numbers       & "         var fp = this.addField(String(""xftPage""+p+1), ""text"", p, [bStart,30,bEnd,15]);   " & vbLf _
'03-01-2011 Superceded by Add Page Numbers       & "         fp.value = ""Page: "" + String(p+1)+ ""/"" + this.numPages;  " & vbLf _
'03-01-2011 Superceded by Add Page Numbers       & "         fp.textSize=6;  fp.readonly = true;  " & vbLf _
'03-01-2011 Superceded by Add Page Numbers       & "         fp.alignment=""center"";  " & vbLf _
'03-01-2011 Superceded by Add Page Numbers       & "     }  " & vbLf _
'03-01-2011 Superceded by Add Page Numbers       & "   }  "
'03-01-2011 Superceded by Add Page Numbers       '//Execute JS-Code
'03-01-2011 Superceded by Add Page Numbers        AForm.Fields.ExecuteThisJavaScript ex
'03-01-2011 Superceded by Add Page Numbers
'03-01-2011 Superceded by Add Page Numbers
'03-01-2011 Superceded by Add Page Numbers End Sub
'03-01-2011 Superceded by Add Page Numbers
'03-01-2011 Superceded by Add Page Numbers






 
Public Function PrintPdfFile(ByVal PdfPath As String, ByVal PdfFileNm As String, ByVal PdfFirstPageToPrint As Integer, ByVal PdfMaxPagesToPrint As Integer) As String
    Dim PdfPathAndFile As String
    Dim AVDoc As Acrobat.AcroAVDoc
    Dim retcd As Integer
   
    If Right(PdfPath, 1) = "\" Then
        PdfPathAndFile = PdfPath & PdfFileNm
    Else
        PdfPathAndFile = PdfPath & "\" & PdfFileNm
    End If
   
    Set AVDoc = CreateObject("AcroExch.AVDoc")
   
    retcd = AVDoc.Open(PdfPathAndFile, "Title")    ' pathname of pdf file to be opened
   
    If retcd <> -1 Then
'        MsgBox ("Failed to open PDF document " & PdfPathAndFile)
        Exit Function
    End If

    'AVDoc.PrintPages(firstPage, numPages, 2, true, true)
    If PdfMaxPagesToPrint >= 1 Then PdfMaxPagesToPrint = PdfMaxPagesToPrint - 1  'allows for the fact that the first page is page 0
    If PdfFirstPageToPrint >= 1 Then PdfFirstPageToPrint = PdfFirstPageToPrint - 1 'allows for the fact that the first page is page 0
   
    'PrintPagesSilent(nFirstPage As Long, nLastPage As Long, nPSLevel As Long, bBinaryOk As Long, bShrinkToFit As Long) As Boolean
        'Params 1 & 2 are: Print from page __ to page __
        'Param 3: Must be either 1 or 2. 1 generates Level 1 PostScript code. 2 generates Level 2 PostScript code. (You'll have to test in your own environment, but fortunately the list is quite short.)
        'Param 4: True, indicates the PostScript code may contain binary data. False, indicates binary data is encoded into an ASCII format.
        'Param 5: True, shrink (if necessary). False, print actual size; pages may appear clipped.

    retcd = AVDoc.PrintPagesSilent(PdfFirstPageToPrint, PdfMaxPagesToPrint, 0, 1, True)         ' this prints the number of pages specified by PdfMaxPagesToPrint (i.e. page 0)
    '11-23-2011 retcd = AVDoc.PrintPagesSilent(PdfFirstPageToPrint, PdfMaxPagesToPrint, 0, 1, 1)         ' this prints the number of pages specified by PdfMaxPagesToPrint (i.e. page 0)
    'retcd = AVDoc.PrintPages(PdfFirstPageToPrint, PdfMaxPagesToPrint, 0, 1, 1)         ' this prints the number of pages specified by PdfMaxPagesToPrint (i.e. page 0)
    'avDoc.PrintPagesSilent(0, (numPages - 1), PSLevel, BinaryOk, ShrinkToFit);


    AVDoc.Close (1)

End Function
ASKER CERTIFIED SOLUTION
Join our community to see this answer!
Unlock 1 Answer and 13 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 13 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros