Print to PDF VBA formatting issues

Tosborn

New Member
Joined
May 24, 2016
Messages
44
Hi all,

I have some VBA to print out a large document for all regional manager & their stores to one pdf file.

I have set the page setup as landscape and fit to one page for each page but it doesn’t work when the pdf is created. Each page comes out as portrait and not on the one page.
Any help is appreciated.

Code:
Sub PDFandEmail()

'To set page breaks on all worksheets, ignoring erros

Dim ws As Worksheet
Worksheets(1).Activate
    For Each ws In ThisWorkbook.Worksheets
    On Error Resume Next

        Dim a As Integer
        a = ActiveSheet.Index + 1
        If a > Sheets.Count Then a = 1
        Sheets(a).Activate
 
        ActiveWindow.View = xlPageBreakPreview
        ActiveSheet.ResetAllPageBreaks
        ActiveSheet.VPageBreaks(1).DragOff Direction:=xlToRight, RegionIndex:=1
            
            With ActiveSheet.PageSetup
            
                .Zoom = False
                .FitToPagesWide = 1
                .FitToPagesTall = 1
                .Orientation = xlLandscape
     
            End With

        
        
Next ws

'Go to the last sheet and set variables

Sheets("List").Select

    Dim sheetArray() As String
    Dim rcell As Range
    Dim i As Integer
    Dim wksAllSheets As Variant
    Dim wksSheet1 As Worksheet
    Dim strFilename As String, strFilepath As String
    Dim r3 As Range
    Dim x As Integer
    Dim z As Integer ' Number of Stores in Row
    Dim y As Variant
    Dim c As Variant
    Dim wks As Worksheet
    Dim lastCell As Long
    Dim oApp As Object
    Dim oMail As Object
    
   
        i = 0
        x = 5
            

'Select sheets for creation of PDFs
             
    For Each rcell In Range("e5:q5")
    
    z = Cells(3, x).Value
    
        If rcell.Value <> "" Then
        
             For Each c In Range(Cells(5, x), Cells(z, x)).Cells
                    ReDim Preserve sheetArray(0 To i)
                    sheetArray(i) = c.Value
                    i = i + 1
             Next c
             
            strFilepath = "G:\Finance\SunV5.3.1\Store P&L's\RMemails\"
            strFilename = rcell
       
            Sheets(sheetArray).Select

            'Export as pdf

            With ActiveSheet
            .ExportAsFixedFormat _
              Type:=xlTypePDF, _
              Filename:=strFilepath & strFilename, _
              Quality:=xlQualityStandard, _
              IncludeDocProperties:=True, _
              IgnorePrintAreas:=False, _
              OpenAfterPublish:=False
              
              End With
            
            'clear integers
            
            x = x + 1
            i = 0
            Set c = Nothing
            
                'Email P&L to RM
            
                        Set oApp = CreateObject("Outlook.Application")
                        Set oMail = oApp.CreateItem(0) 'olMailItem = 0
                            
                            With oMail
                            
                                'User input To property
                                .To = rcell
                                'User input CC property
                                .CC = "Tim osborn"
                                .Subject = "Region Store P&Ls"
                                'Hard code Body property
                                .Body = "Please find your region's store P&Ls for the prior period attached. Kind regards, Timothy Osborn."
                                'Set attachment
                                .Attachments.Add strFilepath & strFilename & ".pdf"
                                '.Send
                                'Display it
                                .Display
                                End With
                 
              End If
        
        ActiveWorkbook.Sheets(1).Select
        Sheets(Sheets.Count).Select
        
        'Application.Wait Now + #12:00:03 AM# - don't think we need this
        
    Next rcell
    
MsgBox "PDF Printing & Email Creation Complete"

End Sub

Thanks,
Tim
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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