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

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
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
 
Upvote 0
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
 
Upvote 0
And what is the name of your worksheet you are printing from? You need to change SheetName to the correct name.

Edit: Spelling
 
Upvote 0

Forum statistics

Threads
1,215,007
Messages
6,122,670
Members
449,091
Latest member
peppernaut

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
Back
Top