I've been a longtime lurker and am now a first time poster! I hope this isn't the case, but it looks like it may be a bug on Microsoft's side of things.
I have a relatively simple macro that is run on the BeforeClose and BeforePrint conditions. The sub's purpose is to print a pdf to a networked location with a time stamp.
Everything works great as long as the macro is not called in the BeforePrint event. Here is the code:
Private Sub exportPDF()
'Application.ScreenUpdating = False
Dim wb As Workbook
Dim ws As Worksheet
Dim filePath As String
Dim pdfName As String
Dim fullPDF As String
Dim TimeStamp As String
Set wb = ThisWorkbook
Set ws = wb.Sheets(1)
TimeStamp = Format(Now(), "mm-dd-yyyy_hhmm-AMPM")
'On Error GoTo ErrHandler:
filePath = Left(wb.FullName, InStrRev(ActiveWorkbook.FullName, Application.PathSeparator))
pdfName = Replace(ActiveWorkbook.Name, ".xls", "_")
fullPDF = filePath + pdfName + TimeStamp
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, _
Filename:=fullPDF, _
Quality:=xlQualityMinimum, IncludeDocProperties:=True, _
IgnorePrintAreas:=False, OpenAfterPublish:=False
'ErrHandler:
'Debug.Print Err.Number & "-" & Err.Description
Exit Sub
'Application.ScreenUpdating = True
End Sub
Now, this instance works fine...
Private Sub Workbook_BeforePrint(Cancel As Boolean)
If Not Me.Saved Then Me.Save
End Sub
But if I put the call to exportPDF in there, it throws up an error or crashes depending on if I have the error handler on.
Edit: This code seems to "activate" even when the sub of BeforePrint shouldn't even be invoked. It crashes even when I simply try to close the file. The exact same code is invoked at the BeforeClose sub, and it causes no problems there.
Moderator's note: Moved to other forum to get better exposure for this specific problem --6SJ
Ok, I fixed the issue. The desired end result was a time stamped PDF in the same directory as the Excel file. I also wanted this file saved every time the worksheet was closed and/or printed. This was achieved by the Me.Save call in the BeforeClose and BeforePrint subs. I was trying to also call the exportPDF sub I previously defined in the BeforeClose and BeforePrint subs. This is where the error was invoked. The solution was to call the exportPDF sub in the AfterSave sub.
This is my fully functional code:
Private Sub exportPDF()
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Dim wb As Workbook
Dim ws As Worksheet
Dim filePath As String
Dim pdfName As String
Dim fullPDF As String
Dim TimeStamp As String
Set wb = ThisWorkbook
Set ws = wb.Sheets(1)
TimeStamp = Format(Now(), "mm-dd-yyyy_hhmm-AMPM")
filePath = Left(wb.FullName, InStrRev(ActiveWorkbook.FullName, Application.PathSeparator))
pdfName = Replace(ActiveWorkbook.Name, ".xls", "_")
fullPDF = filePath + pdfName + TimeStamp
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, _
Filename:=fullPDF, _
Quality:=xlQualityMinimum, IncludeDocProperties:=True, _
IgnorePrintAreas:=False, OpenAfterPublish:=False
Application.ScreenUpdating = True
Application.DisplayAlerts = False
End Sub
Private Sub Workbook_AfterSave(ByVal Success As Boolean)
exportPDF
End Sub
Private Sub Workbook_BeforeClose(Cancel As Boolean)
If Not Me.Saved Then Me.Save
End Sub
Private Sub Workbook_BeforePrint(Cancel As Boolean)
If Not Me.Saved Then Me.Save
End Sub
Thank you to everyone that looked at this. I hope this helps someone else in the future.
Bookmarks