Save To PDF Forcing Event Infinite Loop

jtrombley24

New Member
Joined
Feb 21, 2016
Messages
27
Guys - the following code is causing an infinite loop because when the ExportAsFixedFormat runs, the code goes back to the top of the procedure.

How can I save as a pdf and still run the BeforePrint event?

(By the way, I've tried including the pdf code in the BeforePrint subroutine and also tried passing parameters to a module. No luck either way)

Thanks in advance!

Code:
Private Sub Workbook_BeforePrint(Cancel As Boolean)

'MsgBox "Do not use this method for printing." & vbNewLine & vbNewLine & "Click on the print button located on the NOID report page.", vbExclamation + vbOKOnly, "Print Error"

Dim i As Integer
Dim lotNum As String
Dim sfilename As String

lotNum = NOIDsheet.Cells(20, 1).Value

'   loop through lot numbers to build file name
For i = 21 To 33
    If NOIDsheet.Cells(i, 1).Value <> "" Then
        lotNum = lotNum & " " & NOIDsheet.Cells(i, 1).Value
    End If
Next i

'   add a - to the end so that the username/date can be appended
lotNum = Trim(lotNum) & "-"

'   append the username/date to the file name
sfilename = lotNum & Trim(NOIDsheet.Range("C5").Value)

'   filepath to the sharepoint site
Dim shptfilepath As String
shptfilepath = DataSheet.Range("B2").Value


NOIDsheet.ExportAsFixedFormat Type:=xlTypePDF, _
                              Filename:=shptfilepath & "\" & sfilename, _
                              Quality:=xlQualityStandard, _
                              IncludeDocProperties:=True, _
                              IgnorePrintAreas:=False, _
                              OpenAfterPublish:=False


End Sub
 

Some videos you may like

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.

ZVI

MrExcel MVP
Joined
Apr 9, 2008
Messages
3,800
Office Version
  1. 2016
  2. 2010
  3. 2007
Platform
  1. Windows
Use Application.EnableEvents = False/True to disable/enable events triggering, like this:
Rich (BB code):
Private Sub Workbook_BeforePrint(Cancel As Boolean)
 
  Application.EnableEvents = False
 
  ' ... Your code is here ...
   
  Application.EnableEvents = True
 
End Sub
 
Last edited:

Watch MrExcel Video

Forum statistics

Threads
1,122,261
Messages
5,595,144
Members
413,971
Latest member
User786

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