VBA to copy sheets without changing the format - vba

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

Related

Converting workbook in PDF without blank pages

Hiii
I wrote a code to export sheets from one workbook to an other workbook and then convert it in pdf, but I have a lot of blank pages (maybe because of hidden formula or I don't know.
If you have any idea for what to add to my code in order to have a decent file it would be very appreciated.
Workbooks.Open FileName:="C:\Users\User\Documents\Tests Salome\dailypdf.xlsx"
Dim wbto2 As Workbook: Set wbto2 = Workbooks("dailypdf.xlsx")
wb.Activate
For Each sht In Sheets
If sht.Name <> "USD" And sht.Name <> "Balance" Then
Else
sht.Copy Before:=wbto2.Sheets(wbto2.Sheets.Count)
Rows("140:351").EntireRow.Delete '(I tried to delete the hidden rows)
End If
Debug.Print sht.Name
Next
wbto2.Activate
Application.DisplayAlerts = False
Sheets("Sheet1").Delete
Application.DisplayAlerts = True
FileName = Create_PDF(Source:=wbto2, _
FixedFilePathName:=iFile, _
OverwriteIfFileExist:=True, _
OpenPDFAfterPublish:=False)
The code functions but the result does not satisfy me because of the blank pages..
you can try any option below
1.Delete all unwanted rows before saving as PDF.
2.Set Print area
3.try to save excel range as PDF directly
'Enter Worksheet name, range Address, PDF file path and name
Sheets("Sheet Name").Range("A1:D50").ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
"C:\Temp\PDF_name.pdf", Quality:= _
xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, _
OpenAfterPublish:=True

Select Entire Sheets for Printing: Excel VBA

I created a VBA macro to export specific sheets of a workbook to a PDF. Simple enough. The problem that I am experiencing is that my code only selects part of Sheet4, so in my PDF part of the sheet is missing.
Sheet4 contains data in range A1:W80. However, when running the code below only the range A1:W75 is selected for printing. I have confirmed my PrintArea contains the entire page. Everything even looks great when printing.
I have searched endlessly for a solution, with no success. Could this be a page-layout setting issue? How can I ensure I am selecting the entire sheet, instead of only part of it, when exporting to PDF?
Here is my code:
Sub SaveReportPDF()
Dim filepath As String
filepath = "ABC"
ThisWorkbook.Sheets(Array("Sheet1", "Sheet2", "Sheet3", "Sheet4")).Select
Selection.ExportAsFixedFormat _
Type:=xlTypePDF, _
Filename:=filepath, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=False
End Sub
As good practise, you can set your used range of each sheet as the print area, and fit it to the page, using the following sub:
Sub ScaleForPrinting()
Dim sh As Worksheet
' Stop PrintCommunication for speed
Application.PrintCommunication = False
' Cycle through each sheet
For Each sh In ThisWorkbook.Sheets(Array("Sheet1", "Sheet2", "Sheet3", "Sheet4"))
' Set print area to used range of sheet
sh.PageSetup.PrintArea = sh.UsedRange
' Remove zoom, scale sheet to fit 1 page
With sh.PageSetup
.CenterHorizontally = True
.CenterVertically = True
.Zoom = False
.FitToPagesWide = 1
.FitToPagesTall = 1
End With
Next sh
' Enable PrintCommunication to apply settings
Application.PrintCommunication = True
End Sub
Then you will want to use the ActiveSheet object after selecting, rather than the Selection object. This is perhaps counter-intuitive, but you want to print the sheets not what you've selected in the sheets.
So:
ScaleForPrinting
ThisWorkbook.Sheets(Array("Sheet1", "Sheet2", "Sheet3", "Sheet4")).Select
ActiveSheet.ExportAsFixedFormat _
Type:=xlTypePDF, _
Filename:=filepath, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=False
' Deselect sheets to avoid nasty multiple sheet accidental editing!
ThisWorkbook.Sheets("Sheet1").Select

Saving a PDF automatically to a predetermined folder

I am writing a macro that automates a daily report that I send out each day. One of my last items is to write a script that saves the formatted excel sheet as a PDF (through a print to pdf option), and save it in a specific folder. I have the following written, however, it still prompts the user as to where to save it.
What would be a better way, or a way in general, to have it save automatically to a folder somewhere on my desktop?
Sub printToPDF()
Worksheets("general_report").PageSetup.CenterVertically = False
ActiveWindow.SelectedSheets.PrintOut Copies:=1, ActivePrinter:="Foxit Reader PDF Printer"
End Sub
This should do the work,
Sub printToPDF()
Dim FilePath As String
Dim FileName As String
FilePath = "C:\Users\userName\Desktop\" 'Change as per your username
ActiveSheet.Copy 'Copy a worksheet to a new workbook
'It saves .PDF file at your Descrop with the name of the worksheet
ActiveWorkbook.ExportAsFixedFormat Type:=xlTypePDF, FileName:=FilePath & ActiveSheet.Name, _
Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, _
OpenAfterPublish:=False
'Closing a newly created workbook without saving it
Application.DisplayAlerts = False
ActiveWorkbook.Close
End Sub

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

excel VBA PDF print eparating with horizontal page breaks

I have a worksheet that has 160 pages in it. One set of data could have 3 pages followed by a horizontal page breaks. I am trying to figure out how to make a separate PDF after each horizontal page break and name it as the string in cell A (it is the same name down) of that page break.
This is where I am at with the export to pdf, missing the above.
Sub Print_PDF()
Dim Awb As Workbook
Dim Snr As Integer
Dim ws As Worksheet
Set Awb = ActiveWorkbook
For Each ws In Awb.Sheets
If ws.Visible = xlSheetVisible Then
'Sheets(ws.Name).Copy
Awb.Sheets(ws.Name).Copy
'Sheets(ws.Name).Select
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, filename:= _
Awb.Path & "\" & Awb.Sheets(ws.Name).Name & ".pdf", _
Quality:=xlQualityStandard, IncludeDocProperties:=True, _
IgnorePrintAreas:=False, OpenAfterPublish:=False
ActiveWindow.Close False
End If
Next ws
End Sub
Are you saying you have a workbook with 160 worksheets in it? Or a workbook with 1 worksheet in it with 160 sets of data on the one sheet?