Home BeforePrint Event is Firing without Printing
Reply: 0

BeforePrint Event is Firing without Printing

user3342 Published in July 20, 2018, 4:49 am

I have the code below. It makes me wonder why the BeforePrint event in the workbook codes is fired even though I am not printing anything. The workbook definitely is not blank. The error is in the creation of the PDF file.

The file does a simple job of saving the worksheet in a PDF format with the name of the sheet, the file path of the workbook, and some details inside the worksheet.

Anything that I am missing? I am not new to VBA but this bugs me a lot today. I am using MS Excel 2016 on Windows 7 ultimate.

Edit: I tried removing the following codes below but the problem still persists:

IgnorePrintAreas:=False, _

The code is as follows:

Option Explicit

Public Sub createpdffile()

Dim wsA As Worksheet
Dim wbA As Workbook
Dim strPath As String
Dim strFile As String
Dim strPathFile As String
Dim myFile As Variant
Dim sheetname As String, sheetcode As String
Dim iRow As Long
Dim openPos As Integer
Dim closePos As Integer

'temporarily disable error handler so that I can see where the bug is.
'On Error GoTo errHandler

Set wbA = ActiveWorkbook
Set wsA = ActiveSheet
'get last row of sheet and set print area to last row with L column
iRow = wsA.Cells(Rows.Count, 1).End(xlUp).Row
wsA.PageSetup.PrintArea = wsA.Range("A1:L" & iRow).Address

'just checking name in sheet and removing needed characters
sheetname = wsA.Name
openPos = InStr(sheetname, "(")
closePos = InStr(sheetname, ")")
sheetcode = Mid(sheetname, openPos + 1, closePos - openPos - 1)

'get active workbook folder, if saved
strPath = wbA.Path
If strPath = "" Then
  strPath = Application.DefaultFilePath
End If
strPath = strPath & "\"

'create default name for saving file
strFile = sheetcode & " No. " & wsA.Cells(11, 9) & " - " & wsA.Cells(8, 3) & ".pdf"
strPathFile = strPath & strFile

'use can enter name and
' select folder for file
myFile = Application.GetSaveAsFilename _
    (InitialFileName:=strPathFile, _
        FileFilter:="PDF Files (*.pdf), *.pdf", _
        Title:="Select Folder and FileName to save")

'export to PDF if a folder was selected
If myFile <> "False" Then
    wsA.ExportAsFixedFormat _
        Type:=xlTypePDF, _
        Filename:=myFile, _
        Quality:=xlQualityStandard, _
        IncludeDocProperties:=True, _
        IgnorePrintAreas:=False, _
    'confirmation message with file info
    MsgBox "PDF file has been created: " _
      & vbCrLf _
      & myFile
End If

    Exit Sub
    MsgBox "Could not create PDF file" & vbNewLine & _
    "Please complete the details needed!", vbOKOnly + vbExclamation, "Error Saving as PDF"
    Resume exitHandler
End Sub

Solution from Foxfire and Burns and Burns' idea:

I added a public declaration before the main sub.

Option Explicit

'added line
Public myboolean as Boolean 

Public Sub createpdffile()

myboolean = True


Then I added a line in the BeforePrint Event that says:

If myboolean = True Then Exit Sub

This now bypasses the BeforePrint event when the virtual PDF printer is called.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.309548 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO