Why is my Excel not being exported to PDF in Landscape? - vba

Following Problem:
I have selected a range in excel and want to export it to a PDF. Given that it is quite wide and rather short, I want the pdf to be in landscape format. I've already tried this by setting the Page Layout --> Orientation to Landscape. But this does not work.
My code is as follows:
Sub Excel2PDF()
Dim BottomEquity As Range, BottomBond As Range, PDFArea As Range
Set SB = Worksheets("SalesBrokerage")
SB.Range("B2:N" & SB.Cells(SB.Rows.Count, 2).End(xlUp).Offset(1, 0).Row).Select
Selection.ExportAsFixedFormat Type:=xlTypePDF, Filename:="C:\CustomerPrice.pdf", _
Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, _
OpenAfterPublish:=True, Orientation:=Landscape
End Sub
Any suggestions?
Bonus question: Once I have set the landscape orientation (hopefully with your help), is there a way to ensure that the selected range fills the pdf page BestFit?

Related

VBA to copy sheets without changing the format

I have a question regarding my code that somehow changes the format (row height and column width) when I copy some sheets to another workbook - this is used as a middle step before printing to pdf. In the sheets "1", "2" and "3" the sheets are set up with a print area that only cover one A4 page, but when the code copies the selected sheets to a new temporary workbook all rows and columns have increased (increased pixels) so that the print area now covers several pages. Can anyone help?
Sub Print_to_pdf()
Application.ScreenUpdating = False
Set Output_Sheets = Sheets(Array("1", "2", "3"))
Output_Sheets.Select
Output_Sheets.Copy
ChDir "XXX"
ActiveWorkbook.ExportAsFixedFormat Type:=xlTypePDF, FileName:= _
"XXX\Print_to_pdf".pdf", Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
:=False, OpenAfterPublish:=False
ActiveWorkbook.Close savechanges:=False
Application.ScreenUpdating = True
End Sub
I still don't understand why you tried to copy those sheets. To export the sheets you could simply use this:
Sub Print_to_pdf()
Dim Output_Sheets As Sheets
Application.ScreenUpdating = False
Set Output_Sheets = Sheets(Array("1", "2", "3"))
Output_Sheets.Select
ActiveWorkbook.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
"c:\temp\Print_to_pdf.pdf", Quality:=xlQualityStandard, IncludeDocProperties:=True, _
IgnorePrintAreas:=False, OpenAfterPublish:=False
Application.ScreenUpdating = True
End Sub
If there are differences between the exported PDF file and any printers then this is probably due to differences in printer drivers and the PDF export engine. The only way I see to solve this playing around until it fits on both (printer and export).

Converting multiple sheet Excel file to PDF using VBA error

I am currently trying to convert a multiple sheet excel file to a PDF using VBA using the following code:
Private Sub CommandButton1_Click()
Dim mySheets As Variant, sh
mySheets = Array("Sheet1", "Sheet2", "Sheet3")
For Each sh In mySheets
Sheets(sh).PageSetup.Orientation = xlLandscape
Next
Sheets(mySheets).Select
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:="S:\GasInc\Services\B&ITS\OpsEng\EngServ\_Station Design\Projects\Station Co-ops\Angela Lian" & ".pdf", _
Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False
End Sub
It converts my file fine, however for what I have on sheet 2 it splits it up into multiple pages in the PDF because I guess it does not scale it to fit the page. I was wondering how I could modify the code to make it scale this sheet so it will fit on one page of the PDF.
Thanks!
I have a similar sub in one of the tools I use. Do you have your worksheet's width / height scale to fit properties set to 1 page?
For Each sh In mySheets
With Sheets(sh).PageSetup
.Orientation = xlLandscape
.Zoom = False
.FitToPagesWide = 1
.FitToPagesTall = 1
End With
Next sh
One of the weird things I've run into is scaling issues when converting to PDF with some chart / image objects. For instance a logo looks fine on screen but prints stretched on the pdf. I never fixed the problem, but through research it seemed due to the way the printer sees it compared to the screen resolution. Have you ever run into that problem?

VBA Exporting PDF from Excel in 2 copies

I'm looking for a solution to export couple of sheets from Excel to one file PDF. I've recorded a macro that creates nice PDF with all interesting me sheets. BUT i need 2 copies of one of the sheets in the same PDF, but I don't know how to do it.
Here is my code.
Sub ExportPDF()
Sheets(Array("PackingList", "Administracyjny", "Nadawca", "Odbiorca", "Przewoźnik")).Select
Sheets("PackingList").Activate
ChDir "C:\Users\XXXXXX\Desktop"
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
"C:\Users\XXXXXX\Desktop\Spools_PackingList.pdf", _
Quality:=xlQualityStandard, IncludeDocProperties:=True, _
IgnorePrintAreas:=False, OpenAfterPublish:= False
End Sub
See here for copying a worksheet, it might be a good workaround to copy the entire worksheet at the start of your code, and then you can just delete it at the end with (taken from here):
Sub sbDeleteASheet()
Sheet1.Delete
'OR You can mention the Sheet name
Sheets("Sheet2").Delete
End Sub

