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
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
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:
Upvote 0

Forum statistics

Threads
1,214,826
Messages
6,121,797
Members
449,048
Latest member
greyangel23

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