[VBA] - Need to add File Location and Print Page Setup in Extract PDF Sub

rsolanki

New Member
Joined
Jul 30, 2024
Messages
6
Office Version
  1. 365
  2. 2021
Platform
  1. Windows
I found this working Sub which exports Worksheet in PDF, however how can I change the Location of Export / File and How can i make sure that Page setup is All Row and Column is printed in Landscape and fits into one page?

VBA Code:
Sub Extract_PDF()

Dim wb As Workbook
Dim sh As Worksheet

Set wb = ThisWorkbook

For Each sh In wb.Worksheets

    sh.Select

    pdf_name = sh.Name & ".pdf"
    

    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, _
        Filename:=ActiveWorkbook.Path & pdf_name, _
        Quality:=xlQualityStandard, _
        IncludeDocProperties:=True, _
        IgnorePrintAreas:=False, _
        OpenAfterPublish:=True

Next

End Sub
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
Try the following...

VBA Code:
Sub Export_All_Worksheets_to_PDF()

    Dim wb As Workbook
    Dim ws As Worksheet
    Dim destFolderName As String
    Dim saveAsFileName As String
    
    destFolderName = "c:\users\john\desktop\" 'change the path accordingly
    
    If Right$(destFolderName, 1) <> "\" Then
        destFolderName = destFolderName & "\"
    End If
    
    Set wb = ThisWorkbook
    
    For Each ws In wb.Worksheets
    
        saveAsFileName = destFolderName & ws.Name & ".pdf"
        
        formatWorksheet ws
        
        exportSheetToPDF saveAsFileName, ws
    
    Next
    
End Sub


Private Sub formatWorksheet(ByVal ws As Worksheet)

    Application.PrintCommunication = False
    
    With ws.PageSetup
        .FitToPagesTall = 1
        .FitToPagesWide = 1
        .Orientation = xlLandscape
    End With
    
    Application.PrintCommunication = True
    
End Sub

Private Sub exportSheetToPDF(ByVal saveAsFileName As String, ByVal sh As Object)

        sh.ExportAsFixedFormat _
            Type:=xlTypePDF, _
            Filename:=saveAsFileName, _
            Quality:=xlQualityStandard, _
            IncludeDocProperties:=True, _
            IgnorePrintAreas:=False, _
            OpenAfterPublish:=True
    
End Sub

Hope this helps!
 
Upvote 0
Try the following...

VBA Code:
Sub Export_All_Worksheets_to_PDF()

    Dim wb As Workbook
    Dim ws As Worksheet
    Dim destFolderName As String
    Dim saveAsFileName As String
   
    destFolderName = "c:\users\john\desktop\" 'change the path accordingly
   
    If Right$(destFolderName, 1) <> "\" Then
        destFolderName = destFolderName & "\"
    End If
   
    Set wb = ThisWorkbook
   
    For Each ws In wb.Worksheets
   
        saveAsFileName = destFolderName & ws.Name & ".pdf"
       
        formatWorksheet ws
       
        exportSheetToPDF saveAsFileName, ws
   
    Next
   
End Sub


Private Sub formatWorksheet(ByVal ws As Worksheet)

    Application.PrintCommunication = False
   
    With ws.PageSetup
        .FitToPagesTall = 1
        .FitToPagesWide = 1
        .Orientation = xlLandscape
    End With
   
    Application.PrintCommunication = True
   
End Sub

Private Sub exportSheetToPDF(ByVal saveAsFileName As String, ByVal sh As Object)

        sh.ExportAsFixedFormat _
            Type:=xlTypePDF, _
            Filename:=saveAsFileName, _
            Quality:=xlQualityStandard, _
            IncludeDocProperties:=True, _
            IgnorePrintAreas:=False, _
            OpenAfterPublish:=True
   
End Sub

Hope this helps!
Hey Domenic, Thanks its working!! Yay!

Just wanted to know is it possible to Add Header in each PDF like Organization Name, Address and Logo?

Look forward to hear from you

Thanks in Advance
 
Upvote 0
Have you considered using the header section of the worksheet to add the address and logo, whether manually or via code?

VBA Code:
Ribbon >> View >> Workbook Views >> Page Layout >> Add header

Or, maybe, for more flexibility, simply amend you worksheets to include them?
 
Upvote 0

Forum statistics

Threads
1,224,813
Messages
6,181,117
Members
453,021
Latest member
Justyna P

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