2

For generating a report, I have create pdf with bellow approach.

ActiveSheet.ExportAsFixedFormat _
    Type:=xlTypePDF, _
    fileName:=ThisWorkbook.path & "\rep.pdf", _
    Quality:=xlQualityStandard, _
    IncludeDocProperties:=True, _
    IgnorePrintAreas:=False, _
    OpenAfterPublish:=False

In the ActiveSheet, there were specified Print Area, Witch contains a Table, so table's column filtered value have change programmatically, and need:

I want new face of the print area that reforms by changing filtering criteria; get appends at the end of initial pdf file quietly, without creating a new pdf file, in a new page section.

How can I do that?

I have installed ADOBE Acrobat Professional on my system and able to add appropriate references in VBA references.

mgae2m
  • 1,134
  • 1
  • 14
  • 41
  • 1
    How would you do it if you had to do it manually instead of programmatically? – braX Sep 20 '17 at 19:17
  • I'm preparing a VBA based application for a Workbook, that needs perform acts programmatically. – mgae2m Sep 20 '17 at 19:42
  • I have Adobe Acrobat Professional on my system and please tell me witch references should be add in VBA references so contains related functions. – mgae2m Sep 21 '17 at 17:45
  • I'm not that familiar with Adobe, but with a quick search I found a screenshot here: https://stackoverflow.com/questions/37551957/using-vba-how-do-i-call-up-the-adobe-create-pdf-function – braX Sep 21 '17 at 18:29
  • I viewed above link before, but not succeed using in my need. Why no one answer my question in this therm? I need add my prepared print area for print in a new page section at the end of existed pdf file. – mgae2m Sep 21 '17 at 18:45
  • Have you tried looking on google? I found this: https://www.excelguru.ca/forums/showthread.php?4733-merging-multiple-PDF-files-into-a-single-PDF-file-via-VBA-macro – braX Sep 21 '17 at 19:13
  • Yes, I was viewed this and searched with google search engine. Above reference is discussed about merging two existed pdf file. Thus I need append my new face of print area, at the end of initial pdf in new page section. I dont want create multiple pdfs and merge them. – mgae2m Sep 21 '17 at 19:28
  • That may be your only option, achieving the same results. – braX Sep 21 '17 at 19:29
  • At [this](https://stackoverflow.com/questions/37551957/using-vba-how-do-i-call-up-the-adobe-create-pdf-function) old related valuable question, There where Adobe VBA references, and [developer guide](http://www.adobe.com/content/dam/Adobe/en/devnet/acrobat/pdfs/iac_developer_guide.pdf) for this matter. Are you agree with: There where solution for solve this issue and reach what needed? The option I needed and think there where solution for it, is that appending print area at the end of initial existed pdf file in new page section.Are you believe my wanted option is irrational or unreachable? – mgae2m Sep 21 '17 at 19:49
  • May be codes in http://khkonsulting.com/2009/03/adobe-acrobat-and-vba-an-introduction/ contains the methods you need. If the new face is to be new page in the PDF, I believe you need to create a PDF in Temp folder to merge them. Or perhaps move the face to new Worksheet to be exported. – PatricK Sep 28 '17 at 05:42
  • Right. Thanks for above link. Thus I thought about pdf format and ADOBE functions for creating pdf content. And may us edit or modify an existed pdf file and contents? (as explained in this question) – mgae2m Sep 28 '17 at 09:41

2 Answers2

3

You just need the "Acrobat" library.

One simple solution is to use the native ExportAsFixedFormat method to save each section as a separate PDF file first, e.g. "C:\temp\Part1.pdf" and "C:\temp\Part2.pdf"

Then use the InsertPages method in the Acrobat API as per example below:

Sub MergePDF()

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

Reference: http://www.khk.net/wordpress/2009/03/04/adobe-acrobat-and-vba-an-introduction/

Adobe Developer Guide: http://www.adobe.com/devnet/acrobat/pdfs/iac_developer_guide.pdf

Adobe API Reference: http://www.adobe.com/devnet/acrobat/pdfs/iac_api_reference.pdf

Michael
  • 4,563
  • 2
  • 11
  • 25
  • I want new face of the print area that reforms by change filtering criteria; get appends at the end of initial pdf file quietly, without creating a new pdf file, in a new page section. – mgae2m Sep 27 '17 at 13:34
  • Yes I did read your original question. I also read your comment to the first question which solved your problem but lacked Adobe references. Have you read the Adobe API? It doesn't have methods for modifying the content of Adobe files and doesn't have methods for creating PDF content from external applications. You'll need to combine the two answers and then delete the temp files through Windows API. – Michael Sep 28 '17 at 00:43
  • Of course. I throw this approach. Thus I think about ADOBE functions (procedures) for modifying ADOBE content and edit existed pdf file that witch is I was opened the bounty worth for that. – mgae2m Sep 28 '17 at 09:44
  • Which preferences should we add in VBA? – mgae2m Sep 30 '17 at 07:56
1

In the Solution, I provide there is no need of Acrobat Pro. We suppose that we have a table which named: Table2. We have also a help sheet(to store filtered tables) which is Hiden and named: Help.

Option Explicit

Sub print_to_pdf()
    Dim sh  As Long
    Dim rg  As Range
    Dim Rng As Range
    Dim rw  As Range

    Application.ScreenUpdating = False

    For Each rw In Range("Table2[#All]").Rows

        If rw.EntireRow.Hidden = False Then
            If Rng Is Nothing Then Set Rng = rw
            Set Rng = Union(rw, Rng)
        End If

    Next

    Rng.Copy

    With Sheets("help")
        .Visible = True
         sh = .Cells(Rows.Count, "A").End(xlUp).Row + 2
        Set rg = Range("a3" & ":" & "a" & sh - 2)

        .Activate
        .Cells(sh, "A").Select
        ActiveSheet.Paste

        ActiveSheet.PageSetup.PrintArea = rg

        ActiveSheet.ExportAsFixedFormat _
        Type:=xlTypePDF, _
        Filename:=ThisWorkbook.Path & "\rep.pdf", _
        Quality:=xlQualityStandard, _
        IncludeDocProperties:=True, _
        IgnorePrintAreas:=False, _
        OpenAfterPublish:=False

        .Visible = False
    End With

    Application.ScreenUpdating = True

    MsgBox "Your PDF Has been Created with Success!!", vbInformation
End Sub
Ryan Wildry
  • 5,612
  • 1
  • 15
  • 35
  • Regard. This answer is nice, valuable and smart, thus the mechanism and method of using Adobe procedures for editing an existed pdf file as append, remove or etc... are much valuable and low-range-finder. I suggest extending the perfect and prone accepted answer under [stackoverflow.com/questions...](https://stackoverflow.com/questions/37551957/using-vba-how-do-i-call-up-the-adobe-create-pdf-function) related question. – mgae2m Sep 24 '17 at 09:56
  • I admit your advice solved my problem. And I used your solution. Yet The power of using Adobe references procedures is a matter that I wait for. – mgae2m Sep 24 '17 at 10:17