Set Fit to Visible in Pdf from excel vba - excel

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

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).

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.

"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

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.

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.

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.

Related

Frozen pane gets partially cropped when exporting to PDF

I'm exporting from an excel range with frozen pane at the top to a PDF file through VBA.
In my Page Layout, Width is set to 1 page, and Height is on Automatic.
For some reason though, the two first rows of my frozen pane get cropped in all pages of PDF but the first one.
First page:
Other pages:
Here is the code used to export (pretty straightforward):
lastRow = getLastRow(targetWksht) 'targetWksht is well defined elsewhere
lastCol = getLastCol(targetWksht)
With targetWksht.Range(targetWksht.Cells(1, 1), targetWksht.Cells(lastRow, lastCol))
.ExportAsFixedFormat Type:=xlTypePDF, _
fileName:=Application.ActiveWorkbook.Path & "\" & fileName, _ 'fileName is defined elsewhere
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=True, _
OpenAfterPublish:=True
End With
It might be something stupid, but do you know where it may come from and how to fix that?
Try $worksheet.PageSetup.FitToPagesTall = 9999 instead of automatic or set your margins with code.
I found where it came from.
The frozen pane was selected as page title, but for some reason not all rows in this title were supposed to be repeated in following pages.
Therefore, the title appeared complete in the first page, but only partial in other ones.
To solve the problem, if you encounter it, follow this:
In Excel, go to Page Layout > Page Setup > Print Titles. You will have Print Area, which is the title, and below, Rows to repeat at top.
Make sure that all rows from your title are selected. For some reason my default parameter started at 3 instead of 1, setting it at 1 solved the problem.
NB: It also works for columns at left.

Is there a way to rotate OLEobjects?

I have a macro that will append a page to the current document from a pre-formatted template. This page inserts a picture based off user selection, which they have the standard excel supported images(i.e. .jpg, .png, .bmp) as well as PDF's(taken off an AutoCAD program that can only save as PDF/DWG/DXF file types. The problem I'm having is that I cannot rotate landscape print formatted images the way that I can with the regular images. I know that the problem is likely that not all OLEobjects can be rotated and the shaperange option probably does not allow for rotation. That being said, is it possible to do this within VBA?
I have tried to use the OLEobject shaperange option, as well as inserting the PDF as a picture(which is not supported by MS-Office as far as I know). I have also tried to select the OLEobject as a shape to no avail.
Set rng = crrntWorkbook.Sheets(1).Range("B" & tempRow)
'Allows for the insertion of PDF files into the workbook
Set oleobj = ActiveSheet.OLEObjects.Add(Filename:=txtFileName, link:=False, DisplayAsIcon:=False)
With oleobj
'Inserts the picture into the correct cell
oleobj.Top = rng.Top
oleobj.Left = rng.Left
'If the image is wider than it's height, image will be scaled down by it's width, otherwise it's height
If oleobj.Width > oleobj.Height Then
oleobj.IncrementRotation = 90
oleobj.Width = 545
oleobj.Left = (570 - oleobj.Width) / 2
oleobj.Top = oleobj.Top + 2
'Centers the image
Else
oleobj.Height = 625
oleobj.Left = (550 - oleobj.Width) / 2
oleobj.Top = oleobj.Top + 2
End If
End With
The expected result is that the image will be rotated upon insertion, but I will get either a "runtime error '438' Object doesn't support this property or method" or a "runtime error -2147024809 the shape is locked and cannot be rotated if I use the shaperange approach"

Excel-IE Automation w/VBA on Dynamic Tables

