Save custom range of pages to PDF with VBA

Started by mauno, April 30, 2015, 07:07:11 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

mauno

Hello,

I need to write a macro that selects all pages that has a certain shape on the page, and then saves these pages to a pdf-file.

Example:
On page 1-3, 4, 6 and 11-14 i have a shape with the name MAIN.
I want to select these pages, and export them to a single PDF-file.

I can figure out how to select these pages I think, but:
-->Is there a way to save/export a custom range, for example pages 1-3, 4, 6 and 11-14 to one PDF-file with VBA?

Also I think that I must have the code to save it to PDF, not print.


Br, Mauno
Br, mauno

Surrogate


mauno

Thank you for the tip. I will check if I can do what i want with with either of those.

But the best solution would be to only have to use visio and VBA for this. But i guess that is not possible.

Br, Mauno
Br, mauno

Surrogate

#3
Quote from: mauno on May 06, 2015, 05:43:46 AMBut i guess that is not possible.
don't worry! you can duplicate your file, deletу there unused pages and convert this edited document to pdf...
Sub ForMauno()
Dim fn As String ' variable for filenames
Dim bl As Boolean ' flag for deletion
Dim j As Integer ' counter for pages
Dim pag As Page ' current page
fn = ActiveDocument.FullName ' get active filename
fn = Replace(fn, ".vsd", "_tmp.vsd") ' set filename for copy file
ActiveDocument.SaveAs fn ' create copy of your file
fn = Replace(fn, "_tmp.vsd", ".pdf") ' set filename for pdf file
For j = ActiveDocument.Pages.Count To 1 Step -1 ' iterate all pages
Select Case j
Case 1 To 3, 4, 6, 11 To 14 ' set of desirable page numbers
bl = False ' set flag as false
Case Else
bl = True  ' set flag as true
End Select
If bl Then ActiveDocument.Pages(j).Delete 1 ' if current page have flag = true, this page deleted
Next
ActiveDocument.ExportAsFixedFormat visFixedFormatPDF, fn, visDocExIntentPrint, visPrintAll ' export copy file to pdf
End Sub

And aftter all this operations you just need delete copy file :)

mauno

Quote from: Surrogate on May 06, 2015, 07:51:30 AM

don't worry! you can duplicate your file, deletу there unused pages and convert this edited document to pdf...


Woah! Did not even come to think about that. Thank you!
I will check if I can use this solution, I will report back if I get it to work!

Br, Mauno
Br, mauno

Yacine

My first thought to exclude a page from being printed would have been to set it as background, then back again as foreground page.
... just a thought. ;)
Yacine

mauno

Quote from: Yacine on May 06, 2015, 04:34:53 PM
My first thought to exclude a page from being printed would have been to set it as background, then back again as foreground page.
... just a thought. ;)

Hello, and thank you for the tip!

That would be a less CPU-intensive and more robust solution (i think).
But when putting pages as background they are moved to the end of the document.

(I guess i could write code that saves the position of the page before making it background and then puts it back after the export to pdf. But that might get too complicated for me  :D )

Br, Mauno
Br, mauno

mauno

#7
Hello,

This is the first version of working code that I will use.
I have a shape with the name MAINASSY on some of the pages, these pages gets saved to a .pdf file.

It is very messy, but im new to VBA. (I will "clean" it before I put it to use)

Thank you Surrogate for your help! :)
And thanks Yacine for your tip. Will use that method if I figure out how to do it, and if the other method is too CPU-intensive.


Sub ForMauno()

    Dim fn As String            ' variable for filenames
    Dim fnForOpen As String     ' variable for filename to open after export
    Dim fnForClose As String    ' variable for filename to close after opening orifinal file
    Dim fnForDelete As String   ' variable for filename to delete when done
    Dim j As Integer            ' counter for pages

    fnForOpen = ActiveDocument.FullName ' get active filename and path
   
    fn = ActiveDocument.FullName ' get active filename and path
    fn = Replace(fn, ".vsd", "_tmp.vsd") ' set filename for copy file
    fnForDelete = fn ' save filename_tmp.vsd for deleting later
   
    fnForClose = ActiveDocument.Name ' get active filename
    fnForClose = Replace(fnForClose, ".vsd", "_tmp.vsd") ' set filename for copy file
   
    ActiveDocument.SaveAs fn ' create copy of your file
   
    fn = Replace(fn, "_tmp.vsd", ".pdf") ' set filename for pdf file
   
    For j = ActiveDocument.Pages.Count To 1 Step -1 ' iterate all pages
       
            On Error Resume Next
            Set MAINASSY = Application.ActiveDocument.Pages(j).Shapes.ItemU("MAINASSY") 'check if page has shape: MAINASSY
                   
            If MAINASSY Is Nothing Then 'Delete pages that does not have MAINASSY shape
                ActiveDocument.Pages(j).Delete 1
            Else:
            End If

        Set MAINASSY = Nothing 'Reset

    Next
   
    ActiveDocument.ExportAsFixedFormat visFixedFormatPDF, fn, visDocExIntentPrint, visPrintAll ' export copy file to pdf
   
    Debug.Print fnForDelete
   
    Application.ActiveDocument.Saved = True         'Disable save prompt
    Application.Documents.Open (fnForOpen)          'Open original .vsd file
    Application.Windows.ItemEx(fnForClose).Activate 'Activate temporary .vsd
    Application.ActiveDocument.Close                'Close temporary .vsd file
    Kill (fnForDelete)                              'Deletes temporary .vsd file
     
End Sub


;D
Br, mauno

AndyW

You could use something like PDFSharp (.Net library) to read your generated PDF and delete the pages you don't want.
Live life with an open mind