Performance Degrades After Creating PDF

masouder

Board Regular
Joined
Jul 5, 2013
Messages
79
Office Version
  1. 2013
Platform
  1. Windows
I wrote code to create a PDF file containing one to many sheets based on user selections. The code reads a setup sheet to set the orientation and fit to page settings for each sheet. The code also has to run on a PC and a Mac.

The issue I am experiencing is that after creating a PDF the performance of other code in workbook degrades considerably. For example, code to hide rows in a sheet that normally completes instantaneously takes over 30 seconds after creating a PDF. This happens on PCs and on Macs. Closing and reopening the file corrects the problem, but as soon as I create a PDF it returns. To further analyze the issue I commented out select sections of code, but the only time the issue did not occur is when I commented out all cope related to page orientation and fit to page and all code related to the variant variables - in effect, gutting all important code.

Below is the main procedure, which prepares the sheets, and the procedure that creates the PDF on a PC. Any ideas are greatly appreciated.

VBA Code:
Sub CreatePDFs()
    Dim C As Integer
    Dim FirstOrientation As Integer
    
    Dim sPDFSheets As String
    Dim FileName As String
    
    Dim bFirstPage As Boolean
    Dim bManual As Boolean
    
    Dim PDFSheets As Variant
    
    Dim PDF As Range
    Dim Setup As Range
    
    Set PDF = Range("CreatePDFSheetStart")
    Set Setup = shtPrintSetup.Range("PrintSetupSheetStart")
    
    bManual = (Application.Calculation = xlManual)
    
    Application.ScreenUpdating = False
    Application.Calculation = xlManual
    
    bFirstPage = False
    shtEnd.Visible = True
    
    C = 1
    
    Do Until PDF.Offset(C, 0) = ""
        If UCase(PDF.Offset(C, cCreatePDFPrint)) = "X" Then
            Worksheets(CStr(PDF.Offset(C, cCreatePDFSheet))).Activate
            
            Select Case Setup.Offset(C, cPrintSetupDefaultOrientation)
                Case Is = "Portrait"
                    With ActiveSheet.PageSetup
                        .Orientation = xlPortrait

                        If Setup.Offset(C, cPrintSetupPortraitFitWidth) = "" Then
                            .FitToPagesWide = False
                        Else
                            .FitToPagesWide = CInt(Setup.Offset(C, cPrintSetupPortraitFitWidth))
                        End If

                        If Setup.Offset(C, cPrintSetupPortraitFitLength) = "" Then
                            .FitToPagesTall = False
                        Else
                            .FitToPagesTall = CInt(Setup.Offset(C, cPrintSetupPortraitFitLength))
                        End If

                    End With

                Case Is = "Landscape"
                    With ActiveSheet.PageSetup
                        .Orientation = xlLandscape

                        If Setup.Offset(C, cPrintSetupLandscapeFitWidth) = "" Then
                            .FitToPagesWide = False
                        Else
                            .FitToPagesWide = CInt(Setup.Offset(C, cPrintSetupLandscapeFitWidth))
                        End If

                        If Setup.Offset(C, cPrintSetupLandscapeFitLength) = "" Then
                            .FitToPagesTall = False
                        Else
                            .FitToPagesTall = CInt(Setup.Offset(C, cPrintSetupLandscapeFitLength))
                        End If

                    End With

            End Select
            
            sPDFSheets = sPDFSheets & "|" & ActiveSheet.Name
                    
            If Not bFirstPage Then
                FirstOrientation = ActiveSheet.PageSetup.Orientation
                bFirstPage = True
            End If
            
        End If
        
        C = C + 1
        
    Loop
    
    shtEnd.PageSetup.Orientation = FirstOrientation
    sPDFSheets = sPDFSheets & "|" & shtEnd.Name
    
    'Set file name
    If Range("CalcPDFDate") = Date Then
        Range("CalcLastPDFNumber") = Range("CalcLastPDFNumber") + 1
    Else
        Range("CalcPDFDate") = Date
        Range("CalcLastPDFNumber") = 1
    End If
    
    FileName = ThisWorkbook.Name & " " & Format(Range("CalcPDFDate"), "mmddyyyy") & "-" & Range("CalcLastPDFNumber")
        
    If sPDFSheets <> "" Then
        PDFSheets = Split(Mid(sPDFSheets, 2), "|")
        
        Select Case Range("CreatePDFMacPC")
            Case Is = "Mac"
                SavePDFMac FileName, PDFSheets, True
            Case Is = "PC"
                SavePDFPC FileName, PDFSheets, True
        End Select
                
     Else
        MsgBox "There are no sheets to convert to PDF", vbOKOnly, "Create PDF"
    End If
        
CleanUp:
    Set PDF = Nothing
    Set Setup = Nothing
    PDFSheets = Empty
    
    shtEnd.Visible = False

    shtCreatePDF.Activate
    
    If Not bManual Then Application.Calculation = xlAutomatic
    
End Sub


Sub SavePDFPC(FileName As String, PDFSheets As Variant, Show As Boolean)
    Dim C As Integer
    
    Dim sSelected As String
    
    Dim Selected As Variant
    
    Dim ws As Worksheet
    
    'Test if the Microsoft Add-in is installed
    If Dir(Environ("commonprogramfiles") & "\Microsoft Shared\OFFICE" _
            & Format(Val(Application.Version), "00") & "\EXP_PDF.DLL") = "" Then
        MsgBox "PDF Add-In has not been installed", vbCritical, "Create PDF"
        Exit Sub
    End If
    
    On Error Resume Next
    
    Sheets(PDFSheets).Select
    
    'Deselect End sheet
    C = 1
    For Each ws In ActiveWindow.SelectedSheets
        If C <> ActiveWindow.SelectedSheets.Count Then
            sSelected = sSelected & "|" & ws.Name
        End If

        C = C + 1

    Next ws

    Selected = Split(Mid(sSelected, 2), "|")

    Sheets(Selected).Select
    
    ActiveSheet.ExportAsFixedFormat _
            Type:=xlTypePDF, _
            FileName:=FileName, _
            Quality:=xlQualityStandard, _
            IncludeDocProperties:=True, _
            IgnorePrintAreas:=False, _
            OpenAfterPublish:=Show
    
    On Error GoTo 0
    
    Selected = Empty
    PDFSheets = Empty
    
End Sub
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.

masouder

Board Regular
Joined
Jul 5, 2013
Messages
79
Office Version
  1. 2013
Platform
  1. Windows
Thanks for the reply, but I'm not I follow. Are you suggesting changing the Application.PrintCommunication property to false before setting orientation and fit to page then resetting to true at the end? I also don't understand the suggestion about then PDF printer versus line printer. Can you elaborate? Finally, will either of these improve performance of other routines after the PDF is created?
 

Forum statistics

Threads
1,148,339
Messages
5,746,177
Members
423,998
Latest member
eakenila

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