Good Morning,
I’m hoping that some kind soul out there can help me with a roadblock I’ve encountered in my quest to manipulate a website with VBA. I am using MS Excel 2010 and Internet Explorer 11.0.56.
I’m somewhat comfortable with VBA but have never used it to navigate to a website, enter information and click on buttons. I’ve managed to muddle through as follows:
In Column A of my Excel spreadsheet, I have a list of 10 digit case numbers.
The code below will open IE, navigate to the desired website, pause while I log in, then navigate to the search screen, enter in the first case number and press the SEARCH button (yes, I have the case number in this example hard coded in with no looping, but that stuff I can handle so please ignore):
Sub Button_Click()
Dim objIE As Object
Set objIE = New InternetExplorerMedium
objIE.Top = 0
objIE.Left = 0
objIE.Width = 800
objIE.Height = 600
objIE.AddressBar = 0
objIE.StatusBar = 0
objIE.Toolbar = 0
objIE.Visible = True
objIE.Navigate ("https://somewebsite.com")
MsgBox ("Please log in and then press OK")
objIE.Navigate ("https://somewebsite.com/docs")
Do
DoEvents
Loop Until objIE.ReadyState = 4
objIE.Document.all("caseNumber").Value = "1234567890"
objIE.Document.getElementById("SearchButton").Click
Exit Sub
Do
DoEvents
Loop Until objIE.ReadyState = 4
MsgBox ("Done")
End Sub
That will bring me to this screen
The file number entered in the search field will return any number of files in a dynamic table with a checkbox to the left of each file.
For this example, let’s say I am ONLY concerned with the file called “CC8” under the “Type” column. There will only ever be one instance of “CC8” for a given file number.
What I need help with is, through VBA, how do I search through this table, find the “CC8” line, and then have the checkbox to the left automatically checked?
When I inspect the “CC8” element in IE, this is the HTML associated with it (highlighted in gray; the entire table is under class “listing list-view clearfix”)
see here
The HTML for the checkbox related to the “CC8” item is below:
HTML code here
The “id” for both has the same sequence of numbers, but one starts with “viewPages” and the other “doc”.
Can anyone help me out as to what I need to add to my code to get this checkbox checked? Thank you!
Note:
Please post the actual HTML using the snippet tool.
Generally:
Without HTML to properly test, I am assuming that the following 2 nodeLists are the same length, meaning that when the search text is found in aNodeList then the assumption is the same index can be used to target the corresponding checkbox in the bNodeList:
Dim aNodeList As Object, i As Long
With objIE.document
Set aNodeList = .querySelectorAll("a[target='_blank']")
Set bNodeList = .querySelectorAll("[title='Search Result: Checkbox']")
End With
For i = 0 To aNodeList.Length - 1
If aNodeList.item(i).innerText = "CC8" Then
bNodeList.item(i).Click
Exit For
End If
Next
You could also potentially use the following instead as you say the viewPages prefixes each item:
Set aNodeList = .querySelectorAll("a[id^='viewPages']")
Other observations:
Traditional checkboxes would have a checked attribute and syntax of
bNodeList.item(i).Checked = True, but as I can't see that attribute in your element I am assuming a .Click suffices.

How can I make PDF with Landscape orientation from an Excel file in Vba Access?

I used to make pdf file from Access forms using this code:
DoCmd.OutputTo acOutputForm,"FR_PFMEAPrint2", acFormatPDF,"PFMEAPrint2.PDF", True
and I printed Excelforms with Portrait orientation in Access using this code:
Excel_App.ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True
and for landscape I used this code in Access:
Forms("FR_Print").Printer.Orientation = acPRORLandscape
DoCmd.PrintOut acPages, 1, 1, , 1
but I need to output Excel file to PDF landscape format via Vba Access Thanks
You have to set the property of Excel Sheet object like shown in the sample code snippet below:
Sheets(1).PageSetup.Orientation = xlLandscape
Pertinent to your case it will look like:
Excel_App.ActiveWindow.SelectedSheets(1).PageSetup.Orientation = xlLandscape
or you can iterate through the multiple Sheets and set their property in the loop. Also, FYI: you can use underlying xlLandscape value of 2.
Hope this will help.

How to Add Icon to a Excel Menu/Toolbar Button

I need to add a image to a custom toolbar/menu item which is create through VBA.
For a toolbar item, I tried following code
Set NewBtn = TBar.Controls.Add(Type:=msoControlButton)
With NewBtn
.Picture = LoadPicture("mypic.bmp")
.OnAction = "'MyFunction""" & para1 & """'" //VBA Function
'.Caption = "MyFunction"
.TooltipText = "MyFunction"
.Style = msoButtonCaption
End With
In the above code LoadPicture() does not seem to be working. My toolbar is initializing at the workbook load up event. I noticed that the image is loading to the toolbar button, but in a fraction of second it disappears and only item text is displayed. My image is 16x16 pixel bmp one.
Any help appreciate to get around this problem
Thank you
Use MsoButtonStyle.msoButtonIcon or one of the MsoButtonStyle members that contain the word Icon.
In VBA I store the icons on a worksheet (oTemplate) and transfer them to the buttons using:
with NewBtn
oTemplate.Shapes("picCalcOpt").CopyPicture
.PasteFace

Resources