Home » excel » Set Fit to Visible in Pdf from excel vba

Set Fit to Visible in Pdf from excel vba

Posted by: admin March 9, 2020 Leave a comment

Questions:

I am trying to export excel worksheets into pdf. Is there any way by which I can set the pdf property to fit to visible using VBA.
Thanks

Below is the code snippet which I am using

With wksSalesAndQuotaScoreCard
        '.PageSetup.LeftMargin=Application.InchesToPoints(0.7)
        .PageSetup.LeftMargin = Application.InchesToPoints(0.7)
        .PageSetup.RightMargin = Application.InchesToPoints(0.7)
        .PageSetup.TopMargin = Application.InchesToPoints(0.75)
        .PageSetup.BottomMargin = Application.InchesToPoints(0.75)
        .PageSetup.HeaderMargin = Application.InchesToPoints(0.3)

        .PageSetup.Orientation = xlLandscape
        .PageSetup.PrintTitleRows = "_SalesandQuotaScoreCardView"
        .PageSetup.CenterHorizontally = True
        .PageSetup.Order = xlDownThenOver
        .PageSetup.FitToPagesWide = 1
        ''.PageSetup.Zoom = 50
        Set rngSalesAndQuotaView = Range(.Shapes("_SalesandQuotaViewFrame").TopLeftCell.Offset(0, -1), .Shapes("_SalesandQuotaViewFrame").BottomRightCell.Offset(1, 0))
        rngSalesAndQuotaView.Select
    End With

    wksScoreCardPayoutView.Select
    wksSalesAndQuotaScoreCard.Select False
    Selection.ExportAsFixedFormat xlTypePDF, IncludeDocProperties:=True, openafterpublish:=True
    wksCustomizeScoreCard.Activate
How to&Answers:

Adobe has a set of codes you can use when opening a document:

http://partners.adobe.com/public/developer/en/acrobat/PDFOpenParameters.pdf

Be advised, these only work with the approved Adobe Reader and not all PDF readers will accept these commands.

You might have to have Acrobat to set the conditions manually per this answer on the Adobe Help Forums:

Regarding the OP: “How can I set the default to open all PDF’s at 100%
zoom magnification?”

With Acrobat Pro, run a Batch Process (Sequence)/Action to run the
“Open Options” sequence. This sequence permits setting the values for
all items under the Initial View tab of PDFs’ Document Properties.

Regarding a PDF’s Document Properties > Initial View: Visit an open
PDF’s Document Properties (Ctrl+D, Cmd+D). Go to the Initial View tab.
The top pane (Layout and Magnification) permits configuration of:

Navigation tab: Page Only | Bookmarks Panel and Page | Pages Panel and
Page | Attachments Panel and Page | Layers Panel and Page Page Layout:
Default | Single Page | Single Page Continuous | Two-Up (Facing) |
Two-Up (Cover Page) | Two-Up Continuous (Cover Page) Magnification:
Default | Actual Size | Fit Page | Fit Width | Fit Height | Fit
Visible | Magnification percentages (25% to 6400%)

Configuration settings other than “Default” dictate how the PDF is
viewed initially.

The settings for all values in all panes in the Initial View may be
established for one or many PDFs by use of a Batch Sequence/Action
(Acrobat Pro required).

This functionality is utilized in an enterprise environment where
large document collections are deployed in a LAN/WAN “eLibrary” to
provide a consistent initial view to end-users.

It also provides a content author a mechanism for establishing the
initial view for their deployed content (such as PDF document
collections distributed on optical storage media).

As to the typical default Initial View for a created PDF: When a PDF
is created via Acrobat the PDF document’s default Initial View for
Layout and Magnification is:
–| Navigation: Page Only
–| Page layout: Default
–| Magnification: Default
–| Open to page: 1

Regarding the Open Parameters document — While this document does
provide a few command line options the document’s primary focus is the
use of a URL to open a URL’s targeted PDF’s initially opening.

Worth noting is that the document is specific for URLs and not
applicable to UNC links (links with a LAN/WAN).

Answer:

If I have understood your request, you need your print area (because exporting to PDF it’s like printing) to adjusted both on width and height. You have the .PageSetup.FitToPagesWide = 1 statement, now you need also .FitToPagesTall = 1, so your code will look like:

    With wksSalesAndQuotaScoreCard
            '.PageSetup.LeftMargin=Application.InchesToPoints(0.7)
            .PageSetup.LeftMargin = Application.InchesToPoints(0.7)
            .PageSetup.RightMargin = Application.InchesToPoints(0.7)
            .PageSetup.TopMargin = Application.InchesToPoints(0.75)
            .PageSetup.BottomMargin = Application.InchesToPoints(0.75)
            .PageSetup.HeaderMargin = Application.InchesToPoints(0.3)

            .PageSetup.Orientation = xlLandscape
            .PageSetup.PrintTitleRows = "_SalesandQuotaScoreCardView"
            .PageSetup.CenterHorizontally = True
            .PageSetup.Order = xlDownThenOver
            .PageSetup.FitToPagesWide = 1
            .FitToPagesTall = 1
            ''.PageSetup.Zoom = 50
            Set rngSalesAndQuotaView = Range(.Shapes("_SalesandQuotaViewFrame").TopLeftCell.Offset(0, -1), .Shapes("_SalesandQuotaViewFrame").BottomRightCell.Offset(1, 0))
            rngSalesAndQuotaView.Select
    End With

If this is not the solution, please explain what is this fittovisible for you.

Answer:

“Zoom Level” is not a document property for PDF files rather it is an application property. At the most, what we can do, open the PDF file directly from the excel macro and set the desired zoom level using “SendKeys”.

Refer to the piece of code below which works for me:

Function PDFOpenFile(inPath As String, inPageNo As Long, inZoom As Variant)
 'This function opens the PDF document with the specified
 'Page number and zoom level
 'The Page Number and Zoom Level are simulated through SendKeys method
 '  ************ Important *******************
 ' When the macro runs, do not use the keyboard
 ' else the SenKeys may not function as desired

 ThisWorkbook.FollowHyperlink inPath, NewWindow:=True
 SendKeys ("^+N" & inPageNo & "~^" & inZoom), True
End Function

Sub MyDocument()
 ' After file name, the firt parameter is for page no, the second parameter    is for zoom level
 PDFOpenFile "D:\MyFile.pdf", 3, 3

 ' Zoom Level Details
 '0: Full Page
 '1: Zoom to 100%
 '2: Page Width
 '3: Fit visible
 '4: visible width / Reflow

 ' These zoom level are visible in acrobat reader. Go to "View > Zoom" and see the shortcut mentioned
 ' If the shortcuts are different for your case, you may modify the parameter passed as argument
End Sub

Answer:

I printed a sheet, using PDFCreator, with “no scaling” and with “fit sheet on one page.” The difference between the two was

.Zoom = 100

and

.Zoom = False
.FitToPagesWide = 1
.FitToPagesTall = 1

I think this going to be “printer” dependant though. When you simply Saveas PDF, you don’t get those options, but the results aren’t bad either.

Range("A1:H34").Select
Selection.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
"C:\myfile.pdf", Quality:= _
 xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, _
 OpenAfterPublish:=False

I think much depends on how you are creating your PDF.

Answer:

Print settings vary by computer because only the default printer defines what is the ‘print area’. This is the same in Excel, Word, etc.

So you won’t be able to define it for all computers, unless all the computers you are accessing the document on are set to the same default printer.

Answer:

I think this is as print area setting you need to first set in excel before exporting to PDF. Do this in page layout first or within the “With” block of your code above and then run the code that exports to PDF.