nyconfidential
New Member
- Joined
- Jul 22, 2015
- Messages
- 49
- Office Version
- 365
- 2016
Hey all, I am trying to export multiple sheets to one PDF. I'm using code that I've used successfully before, but now I get an 1004 message after a minute or so, stating: "Document not saved. The document may be open, or an error may have been encountered when saving" The code is below (It stalls/Errors out on the "ActiveSheet.ExportAsFixedFormat " method). Anyone know what I might be doing wrong? If I just use the first part of the code to select multiple sheets and then go to "File-> Print to Adobe PDF" it works fine. It also works fine if I only try to select one sheet instead of multiple sheets. Any help would be appreciated, thanks!
Code:
Public Sub PrintSheetsToPDF()
Dim wrkSheet As Worksheet
For Each wrkSheet In ThisWorkbook.Sheets
If wrkSheet.Visible = True Then
wrkSheet.Select False ' note "False" parameter makes sure previous sheets stay selected
If wrkSheet.Index > 1 Then
If Sheets(wrkSheet.Index - 1).Name = "ofac" Then Exit For
End If
End If
Next wrkSheet
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=ActiveWorkbook.Path & "\" & Format(Now, "mmddyyy hhmmss") & ".pdf", _
OpenAfterPublish:=False, IgnorePrintAreas:=False
End Sub