JuicyMusic
Board Regular
- Joined
- Jun 13, 2020
- Messages
- 210
- Office Version
- 365
- Platform
- Windows
Gurus, I tried to figure out how to insert a line of coding at the DIM section and in the body of the code that will ONLY convert selected sheets into separate PDF files.
I have a code that runs perfectly....but it will generate a PDF for the sheets that I don't want to convert to PDF.
This code also places the PDF in whatever folder I am in.
Here is my code. I wish I could take the credit for it. I found it online, luckily. Thank you so so much. I can't wait to see how this is done.
I have a code that runs perfectly....but it will generate a PDF for the sheets that I don't want to convert to PDF.
This code also places the PDF in whatever folder I am in.
Here is my code. I wish I could take the credit for it. I found it online, luckily. Thank you so so much. I can't wait to see how this is done.
VBA Code:
Sub ExportEachSheet2AsPDF()
Dim objWS As Worksheet
Dim strFileName As String
Dim strPath As String
Dim strCrntWS As String
strPath = ThisWorkbook.Path 'Assigned current file path
strCrntWS = ActiveSheet.Name 'Assigned current sheetname
Application.ScreenUpdating = False 'Disabled screen updating
For Each objWS In Worksheets
objWS.Select
strFileName = objWS.Name & ".pdf" 'Assigned worsksheet name as filename
'Following code will put '\' if it is not there
'-----------------------------------------------
If VBA.Right(strPath, 1) <> Application.PathSeparator Then
strPath = strPath & Application.PathSeparator
End If
'The following code will decide the export file type and then export it
'to the given path considering other required parameters.
'-----------------------------------------------------------------------
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, _
Filename:=strPath & strFileName, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=False 'This will prevent to open the file after publishing
Next objWS
Worksheets(strCrntWS).Select 'After exporting all the worksheets, it will return to active sheet
Application.ScreenUpdating = True 'Enabled screen updating
MsgBox "All sheets have been exported as PDF.", vbInformation + vbOKOnly, "Exported as PDF"
End Sub