Printing to PDF use VBA - Was working but now different sheets are different widths

Tanquen

Board Regular
Joined
Dec 14, 2017
Messages
78
Office Version
  1. 365
Platform
  1. Windows
I'm using VBA script to print 4 sheets from the same workbook to a PDF.

It was working until yesterday, now different sheets are different widths in the PDF. I even went back to an older backup of the Workbook that had made the PDF correctly and it now has the same issue. ???
I tried to remove the sheet reordering, the script was doing and now there are three different widths. ???
1727875935522.png
1727876035759.png


If I manual highlight the sheet tabs and then print to PDF they are the correct width again. Shown in the second pic above.

This is the code now with most everything commented out so it's just selecting the sheets and printing them.

VBA Code:
Sub PrintCoverAndMainSheet()
    Dim FilePath As String
    Dim DefaultPath As String
    Dim FileName As String
    Dim CurrentDateTime As String
    Dim desiredOrder As Variant
    Dim i As Long
    Dim cover_ws As Worksheet
    Dim report_ws As Worksheet
    Dim Devation_ws As Worksheet
  
'    ' Set references to the source and destination sheets
'    Set cover_ws = ThisWorkbook.Sheets("Cover Sheet")
'    Set report_ws = ThisWorkbook.Sheets("BMS vs Azure DB")
'    Set Devation_ws = ThisWorkbook.Sheets("Deviation Exceeded")

'    Call CopyRowsDeviationExceeded
'    Call CopyRowsMissingPoints
  
'    ' Store the PageSetup.Pages.Count in cells T2, U2, and V2
'    report_ws.Range("T2").Value = cover_ws.PageSetup.pages.Count
'    report_ws.Range("U2").Value = report_ws.PageSetup.pages.Count
'    report_ws.Range("V2").Value = Devation_ws.PageSetup.pages.Count

    ' Get the current date and time
    CurrentDateTime = Format(Now, "mm-dd-yyyy_hh-mm_AMPM")

    ' Set the default file name with the current date and time appended
    FileName = "Datalake & BMS Data Verification Report " & CurrentDateTime & ".pdf"

    ' Get the default path for the user's Documents folder with the file name
    DefaultPath = Environ("USERPROFILE") & "\Documents\" & FileName

    ' Prompt the user to select the file path with a default file name
    FilePath = Application.GetSaveAsFilename(InitialFileName:=DefaultPath, _
                                              FileFilter:="PDF Files (*.pdf), *.pdf", _
                                              Title:="Save PDF As")

    ' If the user cancels, exit the macro
    If FilePath = "False" Then Exit Sub
 
'    ' Set the original sheet to a variable and get its index
'    Set OriginalActiveSheet = ActiveSheet
'    Set OriginalSheet = ThisWorkbook.Sheets("Cover Sheet")
'    OriginalIndex = OriginalSheet.Index

'    ' Move "Cover Sheet" to be the first sheet
'    OriginalSheet.Move Before:=ThisWorkbook.Sheets(1)

    ' Select the sheets in the desired order
    Sheets(Array("Cover Sheet", "BMS vs Azure DB", "Deviation Exceeded", "Missing Points")).Select
  
    ' Export the selected sheets to PDF at the chosen location
    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, FileName:=FilePath
  
'    ' Move "Cover Sheet" back to its original position
'    If OriginalIndex > 1 Then
'        OriginalSheet.Move After:=ThisWorkbook.Sheets(OriginalIndex)
'    End If
  
'    ' Select the original sheet again
'    OriginalActiveSheet.Select
  
End Sub
 
Last edited:

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
Makes not sense to me.

So what printer driver is ExportAsFixedFormat Type:=xlTypePDF using, if any? I have a MS PDF printer driver and a Adobe PDF printer driver installed.

When manually printing the the sheets I was selecting the Adobe PDF printer. Trying the MS PDF printer had the same correct outcome but now the script is working again. ???
 
Upvote 0

Forum statistics

Threads
1,222,697
Messages
6,167,702
Members
452,132
Latest member
Steve T

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