Home BeforePrint Event is Firing without Printing
Reply: 1

BeforePrint Event is Firing without Printing

Jem Eripol
1#
Jem Eripol Published in 2018-01-13 04:48:42Z

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, _
OpenAfterPublish:=True

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
wbA.Save
'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
'THIS IS WHERE THE ERROR IS LOCATED
If myFile <> "False" Then
    wsA.ExportAsFixedFormat _
        Type:=xlTypePDF, _
        Filename:=myFile, _
        Quality:=xlQualityStandard, _
        IncludeDocProperties:=True, _
        IgnorePrintAreas:=False, _
        OpenAfterPublish:=True
    'confirmation message with file info
    MsgBox "PDF file has been created: " _
      & vbCrLf _
      & myFile
End If

exitHandler:
    Exit Sub
errHandler:
    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.

Foxfire And Burns And Burns
2#
Foxfire And Burns And Burns Reply to 2018-01-13 04:56:20Z
wsA.ExportAsFixedFormat 

That line activates the BeforePrint Event. Actually, you are printing a PDF file. It works as a virtual PDF Printer.

You need to login account before you can post.

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

© 2016 Powered by mzan.com design MATCHINFO