VBA ExportAsFixedFormat only saving last active chart

I'm trying to save a single worksheet from a workbook as a pdf using the ExportAsFixedFormat method:
Sheets("Overview").ExportAsFixedFormat Type:=xlTypePDF, _
Filename:=Mid(saveFile, 1, InStr(saveFile, ".")) & "pdf", _
Quality:=xlQualityStandard, IncludeDocProperties:=True, _
IgnorePrintAreas:=False, OpenAfterPublish:=False
This worked for a while until i started doing some chart manipulation beforehand. The manipulation I am talking about looks like this:
ActiveSheet.ChartObjects("Diagramm 4").Activate
ActiveChart.SetSourceData Source:=Sheets("Measurements").Range( _
"C4:C29,G4:G29")
Now its not exporting the whole sheet as a pdf but rather only the chart called "Diagramm 4".
I more or less understand why it's doing this but I can't find a way to fix this.
You could try selecting any cell on that sheet like:
Range("A1").Select
before you export the page. Likely this is happening because you're making the chart active without making it inactive again. Look what happens when you normally select a chart and then try to print a worksheet - it'll just try to print the chart.

Excel vba emf pasted to worksheet print/pdf wrong size

I'm taking a enhanced meta file generated from another application (DPlot Jr) copying to the clipboard, and pasting it to an Excel(2007) worksheet. From there i use vba to convert that file to a pdf. This is the code i have to do that:
' copy the graph eml file to the clip board
ret = DPlot_Command(doc, "[CopyPicture()]")
' copy the clip board contents to a new temp worksheet (under the covers)
'Hide the application
Application.ScreenUpdating = False
'Create a new temp worksheet
Set ws = ActiveWorkbook.Sheets.Add(After:=Sheets(1))
ws.Name = "Temp_Graph_DPJR"
ws.Activate
'Paste to the temp worksheet
ActiveSheet.Paste
'Save as pdf from excel
ActiveSheet.ExportAsFixedFormat _
Type:=xlTypePDF, _
filename:=pdf_filename, _
OpenAfterPublish:=False
I discovered that, the image that gets created in the PDF is slightly larger than the actual size of the graphic. For example, the width of the graphic should be 2.65". The size of the graph in the PDF is 2.816, so about .166" or 1 Pica, appears to be added.
While i could, and may have to, just decrease the size of the image initially by .166" that seems kind of hacky and I'd just like to have the image's original size to come over.
I discovered that if i paste the image to a Chartsheet, the size IS maintained, but the image becomes a bitmap on teh Chartsheet page.
When i create the pdf, i have all the correct settings. I have no margins, actual size, etc.
Has anyone else seen this? Can anyone help? I need to have the image as a pdf.
Thanks for any help!
Russ
Well, This is strange, and shouldn't make a difference, but it does, so far appear to work.
In the above workflow, after pasting the EMF to a worksheet, if i select that image THEN copy that to a new Chartsheet, the original size is maintained, as well as the vector nature of the graphic. Here is the code:
ret = DPlot_Command(doc, "[CopyPicture()]")
' copy the clip board contents to a new temp worksheet (under the covers)
'Hide the application
Application.ScreenUpdating = False
'Create a new temp worksheet
Set ws = ActiveWorkbook.Sheets.Add(After:=Sheets(1))
ws.Name = "Temp_Graph_DPJR"
ws.Activate
'Paste to the temp worksheet then select/copy that one
ActiveSheet.Paste
Selection.Copy
'Create a new temp chart
Set temp_chart = ActiveWorkbook.Charts.Add
'temp_chart.Name = "Temp_Chart"
'Set the linesytle of the border to none
temp_chart.ChartArea.Border.LineStyle = xlNone
'Paste the dplotjr graph to the chart sheet
'We had to do this as this maintained the size of the graph
'Dumb MS Excel worksheet
temp_chart.Paste
'Paste to the temp worksheet
'ActiveSheet.Paste
'Save as pdf from excel
ActiveSheet.ExportAsFixedFormat _
Type:=xlTypePDF, _
filename:=pdf_filename, _
OpenAfterPublish:=False
It is almost as though Excel determines that the paste is from another app and pastes a bitmap to the chartsheet, but if it determines that it is from itself, it pastes as a vector image. This maybe a loophole that might go away in the future, but it seems to work so far.
fwiw!