VBA to loop excels in the directory and print to pdf. The pdf page setup should be one page

Bigpotato 668

New Member
Joined
Apr 18, 2021
Messages
2
Office Version
  1. 2016
Platform
  1. Windows
Hi all,

I use below code to loop all excel files in the directory and convert to pdf.

But the pdf output has page setup twisted.

How can I modify my coding to make my pdf output page setup as within one page?

Thank you all.

Public Sub Create_Invoice_PDFs()

Dim folderPath As String
Dim fileName As String
Dim invoiceSheet As Worksheet

folderPath = "C:\path\to\workbooks\" 'MODIFY THIS LINE - FOLDER CONTAINING THE WORKBOOKS

If Right(folderPath, 1) <> "\" Then folderPath = folderPath & "\"

Application.ScreenUpdating = False

fileName = Dir(folderPath & "*.xls*")
Do While fileName <> ""
Workbooks.Open folderPath & fileName
Set invoiceSheet = Nothing
On Error Resume Next
Set invoiceSheet = ActiveWorkbook.Worksheets("RCTI")
Err.Clear
If Not invoiceSheet Is Nothing Then
invoiceSheet.ExportAsFixedFormat Type:=xlTypePDF, fileName:=folderPath & Left(ActiveWorkbook.Name, InStrRev(ActiveWorkbook.Name, ".")) & "pdf", _
Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
Else
Debug.Print fileName & " doesn't contain a 'RCTI' sheet"
End If
ActiveWorkbook.Close False

fileName = Dir
Loop



Application.ScreenUpdating = True

End Sub
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.

severynm

Board Regular
Joined
Jan 8, 2021
Messages
136
Office Version
  1. 365
Platform
  1. Windows
Welcome!

See the answer to this same question posted here. If you want to do the equivalent of "File→Print→Print Sheet On One Page", the bare minimum you will want to add before printing is
VBA Code:
    SheetName.PageSetup.Zoom = False
    SheetName.PageSetup.FitToPagesWide = 1
    SheetName.PageSetup.FitToPagesTall = 1
 

Bigpotato 668

New Member
Joined
Apr 18, 2021
Messages
2
Office Version
  1. 2016
Platform
  1. Windows
thanks mate. Sorry for the late reply. I added below and try again, but seems not work...

Public Sub Create_Invoice_PDFs()

Dim folderPath As String
Dim fileName As String
Dim invoiceSheet As Worksheet

folderPath = "C:\Users\aacc143\Desktop\Monthly Commission Coding and Macro\Test" 'MODIFY THIS LINE - FOLDER CONTAINING THE WORKBOOKS

If Right(folderPath, 1) <> "\" Then folderPath = folderPath & "\"

Application.ScreenUpdating = False

fileName = Dir(folderPath & "*.xls*")
Do While fileName <> ""
Workbooks.Open folderPath & fileName
Set invoiceSheet = Nothing
On Error Resume Next
Set invoiceSheet = ActiveWorkbook.Worksheets("RCTI")
Err.Clear
If Not invoiceSheet Is Nothing Then
invoiceSheet.ExportAsFixedFormat Type:=xlTypePDF, fileName:=folderPath & Left(ActiveWorkbook.Name, InStrRev(ActiveWorkbook.Name, ".")) & "pdf", _
Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
Else
SheetName.PageSetup.Zoom = False
SheetName.PageSetup.FitToPagesWide = 1
SheetName.PageSetup.FitToPagesTall = 1

Debug.Print fileName & " doesn't contain a 'RCTI' sheet"
End If
ActiveWorkbook.Close False

fileName = Dir
Loop



Application.ScreenUpdating = True

End Sub
 

severynm

Board Regular
Joined
Jan 8, 2021
Messages
136
Office Version
  1. 365
Platform
  1. Windows
And what is the name of your worksheet you are printing from? You need to change SheetName to the correct name.

Edit: Spelling
 

Watch MrExcel Video

Forum statistics

Threads
1,133,625
Messages
5,659,941
Members
418,538
Latest member
alc51103

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top